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

This should avoid surprising error messages being printed to the console
This commit is contained in:
David Taylor 2023-11-07 23:44:39 +00:00 committed by GitHub
parent 789be84744
commit 36f4ebc64b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 10 deletions

View File

@ -141,22 +141,27 @@ export default AdComponent.extend({
this._super(); this._super();
}, },
_triggerAds() { async _triggerAds() {
if (isTesting()) { if (isTesting()) {
return; // Don't load external JS during tests return; // Don't load external JS during tests
} }
this.set("adRequested", true); this.set("adRequested", true);
loadAdsense().then(function () {
const adsbygoogle = window.adsbygoogle || []; await loadAdsense();
if (this.isDestroyed || this.isDestroying) {
// Component removed from DOM before script loaded
return;
}
try { try {
const adsbygoogle = (window.adsbygoogle ||= []);
adsbygoogle.push({}); // ask AdSense to fill one ad unit adsbygoogle.push({}); // ask AdSense to fill one ad unit
} catch (ex) { } catch (ex) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error("Adsense error:", ex); console.error("Adsense error:", ex);
} }
});
}, },
didInsertElement() { didInsertElement() {