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/ theprerenderingchangeevent —activationStarttells 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
immediaterule on a page with a hundred links can fire a hundred requests.moderatekeeps volume sane; rate-limit at the CDN if you shipeager. - 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 phaseWebPageTest 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?
Keep reading
Related articles from the SudoSecurity blog.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.
Inside TypeScript 7: What a Year-Long Rewrite in Go Actually Bought Developers
A deep look at how Microsoft's Go-based TypeScript 7 compiler achieves its speed gains, and what the Visual Studio Code team's year-long migration reveals about adopting it safely.
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.