Document: prerendering property

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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.

Value

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.

Examples

To run an activity while the page is prerendering, you can check for the prerendering property. You could for example run some analytics:

js
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:

js
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:

js
if (document.prerendering) {
  document.addEventListener("prerenderingchange", initAnalytics, {
    once: true,
  });
} else {
  initAnalytics();
}

Note: See the Speculation Rules API landing page and particularly the Unsafe speculative loading conditions section for more information on the kinds of activities you might wish to delay.

Specifications

Specification
Prerendering Revamped
# dom-document-prerendering

Browser compatibility

BCD tables only load in the browser

See also