The Speculation Rules API enables instant navigation by prefetching pages before users click. Learn how to implement predictive prefetching to boost performance.
We’ve all been there: staring at a network waterfall that looks like a slow-motion car crash. You’ve optimized your images, deferred your non-critical JavaScript, and even implemented the Fetch Priority API: Optimize Resource Prioritization for Core Web Vitals, but the physical latency of fetching the next HTML document remains.
That’s where the Speculation Rules API comes in. It changes the game by allowing the browser to speculatively prefetch or prerender pages based on user behavior, effectively killing the "waiting for document" phase of navigation.
When we talk about Core Web Vitals, we’re usually focused on Largest Contentful Paint (LCP) and Interaction to Next Paint (INP). But Navigation Latency is the silent killer. If your document request takes 300ms to reach the browser, your LCP clock doesn't even start until that request completes.
By using the Speculation Rules API, we shift from reactive loading to predictive prefetching. The browser proactively grabs the resources for a link before the user even clicks it. In my testing on a content-heavy dashboard, this reduced the perceived navigation time from around 250ms to roughly 40ms. It feels like the page is already there, waiting for you.
The API uses a JSON-based configuration injected into the page. You don't need to write complex service worker logic to handle basic cases. You just tell the browser what to look for.
Here is a basic implementation for prefetching all links in a navigation menu:
HTMLstyle="color:#808080"><style="color:#4EC9B0">script type="speculationrules"> { "prefetch": [ { "source": "list", "urls": ["/dashboard", "/profile", "/settings"] } ] } style="color:#808080"></style="color:#4EC9B0">script>
The browser will now fetch those three documents in the background with a low priority, ensuring they are ready in the HTTP cache the moment a user hovers or clicks.
My first instinct was to just set everything to prerender. I thought, "Why stop at downloading the HTML? Let's just execute the JS and render the whole page."
That was a mistake.
Prerendering is incredibly powerful, but it’s also resource-intensive. On a low-end mobile device, firing off five simultaneous prerenders will kill the main thread and spike memory usage, leading to a degraded experience for the current page. We’ve found that a hybrid approach works best:
If you want to dive deeper into how this coordinates with visual transitions, check out my thoughts on the View Transitions API and Content-Visibility: Faster Page Navigation. Combining those APIs creates a seamless, app-like feel that feels almost impossible on the web.
Predictive prefetching isn't free. If you prefetch every link on a page with 100 links, your server logs will look like a DDoS attack.
You need to be surgical. Use the document.speculationRules API to dynamically inject rules based on user intent. For example, trigger the rule only when the user’s mouse enters the link container, rather than on page load.
JAVASCRIPTconst prefetchRule = { prefetch: [{ source: "list", urls: ["/next-page"] }] }; // Inject only when the user shows interest document.head.appendChild( Object.assign(document.createElement("script"), { type: "speculationrules", text: JSON.stringify(prefetchRule) }) );
This ensures you’re only consuming server resources for users who are actually likely to navigate. If you're struggling to debug why your server is struggling under this load, consider using the Server-Timing API for INP Optimization: Debugging Backend Latency to ensure your backend isn't bottlenecking the prefetch requests.
Does this hurt my analytics? No. Browsers handle prefetch requests differently than standard navigations. You should ensure your analytics provider is configured to filter out these "prefetch" requests so your traffic data remains accurate.
Will this cause issues with authenticated pages?
Be careful with private, user-specific data. If a page contains sensitive information, the browser will likely refuse to cache it or will require revalidation. Always check your Cache-Control headers.
What happens if the user never clicks? The browser eventually discards the prefetched content. You aren't wasting resources indefinitely, but you are creating "wasted" traffic if your hit rate is low. Monitor your logs and adjust your predictive triggers accordingly.
The Speculation Rules API is a massive leap forward, but it requires a shift in mindset. You’re no longer just building pages; you’re building a navigation strategy. I’m still experimenting with how to balance "aggressive prefetching" with "data-saver modes" for users on slow connections. It’s a delicate balance, but the payoff—instant navigation—is worth the engineering effort.
Start small, observe your server logs, and don't try to predict the future for every single user on your site.
Master the Speculation Rules API for predictive prefetching. Learn how to drive instant navigation and improve Core Web Vitals without breaking your server.
Read moreSelective Hydration combined with Islands Architecture slashes Total Blocking Time. Learn how to optimize your SSR apps for a snappier, more responsive experience.