-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: suspend auctions during prerendering (#12763)
* Core: suspend auctions during prerendering * Delay only auctions by default * add option to delay queue --------- Co-authored-by: Patrick McCann <[email protected]>
- Loading branch information
1 parent
8813b40
commit 13b18fa
Showing
4 changed files
with
101 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {logInfo} from '../utils.js'; | ||
|
||
/** | ||
* Returns a wrapper around fn that delays execution until the page if activated, if it was prerendered and isDelayEnabled returns true. | ||
* https://developer.chrome.com/docs/web-platform/prerender-pages | ||
*/ | ||
export function delayIfPrerendering(isDelayEnabled, fn) { | ||
return function () { | ||
if (document.prerendering && isDelayEnabled()) { | ||
const that = this; | ||
const args = Array.from(arguments); | ||
return new Promise((resolve) => { | ||
document.addEventListener('prerenderingchange', () => { | ||
logInfo(`Auctions were suspended while page was prerendering`) | ||
resolve(fn.apply(that, args)) | ||
}, {once: true}) | ||
}) | ||
} else { | ||
return Promise.resolve(fn.apply(this, arguments)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {delayIfPrerendering} from '../../../src/utils/prerendering.js'; | ||
|
||
describe('delayIfPrerendering', () => { | ||
let sandbox, enabled, ran; | ||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
enabled = true; | ||
ran = false; | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}) | ||
|
||
const delay = delayIfPrerendering(() => enabled, () => { | ||
ran = true; | ||
}) | ||
|
||
it('should not delay if page is not prerendering', () => { | ||
delay(); | ||
expect(ran).to.be.true; | ||
}) | ||
|
||
describe('when page is prerendering', () => { | ||
before(() => { | ||
if (!('prerendering' in document)) { | ||
document.prerendering = null; | ||
after(() => { | ||
delete document.prerendering; | ||
}) | ||
} | ||
}) | ||
beforeEach(() => { | ||
sandbox.stub(document, 'prerendering').get(() => true); | ||
}); | ||
function prerenderingDone() { | ||
document.dispatchEvent(new Event('prerenderingchange')); | ||
} | ||
|
||
it('should run fn only after prerenderingchange event', async () => { | ||
delay(); | ||
expect(ran).to.be.false; | ||
prerenderingDone(); | ||
expect(ran).to.be.true; | ||
}); | ||
|
||
it('should not delay if not enabled', () => { | ||
enabled = false; | ||
delay(); | ||
expect(ran).to.be.true; | ||
}) | ||
}) | ||
}) |