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

DEV: Abort placing adsense if Ember component is destroyed #190

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
27 changes: 16 additions & 11 deletions assets/javascripts/discourse/components/google-adsense.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,27 @@ export default AdComponent.extend({
this._super();
},

_triggerAds() {
async _triggerAds() {
if (isTesting()) {
return; // Don't load external JS during tests
}

this.set("adRequested", true);
loadAdsense().then(function () {
const adsbygoogle = window.adsbygoogle || [];

try {
adsbygoogle.push({}); // ask AdSense to fill one ad unit
} catch (ex) {
// eslint-disable-next-line no-console
console.error("Adsense error:", ex);
}
});

await loadAdsense();

if (this.isDestroyed || this.isDestroying) {
// Component removed from DOM before script loaded
return;
}

try {
const adsbygoogle = (window.adsbygoogle ||= []);
adsbygoogle.push({}); // ask AdSense to fill one ad unit
} catch (ex) {
// eslint-disable-next-line no-console
console.error("Adsense error:", ex);
}
},

didInsertElement() {
Expand Down