Skip to content

Commit

Permalink
Merge pull request #111 from disqus/links-cleanup
Browse files Browse the repository at this point in the history
Improve component resources cleanup
  • Loading branch information
tterb committed Aug 24, 2021
2 parents c8adc27 + ab9da8b commit 2c3441c
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 8 deletions.
23 changes: 19 additions & 4 deletions examples/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { DiscussionEmbed, CommentCount, CommentEmbed, Recommendations } from 'disqus-react';
import { DiscussionEmbed, CommentCount, CommentEmbed } from 'disqus-react';

/* eslint-disable max-len */
const ARTICLES = {
Expand Down Expand Up @@ -114,7 +114,6 @@ class Article extends React.Component {
<div className="article-body">
<h1>{data.title}</h1>
<p>{data.body}</p>
<Recommendations shortname={this.props.disqusShortname} />
<DiscussionEmbed shortname={this.props.disqusShortname} config={threadConfig} />
</div>
</div>
Expand Down Expand Up @@ -148,12 +147,28 @@ class App extends React.Component {
});
}

navigateHome() {
this.setState({
articleId: null,
});
}

render() {
return (
<div className="container">
{this.state.articleId ?
<Article id={this.state.articleId} disqusShortname={this.state.disqusShortname} />
: <Home disqusShortname={this.state.disqusShortname} />
<div>
<Article
id={this.state.articleId}
disqusShortname={this.state.disqusShortname}
/>
<div className="button-wrapper">
<button onClick={this.navigateHome.bind(this)}>Back</button>
</div>
</div>
: <Home
disqusShortname={this.state.disqusShortname}
/>
}
<div className="button-wrapper">
<button onClick={this.changeShortname.bind(this)}>Change shortname</button>
Expand Down
9 changes: 8 additions & 1 deletion src/CommentCount.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';
import { insertScript, removeScript, debounce, shallowComparison } from './utils';
import {
insertScript,
removeScript,
debounce,
shallowComparison,
removeResources,
} from './utils';
// Constants
import {
COMMENT_COUNT_CLASS,
Expand Down Expand Up @@ -48,6 +54,7 @@ export class CommentCount extends React.Component {

// count.js only reassigns this window object if it's undefined.
window.DISQUSWIDGETS = undefined;
removeResources();
}

render() {
Expand Down
8 changes: 7 additions & 1 deletion src/DiscussionEmbed.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import { insertScript, removeScript, shallowComparison } from './utils';
import {
insertScript,
removeResources,
removeScript,
shallowComparison,
} from './utils';
// Constants
import {
CALLBACKS,
Expand Down Expand Up @@ -64,6 +69,7 @@ export class DiscussionEmbed extends React.Component {
while (disqusThread.hasChildNodes())
disqusThread.removeChild(disqusThread.firstChild);
}
removeResources();
}

getDisqusConfig(config) {
Expand Down
8 changes: 7 additions & 1 deletion src/Recommendations.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import { insertScript, removeScript, shallowComparison } from './utils';
import {
insertScript,
removeScript,
shallowComparison,
removeResources,
} from './utils';
// Constants
import {
RECOMMENDATIONS_ID,
Expand Down Expand Up @@ -72,6 +77,7 @@ export class Recommendations extends React.Component {
while (recommendations.hasChildNodes())
recommendations.removeChild(recommendations.firstChild);
}
removeResources();
}

render() {
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export function removeScript(id, parentElement) {
parentElement.removeChild(script);
}

export function removeResources() {
// Remove the bundles that the Disqus scripts add to prevent duplicated resources when navigating between pages
const disqusResources = window.document.querySelectorAll(
// eslint-disable-next-line max-len
'link[href*="disquscdn.com/next/embed"], link[href*="disquscdn.com/next/recommendations"], link[href*="disqus.com/next/config.js"], script[src*="disquscdn.com/next/embed"], script[src*="disqus.com/count-data.js"], iframe[title="Disqus"]'
);
disqusResources.forEach(el => el.remove());
}

export function debounce(func, wait, runOnFirstCall) {
let timeout;
return function () {
Expand Down
3 changes: 3 additions & 0 deletions tests/CommentCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ test('Cleans script and window attributes on unmount', () => {
const scriptQuery = baseElement.querySelectorAll(`#${COMMENT_COUNT_SCRIPT_ID}`);
// Make sure the script is removed
expect(scriptQuery.length).toEqual(0);
// Make sure the resources created by the count script are removed
const resourcesQuery = baseElement.querySelectorAll('script[src*="disqus.com/count-data.js"]');
expect(resourcesQuery.length).toEqual(0);
// Make sure window.DISQUSWIDGETS is removed
expect(global.window.DISQUSWIDGETS).toBeUndefined();
});
5 changes: 4 additions & 1 deletion tests/DiscussionEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ test('Cleans script and window attributes on unmount', () => {
const { baseElement, unmount } = render(<Component config={DISQUS_CONFIG} />);
unmount();
const scriptQuery = baseElement.querySelectorAll(`#${EMBED_SCRIPT_ID}`);
// Make sure the script is removed
// Make sure the embed script is removed
expect(scriptQuery.length).toEqual(0);
// Make sure the resources created by the embed script are removed
const resourcesQuery = baseElement.querySelectorAll('link[href*="disquscdn.com/next/embed"], link[href*="disqus.com/next/config.js"], script[src*="disquscdn.com/next/embed"]');
expect(resourcesQuery.length).toEqual(0);
// Make sure window.DISQUS is removed
expect(global.window.DISQUS).toBeUndefined();
});
3 changes: 3 additions & 0 deletions tests/Recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ test('Cleans script and window attributes on unmount', () => {
const scriptQuery = baseElement.querySelectorAll(`#${RECOMMENDATIONS_SCRIPT_ID}`);
// Make sure the script is removed
expect(scriptQuery.length).toEqual(0);
// Make sure the resources created by the embed script are removed
const resourcesQuery = baseElement.querySelectorAll('link[href*="disquscdn.com/next/recommendations"]');
expect(resourcesQuery.length).toEqual(0);
// Make sure window.DISQUS is removed
expect(global.window.DISQUS_RECOMMENDATIONS).toBeUndefined();
});

0 comments on commit 2c3441c

Please sign in to comment.