The prerendering
read-only property of the Document
interface returns true
if the document is currently in the process of prerendering, as initiated via the Speculation Rules API.
A boolean. Returns true
if the document is currently in the process of prerendering, and false
if it is not. false
will be returned for documents that have finished prerendering, and documents that were not prerendered.
To run an activity while the page is prerendering, you can check for the prerendering
property. You could for example run some analytics:
if (document.prerendering) {
analytics.sendInfo("got this far during prerendering!");
}
When a prerendered document is activated, PerformanceNavigationTiming.activationStart
is set to a DOMHighResTimeStamp
value representing the time between when the prerender was started and the document was actually activated. The following function can check for prerendering and prerendered pages:
function pagePrerendered() {
return (
document.prerendering ||
self.performance?.getEntriesByType?.("navigation")[0]?.activationStart > 0
);
}
When the prerendered page is activated by the user viewing the page, the prerenderingchange
event will fire. This can be used to enable activities that previously would be started by default on page load but which you wish to delay until the page is actually viewed by the user. The following code sets up an event listener to run a function once prerendering has finished, on a prerendered page, or runs it immediately on a non-prerendered page:
if (document.prerendering) {
document.addEventListener("prerenderingchange", initAnalytics, {
once: true,
});
} else {
initAnalytics();
}