Skip to content

Commit

Permalink
fix(content): do not monetize during prerendering (#486)
Browse files Browse the repository at this point in the history
Add `visibilitychange` listener if the page is being prerendered

Co-authored-by: Diana Fulga <[email protected]>
Co-authored-by: Sid Vishnoi <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2024
1 parent e216ce1 commit faea531
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/content/services/monetizationTagManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,35 @@ export class MonetizationTagManager extends EventEmitter {
node.dispatchEvent(customEvent)
}

start(): void {
if (
document.readyState === 'interactive' ||
document.readyState === 'complete'
private isDocumentReady() {
return (
(document.readyState === 'interactive' ||
document.readyState === 'complete') &&
document.visibilityState === 'visible'
)
}

start(): void {
if (this.isDocumentReady()) {
this.run()
return
}

document.addEventListener(
'readystatechange',
() => {
if (document.readyState === 'interactive') {
if (this.isDocumentReady()) {
this.run()
} else {
document.addEventListener(
'visibilitychange',
() => {
if (this.isDocumentReady()) {
this.run()
}
},
{ once: true }
)
}
},
{ once: true }
Expand Down

0 comments on commit faea531

Please sign in to comment.