JavaScript Document prerenderingchange Event

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Document event
Limited availability
Experimental

What You’ll Learn

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

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).

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.
  • Handlerdocument.onprerenderingchange or addEventListener("prerenderingchange", ...).
  • Typical use — defer analytics and unsafe side effects until activation.
  • Sibling propertydocument.prerendering is true during prerender.
  • Status — Experimental and Limited availability on MDN (not Baseline).

📝 Syntax

Use the event name with addEventListener, or set the handler property on document:

JavaScript
addEventListener("prerenderingchange", (event) => { });

onprerenderingchange = (event) => { };

Event type

A generic Event.

MDN pattern — defer until activation

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

Prerender status toolkit

APIAnswers
document.prerenderingPrerender in progress now?
prerenderingchangePrerender just activated?
activationStart > 0Was this page prerendered earlier?

⚠️ Why Delay Code Until Activation?

  • Analytics — a background prerender is not a real page view yet.
  • Ads / autoplay — start only when the user is looking.
  • Server / storage writes — avoid updating carts or preferences during speculative load.
  • Third-party scripts — many are not prerender-aware; load after activation.

See MDN’s Speculation Rules API and its unsafe speculative loading notes for more cases.

⚖️ prerenderingchange vs document.prerendering

Topicdocument.prerenderingprerenderingchange
KindRead-only booleanDocument event
When usefulCheck status right nowReact when activation happens
After activationBecomes falseAlready fired (or already missed)
Normal loadfalseDoes not fire
MDN statusExperimentalExperimental

⚡ Quick Reference

GoalCode / note
Listendocument.addEventListener("prerenderingchange", fn)
Handler propertydocument.onprerenderingchange = fn
Defer init (MDN)If prerendering then listen { once: true }, else run now
Past prerender?navigation[0]?.activationStart > 0
Sibling propertydocument.prerendering
MDN statusExperimental & Limited availability

🔍 At a Glance

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

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();
}
Try It Yourself

How It Works

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.

JavaScript
const out = document.getElementById("out");

out.textContent =
  "prerendering now? " +
  ("prerendering" in document ? document.prerendering : "(unsupported)");

document.onprerenderingchange = () => {
  out.textContent = "prerenderingchange: page activated";
};
Try It Yourself

How It Works

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");
}
Try It Yourself

How It Works

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";
}
Try It Yourself

How It Works

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";
});
Try It Yourself

How It Works

One helper keeps every side-effectful module consistent. Call whenActivated(startAds), whenActivated(initAnalytics), and so on without copying the if / else each time.

🚀 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.”

🔧 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.

Wait
3

User activates the page

Navigation commits; Document fires prerenderingchange.

Activate
4

Safe init runs

prerendering becomes false; analytics and ads may start.

📝 Notes

  • MDN: Experimental and Limited availability—Experimental banner shown above.
  • Not Deprecated or Non-standard.
  • On non-prerendered loads the event does not fire—always keep an else path.
  • Do not use the defer-init pattern alone for activation metrics (MDN).
  • Related learning: document.prerendering, hidden, DOMContentLoaded, JavaScript hub.

Browser Support

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).

Limited Check compat
Google Chrome Supported with Speculation Rules
Supported
Mozilla Firefox Not supported at time of writing
Not supported
Apple Safari Not supported at time of writing
Not supported
Microsoft Edge Chromium Speculation Rules
Supported
Opera Follow Chromium behavior
Partial
Internet Explorer No Speculation Rules API
Not supported
prerenderingchange Limited availability

Bottom line: Feature-detect document.prerendering, defer side effects with prerenderingchange when prerendering is true, and use activationStart for past prerender detection.

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.

Continue with readystatechange, document.prerendering, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check document.prerendering before waiting for the event
  • Use { once: true } for one-shot activation handlers
  • Feature-detect with "prerendering" in document
  • Combine with activationStart for activation metrics
  • Keep a normal-load else path that runs init immediately

❌ Don’t

  • Count prerender as a completed page view
  • Assume every browser supports Speculation Rules
  • Rely on the defer pattern alone for activation rates (MDN)
  • Start ads / autoplay during prerender without care
  • Ignore MDN Experimental / Limited availability warnings

Key Takeaways

Knowledge Unlocked

Five things to remember about prerenderingchange

Activation signal for Speculation Rules prerender — defer side effects.

5
Core concepts
🔍 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.

Next: Document readystatechange

Learn the Document event that fires whenever document.readyState changes.

readystatechange →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful