The Speculation Rules API lets browsers prefetch or prerender likely future navigations to deliver much faster page loads; it’s designed for multi‑page sites and is currently experimental in many browsers.
The Speculation Rules API provides a declarative way to tell the browser which document URLs to prefetch or prerender based on user interactions, lists, or document rules. It aims to replace older, less-safe prerender techniques and to give developers more control over when and how speculation happens.
Benefits include faster perceived navigation, reduced latency for common flows, and a more explicit, auditable configuration for speculation behavior. It is particularly useful for multi-page applications (MPAs) where whole-document navigations are common.
The most common way to register rules is with a JSON block inside a script tag using type="speculationrules". Rules can include lists of URLs, prerender or prefetch actions, and conditions such as user eagerness or source type.
<script type="speculationrules">
{
"prerender": [
{
"source": "list",
"urls": ["/next-page", "/checkout"]
}
],
"prefetch": [
{
"source": "list",
"urls": ["/assets/heavy-image.jpg"]
}
]
}
</script>
You can also register rules dynamically from JavaScript by inserting a <script type="speculationrules"> node or by using document rules for automatic link speculation; the codelab demonstrates debugging and dynamic registration techniques.
// Example: inject rules at runtime
const script = document.createElement('script');
script.type = 'speculationrules';
script.textContent = JSON.stringify({
"prerender": [{ "source": "list", "urls": ["/next"] }]
});
document.head.appendChild(script);
Use DevTools to inspect speculation activity, start conservatively (prefetch before prerender), and measure cost vs. benefit—prerenders can consume CPU and network resources if overused. DebugBear and Chrome docs provide practical guidance for complex sites and monitoring.
| Browser | Typical status | Notes |
|---|---|---|
| Chrome (desktop & Android) | Supported | Full implementation in stable Chrome; actively documented by Chrome team |
| Edge (Chromium) | Supported | Matches Chromium behavior; same runtime as Chrome for speculation rules |
| Opera | Supported | Chromium-based builds inherit support |
| Firefox | Not supported / experimental | MDN marks the API experimental; Firefox has not shipped full support |
| Safari (macOS & iOS) | Limited | Apple’s WebKit has been cautious; expect partial support or flags |
| Other mobile browsers | Varies | Chromium-based mobile browsers follow Chrome; others vary widely |