Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

Speculation Rules: Prefetch, Prerender, Headers, and Debugging

How navigational speculation rules prefetch and prerender next pages, delivery options, eagerness, limits, CDN injection, and verification.

Navigational speculation rules tell the browser which same-origin (and limited cross-origin) links may be prefetched or prerendered before the user navigates. Most of this page follows the HTML living standard on speculative loading and Chrome’s Speculation Rules API guidance. Cloudflare Speed Brain is a common way zones get rules without editing HTML.

Speculative loading is the practice of performing navigation actions, such as prefetching, ahead of navigation starting. This makes subsequent navigations faster.

— WHATWG HTML, 7.6 Speculative loading

Speculation rules are how developers instruct the browser about speculative loading operations that the developer believes will be beneficial. They are delivered as JSON documents, via either: inline script elements with their type attribute set to “speculationrules”; or resources fetched from a URL specified in the Speculation-Rules HTTP response header.

— WHATWG HTML, 7.6.1 Speculation rules

Section titled “Speculation rules vs link prefetch: what gets faster”

Prefetch (via speculation rules) fetches the navigation document ahead of time; the browser stores it like a navigation, not only in the HTTP disk cache. Prerender goes further: the page loads and renders in a hidden background process until activation.

Prefetch: fetching a resource or document in advance to improve future performance. This post covers prefetching documents using the Speculation Rules API, rather than the similar, but older <link rel="prefetch"> option often used for prefetching subresources.

— Chrome, Debug speculation rules with Chrome DevTools

Key point: Prefetch speculation rules only prefetch the document, not its subresources. This uses fewer resources than a full prerender of a potential next navigation, but also means the subresources need to be fetched and rendered upon navigation.

— Chrome, Debug speculation rules with Chrome DevTools

Mechanism Typical use Subresources Full render before click
<link rel="prefetch"> Subresources or older doc prefetch Often subresources No
Speculation rules prefetch Next document navigation Document only No
Speculation rules prerender Likely next page Yes (in hidden renderer) Yes (until activated)

Speculation rules suit multi-page applications where each navigation is a new document. They are not for in-app “soft” route changes in single-page apps without a document navigation—see prerender pages.

How to add speculation rules: inline JSON and the Speculation-Rules header

Section titled “How to add speculation rules: inline JSON and the Speculation-Rules header”

Rules are JSON with at most tag, prefetch, and prerender keys. Each rule is either a list rule (urls) or a document rule (where with href_matches, selector_matches, and / or / not).

Inline <script type="speculationrules"> on the page

Section titled “Inline <script type="speculationrules"> on the page”

Put rules in <head> or <body> of the main frame, or inject with JavaScript (not via innerHTML on the script element—see add speculation rules dynamically).

<script type="speculationrules">
{
"prefetch": [
{ "urls": ["/chapters/5"] },
{
"eagerness": "moderate",
"where": {
"and": [
{ "href_matches": "/*" },
{ "not": { "selector_matches": ".no-prefetch" } }
]
}
}
]
}
</script>

The implementing speculation rules guide compares HTML, JavaScript, and header deployment for complex sites.

Speculation-Rules HTTP response header (CDN and edge)

Section titled “Speculation-Rules HTTP response header (CDN and edge)”

The header is a structured list of URLs pointing at rule sets. The browser fetches each URL with destination speculationrules, CORS mode, and accepts only responses whose MIME type is application/speculationrules+json.

The Speculation-Rules HTTP response header allows the developer to request that the user agent fetch and apply a given speculation rule set to the current Document. It is a structured header whose value must be a list of strings that are all valid URL strings.

— WHATWG HTML, 7.6.3 The Speculation-Rules header

Example header value:

Speculation-Rules: "/speculationrules.json"

The resource should be served with Content-Type: application/speculationrules+json and, if cross-origin, appropriate Access-Control-Allow-Origin—see Speculation-Rules HTTP header.

When a domain is on Cloudflare, product traffic may use the managed /cdn-cgi/ path; Speed Brain’s configuration is exposed via Speculation-Rules pointing at a Cloudflare-hosted rules URL (same pattern as in the fundamentals doc).

Eagerness settings: when prefetch and prerender actually run

Section titled “Eagerness settings: when prefetch and prerender actually run”

Eagerness controls how soon the browser may start speculative loads—especially for document rules. From conservative to immediate: conservative, moderate, eager, immediate (definitions in 7.6.1.1 Data model).

conservative: User agents should enact the candidate only when the user is very likely to navigate to this URL at any moment. For instance, the user might have begun to interact with a link.

— WHATWG HTML, 7.6.1.1 Data model

Chrome defaults: list rules default to immediate; document rules default to conservative. A practical starting point for many sites:

<script type="speculationrules">
{
"prerender": [{
"where": { "href_matches": "/*" },
"eagerness": "moderate"
}]
}
</script>

Document rules with immediate on every link can waste bandwidth—prefer moderate or conservative, or prefetch first then prerender on stronger signals (improve next page load performance).

Chrome speculation limits (prefetch and prerender caps)

Section titled “Chrome speculation limits (prefetch and prerender caps)”
Eagerness Prefetch limit Prerender limit
immediate 50 10
eager / moderate (desktop) 2 (FIFO) 2 (FIFO)
eager / moderate (mobile) 6 (FIFO) 2 (FIFO)
conservative 2 (FIFO) 2 (FIFO)

Chrome may also skip speculation when Save-Data, energy saver, low memory, the “Preload pages” setting, or extensions block it—see Chrome limits.

Server-side signals: Sec-Purpose, tags, and Cloudflare Speed Brain

Section titled “Server-side signals: Sec-Purpose, tags, and Cloudflare Speed Brain”

Identifying speculative requests on the server uses request headers. Prefetch typically sends Sec-Purpose: prefetch; prerender adds prerender.

Clicking on one of the rows also shows the Sec-Purpose: prefetch HTTP header, which is how these requests can be identified on the server side:

— Chrome, Debug speculation rules with Chrome DevTools

Rule tags (ASCII only) appear on speculative requests as Sec-Speculation-Tags (7.6.4, tag field).

Speed Brain (Cloudflare) adds Speculation-Rules to HTML responses with a conservative document-wide prefetch ruleset:

When Cloudflare’s Speed Brain feature is enabled, an HTTP header called Speculation-Rules is added to web page responses. The value for this header is a URL that hosts an opinionated Speculation-Rules configuration. This configuration instructs the browser to consider prefetching any future navigations with a conservative eagerness.

— Cloudflare, Speed Brain

Safeguards: prefetches do not hit origin if uncached at the edge, and routes that run Workers are skipped (Speed Brain caveats). Enable in the dashboard under Speed settings.

Returning non-2xx for a page you do not want prerendered cancels prerender; prefer delaying analytics and stateful work with document.prerendering and prerenderingchange when possible (detect prerender server-side).

Security and common mistakes with document rules

Section titled “Security and common mistakes with document rules”

Cross-site speculative loads run without credentials. Rule-set fetches from Speculation-Rules use same-origin credentials and CORS; wrong MIME types are ignored (7.6.5.1).

User-generated links are the main foot-gun: a malicious href like /logout could be speculatively requested for other users.

For example, if the href of a link can be entered by one user and displayed to all other users, a malicious user might choose a value like “/logout”, causing other users’ browsers to automatically log out of the site when that link is speculatively loaded.

— WHATWG HTML, 7.6.5.2 Injected content

Mitigations:

  • Exclude dangerous paths with not + href_matches (patterns in modern web guidance).
  • Do not put untrusted text inside <script type="speculationrules"> without escaping </script>.
  • Use Content Security Policy: script-src for inline rules; default-src affects prefetch destinations (7.6.5.2).

Exclude state-changing GET URLs explicitly when using broad href_matches": "/*" rules (start with prefetch).

Debug speculation rules in Chrome DevTools

Section titled “Debug speculation rules in Chrome DevTools”

Open Application → Background services → Speculative loads. Reload after opening the panel so CDP monitoring attaches (background services).

View What it shows
Speculative loads Whether this navigation used prefetch/prerender
Rules Rule sets and links into Elements
Speculations Per-URL status, failures, filters (url:, action:)

Prefetch appears in the Network panel at low priority with Sec-Purpose: prefetch. Prerender traffic runs in another renderer—use the renderer switcher or Speculations (debug prerender).

On the destination page, performance.getEntriesByType('navigation')[0].activationStart > 0 indicates an activated prerender (detect prerender in JavaScript).

Feature detection:

if (HTMLScriptElement.supports && HTMLScriptElement.supports('speculationrules')) {
// Browser supports speculation rules
}
  1. Load a page and confirm rules exist: inline <script type="speculationrules"> in the document, or a Speculation-Rules response header (as in Speed Brain testing).
  2. In Chrome DevTools, open Application → Background services → Speculative loads, reload, and review Rules and Speculations for successes and failure reasons.
  3. For prefetch, confirm low-priority requests in the Network panel and Sec-Purpose: prefetch on the request (debug prefetch).
  4. After navigating to a candidate next page, check activationStart or the Speculative loads summary for prefetch/prerender use (debug background services).
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page looks wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about.