Skip to main content
KBY Technologies Logo

Speculation Rules API: Architecting Safe Prerendering

How document rules, score-based heuristics and No-Vary-Search headers let the Speculation Rules API prerender navigations without wasting origin compute.

KBY Technologies10 min read
Speculation Rules API: Architecting Safe Prerendering

Underlined words are defined in the KBY Lexicon. Tap or click one to see what it means.

A cold client-side navigation on a content-heavy SPA typically costs 200-800ms between click and first paint, even with a warm cache: route resolution, hydration, and data fetching all execute serially on the main thread. Prefetching the JavaScript bundle helps marginally, but it does nothing for render cost.

For full document navigations (it does not speed up client-side route changes inside an already loaded SPA), the speculation rules api closes this gap by letting the browser fetch, render, and in some cases fully execute a destination page before the user commits to the click, so the eventual navigation becomes a document swap rather than a cold load.

Rolling it out safely means understanding how that mechanism works, avoiding corrupted analytics and leaked session state, and knowing where it breaks under production load.

#The Cost of Client-Side Navigation Latency

Traditional mitigation for navigation latency falls into two camps: <link rel="prefetch">, which warms the HTTP cache but leaves parsing, layout, and hydration on the critical path; and full route-level code-splitting with eager imports, which shifts cost earlier but doesn’t touch render or data-fetch latency. Neither addresses the actual bottleneck on JavaScript-heavy micro-frontend shells: the browser still has to construct a render tree from zero at click-time.

The speculation rules api — incubated in the WICG and shipping in Chromium-based browsers — introduces a declarative ruleset that tells the browser which URLs are candidates for background prefetch or full prerendering. A prerendered page is rendered in a hidden, inactive browsing context with restricted capabilities, then activated instantly on navigation.

Correctly implemented, this converts a 600ms navigation into a sub-50ms tab swap. Incorrectly implemented, it duplicates analytics events, triggers unintended side effects on GET endpoints, and burns origin compute on speculative traffic that never converts.

#Architectural Breakdown: How the Speculation Rules API Works

The specification defines two distinct behaviours under one surface, and conflating them is the most common architectural mistake teams make when adopting the speculation rules api.

#
Prefetch vs Prerender Semantics

Prefetch rules fetch the document response and hold it in a per-document in-memory cache for that navigation. No DOM construction, no script execution. Prerender rules go further: they load the destination in a separate, throttled renderer process, run its JavaScript, and construct a full DOM — all before the user clicks. The trade-off is resource cost against latency reduction.

StrategyRendering costLatency reductionSide-effect riskBest fit
rel=prefetch (link)NoneLow (network only)LowStatic assets, fonts
Speculation Rules: prefetchNoneMediumLow-MediumMulti-page document navigations
Speculation Rules: prerenderFull DOM + JS executionHigh (near-instant)High if unmitigatedHigh-confidence next-page navigation
Service Worker cache warmNoneMediumMedium (stale data)Offline-first PWAs

Because prerendering executes real JavaScript, any code that fires on load — analytics beacons, A/B test bucketing, cart mutation calls — will fire during speculation, not just on actual navigation. The API mitigates this via the document.prerendering flag and the prerenderingchange event, but only if engineering teams explicitly gate side-effecting code behind them.

speculation rules api

#Implementation Logic: Rolling Out the Speculation Rules API

A production rollout of the speculation rules api follows four stages: rule authoring, eligibility scoring, activation gating, and observability. Skipping the third stage is where most incidents originate.

#
1. Static and Dynamic Rule Authoring

Rules can be inlined as a <script type="speculationrules"> block or served via HTTP header for CDN-level control without touching the HTML template:

1{
2  "prerender": [
3    {
4      "source": "document",
5      "where": {
6        "and": [
7          { "href_matches": "/product/*" },
8          { "not": { "href_matches": "/product/*/checkout" } }
9        ]
10      },
11      "eagerness": "moderate"
12    }
13  ],
14  "prefetch": [
15    {
16      "source": "list",
17      "urls": ["/api/session-warm", "/cart"]
18    }
19  ]
20}

The eagerness field controls the trigger threshold: immediate fires as soon as the rule is parsed, eager fires on the slightest sign of intent, moderate waits for sustained hover intent (roughly 200ms in Chrome on desktop), and conservative waits for pointerdown or touch. Document rules default to conservative and list rules to immediate. For most catalogue-style navigation, moderate gives the best balance between hit rate and wasted speculative renders.

#
2. Serving Rules via HTTP Header for CDN Control

Rather than baking rules into every HTML response, serve them from the edge so the ruleset can be updated without a redeploy:

1location / {
2    add_header Speculation-Rules '"/speculation-rules.json"';
3    add_header No-Vary-Search 'params=("utm_source" "utm_campaign")';
4}

Both headers are structured fields, so the URL and parameter names must be sent as quoted strings, hence the single quotes around the nginx values; the rules file itself must be served as application/speculationrules+json. The No-Vary-Search header is essential in any architecture using UTM parameters or session tracking query strings — without it, the browser treats /product/42?utm_source=x and /product/42 as distinct cache keys, and every prerendered speculative fetch is wasted the moment a tracking parameter changes.

#
3. Gating Side Effects During Speculation

Any code with observable side effects — analytics, feature-flag evaluation with server mutation, cart pings — must check the prerendering state before firing:

Speculation Rules API: Architecting Safe Prerendering architecture diagram 2
1function trackPageView(pageId) {
2  if (document.prerendering) {
3    document.addEventListener('prerenderingchange', () => {
4      trackPageView(pageId);
5    }, { once: true });
6    return;
7  }
8  analytics.track('page_view', { pageId, prerendered: performance.getEntriesByType('navigation')[0]?.activationStart > 0 });
9}

This defers the event until the page is actually activated, preventing phantom page-view inflation in downstream analytics pipelines — a failure mode that has silently corrupted conversion funnels in more than one production rollout.

Loading diagram...

#Failure Modes and Edge Cases

The speculation rules api degrades gracefully by design — unsupported browsers simply ignore the ruleset — but several failure modes are specific to the speculative execution model itself, not to browser support gaps.

  • POST and non-idempotent GET routes: the specification explicitly forbids prerendering pages reachable only via forms, and any GET endpoint with mutation side effects (add-to-cart-via-link patterns) will silently execute during speculation, corrupting state before the user has committed to anything.
  • Memory pressure eviction: prerendered renderers consume full process memory. Under constrained devices, Chromium evicts speculative renders opportunistically, meaning the “instant” navigation degrades back to a cold load with no error surfaced to the application — monitoring must treat this as expected variance, not a bug.
  • Cross-origin restriction: prerendering a cross-origin but same-site URL requires the destination to opt in via a Supports-Loading-Mode: credentialed-prerender response header, and cross-site prerendering is not supported at all; without the opt-in, the page is simply not prerendered.
  • Service worker interception conflicts: a fetch event handler that assumes an active, foregrounded document will misbehave in the prerendering context, since self.clients reports the speculative client with a distinct visibility state.
  • Duplicate mutation on activation: code that binds mutation logic to DOMContentLoaded rather than gating on document.prerendering runs during the speculative render whether or not the user ever navigates, and runs again only if activation code re-triggers it, because DOMContentLoaded does not fire a second time on activation; enforce idempotency at the request layer.

#Scaling and Security Trade-offs

Rolling this out fleet-wide inside a micro-frontend shell interacts directly with existing architectural patterns around edge caching, CSP, and origin capacity planning. The trade-offs are not symmetrical — prerendering shifts cost from user-perceived latency to origin compute and privacy surface area.

  • Origin load: a naive eagerness: immediate rule across a high-traffic catalogue can 3-5x origin request volume from speculative renders that never convert; start with moderate and measure activation-to-speculation ratio before widening scope.
  • Cache key correctness: without precise No-Vary-Search configuration, CDN cache hit ratios for speculative traffic collapse, and every hover event becomes a full origin round trip.
  • Referrer leakage: prerendered documents can read document.referrer and run their code before the user commits (they join session history only on activation); sensitive destinations (account settings, payment flows) should be explicitly excluded via not clauses in the ruleset rather than relying on default restrictions.
  • CSP interaction: a strict Content-Security-Policy script-src blocks an inline speculationrules script unless it allows it with the 'inline-speculation-rules' keyword plus a hash or nonce; the violation appears in the console but is easy to miss because nothing visibly breaks.
  • Third-party script cost: tag-manager scripts that fire network calls on load will execute during speculative prerendering unless explicitly gated, multiplying third-party vendor billing for impressions that never resulted in an actual page view.
  • Bot and crawler exposure: automated crawlers that programmatically hover or rapidly traverse links can trigger disproportionate prerender volume; rate-limiting speculative fetch endpoints separately from standard navigation traffic is advisable at the edge layer.

The specification text itself, maintained by the WICG, is the authoritative reference for eagerness thresholds and header semantics: WICG Speculation Rules specification. Teams adopting the speculation rules api at scale should treat the ruleset as a distinct deployable artifact with its own rollback path, versioned independently from the application bundle, since a malformed rule fails silently rather than throwing a build error — the only signal is a navigation that quietly stops feeling instant.

Evidence trail

Sources and verification

Primary documentation and external technical references used in this article.

  1. 01WICG Speculation Rules specificationwicg.github.io

About this page

Credited to

Dates

Published

Last updated

Corrections

No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.

Found this useful? Follow KBY for more like it.

Reader Interaction

Comments

Add a thoughtful note on Speculation Rules API: Architecting Safe Prerendering. Comments are checked for spam and held for moderation before appearing.

Loading comments...

More from KBY that goes with this article.

Discover more

Lexicon Definitions

Was this useful?

Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.

Engineering insights, direct to you.

Receive the latest Systems Engineering tutorials, production guides, Engineering Labs and operational best practices.