Network & PerformanceExperimental

Speculation Rules & Prerendering

Mental Model

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

Interactive Simulator

Speculation Rules: Hidden Prerender to Activation

Trace how moderate eagerness prerenders a target route in a hidden context and swaps it on click.

Step 1 / 3: 1. Pointer Hover (moderate eagerness)

User hovers over /checkout for 200ms. Speculation Rules queues a background prerender.

<script type="speculationrules">{"prerender":[{"eagerness":"moderate"}]}</script>
Internal State / Memory
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.

  1. A <script type="speculationrules"> block (or Speculation-Rules HTTP header) defines prefetch or prerender rules using URL lists or CSS where selectors.
  2. When an eagerness trigger fires (moderate on 200 ms hover, conservative on pointerdown), the engine allocates an isolated hidden browsing context.
  3. The hidden document parses HTML, runs JS, and paints frames, while blocking intrusive APIs (camera, audio, alert) until activation.
  4. On click, document.prerendering flips from true to false and prerenderingchange fires.
HTML
<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 eagerness controls, Sec-Purpose: prefetch;prerender headers, and a privacy-preserving proxy for cross-site prefetch.

Production Pitfalls

Production Pitfall
  • Inflated analytics & ad beacons: Scripts that fire on DOMContentLoaded count phantom pageviews for every hovered link; gate telemetry on !document.prerendering and prerenderingchange.
  • eagerness: immediate on large lists: Putting immediate on a 30-item catalog grid DDoS-attacks your own origin server on every page load.
When Not to Use
  • 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

Useful Prerequisites
Foundational node
Network & PerformanceSpeculation Rules & Prerendering

Mechanical Comparisons