Skip to content

Lesson 8 of 14 · 18m

Speculation Rules: Prefetch and Prerender

Chrome's Speculation Rules API lets the browser fetch or fully render the next page before the click — how to use it without phantom pageviews or request storms.

The fastest navigation is the one that already happened. Chrome's Speculation Rules API lets you tell the browser which pages the user is likely to visit next, and it prefetches — or fully prerenders — them in the background. When the user actually clicks, the document is already there. Prerendered navigations routinely land LCP under 200 ms because the page was painted before the click existed.

The wrong mental model is treating prefetch as a cache hint. <link rel="prefetch"> fetched a resource into the HTTP cache and hoped something used it. Speculation rules are a different thing: a declarative contract where the browser runs the whole page — parsing, subresources, layout — in a hidden environment, then swaps it in on activation.

The API, concretely

<script type="speculationrules">
{
  "prefetch": [
    { "where": { "href_matches": "/product/*" }, "eagerness": "moderate" }
  ],
  "prerender": [
    {
      "where": { "and": [
        { "href_matches": "/*" },
        { "not": { "href_matches": "/logout" } },
        { "not": { "href_matches": "/cart/checkout*" } }
      ]},
      "eagerness": "moderate"
    }
  ]
}
</script>

Two rule types, one gate. prefetch downloads the top-level document's response body only — cheap, low-commitment. prerender builds the page in a hidden prerendering browsing context: scripts run, images load, layout happens, but no scrolling, no unload, no visibility to the user.

eagerness decides when speculation fires:

  • immediate / eager — as soon as the rules parse (or close to it). For near-certain next steps.
  • moderate — on 200 ms hover, or pointerdown on mobile touch. The sweet spot for link lists.
  • conservative — on pointerdown only, the last moment before a real navigation.

Per-link overrides exist: document rules take href_matches/selector_matches, and list rules take explicit urls.

What can go wrong, honestly

  • Prerender runs your JavaScript. Analytics, consent probes, and session pings execute during prerender. If your analytics counts a pageview on script load rather than on activation, you will book phantom views. Gate activation-sensitive code on document.prerendering / the prerenderingchange event — activationStart tells you when the prerendered page actually became visible.
  • It's GET-only and same-origin by default. The spec refuses to speculate on non-GET navigations, and cross-origin prerender needs explicit opt-in handling. Do not point prerender at sign-outs, mutations, or anything with side effects — the not: filters in the example exist for a reason.
  • Server load is real. An immediate rule on a page with a hundred links can fire a hundred requests. moderate keeps volume sane; rate-limit at the CDN if you ship eager.
  • It's Chromium-only today. Firefox and Safari ignore the script safely — it's a progressive enhancement, not a dependency. Ship it because it helps the Chrome majority, not because it's universal.

Debugging it

DevTools → Application → Speculative loads lists every rule the page declared and whether each speculated load fired, succeeded, or failed (and why — memory pressure, user opt-out, non-GET). A Sec-Purpose: prefetch / Purpose: prefetch request header on the server side is how you tell a speculative hit from a real one in your access logs — useful when your hit counts look inflated.

Practice

Start with prefetch at moderate eagerness on your most-clicked templates — product cards, article links, docs nav. Measure the activation rate (a page that prerenders but is never visited is waste) and the LCP delta for navigationType === "prerender" in your RUM. Then graduate the highest-confidence flows — "next page" in a checkout or a tutorial sequence — to prerender.

// in RUM tagging
const navEntry = performance.getEntriesByType("navigation")[0];
if (navEntry.activationStart > 0) navType = "prerender"; // or document.prerendering during the hidden phase

WebPageTest and Lighthouse both understand speculation now, so a prerendered lab run tells you what activation will cost before you ship it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.