Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve component resources cleanup #111

Merged
merged 5 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});