Speculation Rules & Prerendering
<script type="speculationrules"> instructs Chromium to prefetch HTML or fully prerender predicted next pages in a hidden tab before the user clicks. When the user clicks the link, the engine swaps the pre-painted page in atomically for near-zero LCP.
Speculation Rules: Hidden Prerender to Activation
Trace how moderate eagerness prerenders a target route in a hidden context and swaps it on click.
User hovers over /checkout for 200ms. Speculation Rules queues a background prerender.
<script type="speculationrules">{"prerender":[{"eagerness":"moderate"}]}</script>- documentPrerendering
- true
- hiddenTabAllocated
- true
- visibleToUser
- false
Under the Hood
While bfcache handles instant Back/Forward navigation and the Preload Scanner handles subresources on the current page, Speculation Rules tackle forward clicks. Think of it as staging the next theater set behind a closed curtain before the scene change.
- A
<script type="speculationrules">block (orSpeculation-RulesHTTP header) definesprefetchorprerenderrules using URL lists or CSSwhereselectors. - When an
eagernesstrigger fires (moderateon 200 ms hover,conservativeon pointerdown), the engine allocates an isolated hidden browsing context. - The hidden document parses HTML, runs JS, and paints frames, while blocking intrusive APIs (camera, audio,
alert) until activation. - On click,
document.prerenderingflips fromtruetofalseandprerenderingchangefires.
<script type="speculationrules">
{
"prerender": [{
"where": { "href_matches": "/docs/*" },
"eagerness": "moderate"
}]
}
</script>Problem Genealogy
- 2011 (
<link rel="prerender">): Rendered full pages unconditionally, burning battery and bandwidth; deprecated across browsers. - 2018 (No-state Prefetch): Downloaded subresources without executing JS, saving bandwidth at the cost of slower activation.
- 2023+ (Speculation Rules API): Reintroduced full prerendering with strict
eagernesscontrols,Sec-Purpose: prefetch;prerenderheaders, and a privacy-preserving proxy for cross-site prefetch.
Production Pitfalls
- Inflated analytics & ad beacons: Scripts that fire on
DOMContentLoadedcount phantom pageviews for every hovered link; gate telemetry on!document.prerenderingandprerenderingchange. eagerness: immediateon large lists: Puttingimmediateon a 30-item catalog grid DDoS-attacks your own origin server on every page load.
- When not to use
prerender: Avoid prerendering URLs with server-side side effects (logout links, add-to-cart GET endpoints) or pages heavy on WebGL/video memory.
Connections & Logical Next Steps
Hover prefetching and Speculation Rules warm route chunks and loader data before navigation commits.
Preload fetches subresources for the current page; Speculation Rules fetches or prerenders full future navigations.
bfcache accelerates Back/Forward history traversal; Speculation Rules accelerates forward navigations.
Mechanical Comparisons
bfcache accelerates Back/Forward history traversal; Speculation Rules accelerates forward navigations.
Preload scanner prioritizes current-page subresources, while Speculation Rules prerender next-page navigations.