The Document prerenderingchange event fires when a prerendered page becomes active (the user views it). Learn MDN’s defer-analytics pattern, how it pairs with document.prerendering and activationStart, and five try-it labs.
01
Kind
Document event
02
Type
Event
03
Means
Prerender activated
04
API
Speculation Rules
05
Pair with
document.prerendering
06
Status
Experimental
Fundamentals
Introduction
With the Speculation Rules API, browsers can load a page in the background before the user clicks. That hidden phase is prerendering. When the user finally navigates to that page, the prerender is activated—and prerenderingchange fires on the Document.
That moment is the safe point to start analytics, ads, client storage updates, or other work that should only run when someone is really viewing the page.
💡
Beginner tip
On a normal (non-prerendered) page load, this event never fires. Always check document.prerendering first: if it is false, run your init immediately; if true, wait for prerenderingchange (MDN).
Concept
Understanding prerenderingchange
A Document event that answers: “Did this prerendered page just become the page the user is viewing?”
Fires on a prerendered document when it is activated (MDN).
Event type — a generic Event.
Handler — document.onprerenderingchange or addEventListener("prerenderingchange", ...).
Typical use — defer analytics and unsafe side effects until activation.
Sibling property — document.prerendering is true during prerender.
Status — Experimental and Limited availability on MDN (not Baseline).
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property on document:
Four facts to remember about Document prerenderingchange.
Event type
Event
Plain Event
Means
Activated
User views page
Use
Defer init
Analytics / ads
Status
Experimental
Not Baseline
Hands-On
Examples Gallery
Examples follow MDN Document: prerenderingchange event. On a normal lab load, document.prerendering is usually false, so the else branch runs—that is still the correct MDN pattern.
📚 Getting Started
MDN defer-until-activation pattern and the handler property.
Example 1 — Defer Analytics Until Activation (MDN)
If prerendering, wait for prerenderingchange; otherwise init immediately.
JavaScript
function initAnalytics() {
console.log("Analytics initialized");
}
if (document.prerendering) {
document.addEventListener("prerenderingchange", initAnalytics, {
once: true,
});
} else {
initAnalytics();
}
MDN’s core pattern. During prerender the listener waits; on a normal load the else branch runs right away. Use { once: true } so the handler does not stick around after activation.
Example 2 — document.onprerenderingchange
Set the handler property and show current prerender status in the UI.
Prefer addEventListener when you need multiple listeners. The property form is fine for small demos. The event only fires if this document was prerendered and then activated.
📈 Measure, Detect & Boot Safely
MDN activation measurement, feature detection, and a reusable helper.
Example 3 — Measuring Prerender Activations (MDN)
Combine prerendering, prerenderingchange, and activationStart.
JavaScript
if (document.prerendering) {
document.addEventListener(
"prerenderingchange",
() => {
console.log("Prerender activated after this script ran");
},
{ once: true },
);
} else if (performance.getEntriesByType("navigation")[0]?.activationStart > 0) {
console.log("Prerender activated before this script ran");
} else {
console.log("This page load was not via prerendering");
}
MDN warns that the simple defer-init pattern alone is not enough for activation metrics—activation may already have happened before your script runs. This three-way check covers “now,” “already activated,” and “never prerendered.”
Example 4 — Feature Detect the Event Path
Guard for browsers that lack document.prerendering / Speculation Rules.
JavaScript
const out = document.getElementById("out");
const supported = "prerendering" in document;
if (!supported) {
out.textContent = "prerendering API missing — run normal init";
// initAnalytics();
} else if (document.prerendering) {
document.addEventListener("prerenderingchange", () => {
out.textContent = "Activated after prerender";
}, { once: true });
out.textContent = "Waiting for prerenderingchange...";
} else {
out.textContent = "Supported; not prerendering — run normal init";
}
Experimental APIs must degrade gracefully. If the property is missing, treat the page like a normal load and start your init without waiting for an event that will never come.
Example 5 — Reusable whenActivated Helper
Wrap MDN’s pattern in a small helper you can reuse for ads, analytics, or boot.
JavaScript
function whenActivated(fn) {
if ("prerendering" in document && document.prerendering) {
document.addEventListener("prerenderingchange", fn, { once: true });
} else {
fn();
}
}
whenActivated(() => {
document.getElementById("out").textContent =
"Safe to start analytics / ads now";
});
One helper keeps every side-effectful module consistent. Call whenActivated(startAds), whenActivated(initAnalytics), and so on without copying the if / else each time.
Applications
🚀 Common Use Cases
Deferring analytics until the user actually views a prerendered page.
Loading third-party ad or chat scripts after activation.
Updating carts / client storage only after a real navigation.
Measuring how often Speculation Rules prerenders become visits.
Teaching why pageshow alone is not enough for “user saw the page.”
Under the Hood
🔧 How It Works
1
Speculation Rules prerender
The browser loads the page in the background; document.prerendering is true.
Prerender
2
Scripts defer side effects
Your code registers prerenderingchange instead of starting analytics immediately.
Document prerenderingchange is marked Experimental and Limited availability on MDN (not Baseline). Primarily Chromium with Speculation Rules. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Experimental · Limited availability
Document prerenderingchange
Fires when a Speculation Rules prerender is activated (user views the page).
LimitedCheck compat
Google ChromeSupported with Speculation Rules
Supported
Mozilla FirefoxNot supported at time of writing
Not supported
Apple SafariNot supported at time of writing
Not supported
Microsoft EdgeChromium Speculation Rules
Supported
OperaFollow Chromium behavior
Partial
Internet ExplorerNo Speculation Rules API
Not supported
prerenderingchangeLimited availability
Bottom line: Feature-detect document.prerendering, defer side effects with prerenderingchange when prerendering is true, and use activationStart for past prerender detection.
Wrap Up
Conclusion
prerenderingchange is the activation signal for Speculation Rules prerender. Pair it with document.prerendering to defer analytics and other side effects until the user really views the page, and use activationStart when you need past-prerender metrics.
Activation signal for Speculation Rules prerender — defer side effects.
5
Core concepts
📄01
Activated
user views page
Event
🔍02
Check first
document.prerendering
Guard
📈03
Metrics
+ activationStart
Timing
👋04
Else path
normal load now
Fallback
⚠️05
Experimental
feature-detect
Compat
❓ Frequently Asked Questions
It fires on a prerendered document when it is activated — that is, when the user actually views the page after Speculation Rules prerender (MDN).
MDN marks Document prerenderingchange as Experimental and Limited availability (not Baseline). It is not Deprecated or Non-standard.
When document.prerendering is true, add a once listener for prerenderingchange to run code after activation. If prerendering is false, run that code immediately (MDN).
Not safely by itself. MDN notes the defer-init pattern can miss activations that already happened. Combine prerendering, prerenderingchange, and navigation activationStart.
A generic Event. Listen with document.addEventListener("prerenderingchange", ...) or document.onprerenderingchange.
Analytics, ads, autoplay, client storage writes, and other side effects that assume the user is viewing the page. See MDN’s Speculation Rules unsafe speculative loading guidance.
Did you know?
MDN warns that pageshow can fire during prerender—before the user ever sees the page. For “the user is viewing this document now,” prerenderingchange (plus visibility / reveal events where appropriate) is the right Speculation Rules signal.