On this page
Before enabling speculation rules on a site, test which links a browser may request before a visitor navigates to them. This experiment uses a small local site to separate links selected by a rule from ordinary links, then checks what the browser actually does.
Key Takeaways
- A speculation rule identifies candidates; it does not guarantee that the browser will prefetch every matching URL.
- Prefetching requests resources ahead of navigation. Prerendering can also render and execute the destination page before it becomes visible.
- A request in browser tools or server logs is not proof that a person clicked a link.
- Test with safe, read-only pages first, and record the browser, version, and relevant settings alongside your observations.
What a speculation rule selects
A document rule can select links in the current page using a CSS selector. For example, a rule matching a[data-speculate] identifies anchors carrying that attribute as potential candidates. The browser may still decline to act because of its implementation, settings, resource limits, or other eligibility conditions.
That distinction matters when reading a network trace:
- Ordinary navigation: A link activation asks the browser to navigate to its destination.
- Prefetch: The browser may request a destination in advance, hoping to reuse the response for a later navigation. A prefetch does not establish that a later navigation occurred.
- Prerender: The browser may prepare a destination more fully before navigation, including rendering and executing page code. That can have different privacy, resource, and application-side effects from a prefetch.
The following experiment uses a prefetch rule, not a prerender rule. It is deliberately small so you can inspect its behavior before applying similar rules to a real site.
Advertisement
Test browser speculation rules links locally
You need Python 3 and a browser that implements the rule syntax below. Browser support and behavior were not verified for this article; check the current W3C specification and your browser vendor's documentation before relying on this syntax or deploying it. The example was not executed for this article.
Create a new empty directory and save the following as index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Speculation test</title>
<script type="speculationrules">
{
"prefetch": [{
"source": "document",
"where": { "selector_matches": "a[data-speculate]" },
"eagerness": "moderate"
}]
}
</script>
</head>
<body>
<h1>Speculation test</h1>
<p><a href="next.html" data-speculate>Marked link</a></p>
<p><a href="control.html">Unmarked control link</a></p>
</body>
</html>
Save this as next.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Destination</title>
</head>
<body>
<h1>Read-only destination</h1>
<p>This static page performs no state-changing action.</p>
</body>
</html>
Copy next.html to control.html in the same directory. Start a local server from that directory:
python3 -m http.server 8000 --bind 127.0.0.1
Open http://127.0.0.1:8000/index.html in the test browser. Stop the server with Ctrl+C when finished. These files contain only static content; do not substitute endpoints that modify data on GET, such as sign-out, deletion, or purchase URLs.
Record the setup
For each run, note:
- Browser name and exact version, operating system, and whether this is a normal or private browsing session.
- Relevant browser settings, flags, extensions, and network-saving settings that could affect preloading.
- Whether developer tools are open, whether their Disable cache setting is enabled, and whether the browser profile or cache was cleared between runs.
- Whether the rule is enabled and what action you took before each request, such as loading the page, hovering over a link, or clicking it.
Use a fresh test profile if practical, and keep unrelated extensions and other tabs out of the experiment. Developer-tool labels and speculation-specific panels vary by browser and version.
Compare the rule with a control
- Open developer tools and select the Network panel. Enable Preserve log if available, so requests remain visible after navigation. Start recording before loading
index.html. - Load the page, wait briefly, and inspect requests without clicking either link. Then hover over the marked link without clicking and inspect the log again. The
moderateeagerness setting is intended to allow a browser to act on interaction; its exact triggers and timing depend on the implementation. - Inspect any request for
next.html: its timing, initiator, request headers, and any browser-provided indication that it is speculative. Record what the tools actually show. Do not assume a particular header or panel is present. - Observe the unmarked link under the same conditions. It does not match this rule, although a browser or extension might have other preloading mechanisms.
- In a separate run, click the marked link and keep the log. Compare the navigation with the earlier request. Whether a prefetched response is reused, and how that appears in developer tools, can vary. A request alone does not prove reuse.
- Remove the speculation-rule script from
index.htmland repeat with the same settings. Ensure the edited page has loaded, and account for previously cached or prefetched resources when comparing runs.
If no speculative request appears, do not conclude immediately that the selector is wrong. Check browser support and settings, rule parsing, the link's attributes, the interaction you used, and any diagnostics the browser exposes. A valid candidate can still be skipped.
For cache-sensitive comparisons, note that disabling the cache in developer tools can change the behavior you are trying to observe. Run a separate comparison with that setting off; do not assume that either mode represents every visitor's experience.
Advertisement
Evaluate real links before deployment
Start with a narrow set of same-origin, read-only destinations that users are likely to visit. Review the actual URLs generated in the page, including query parameters and redirects, rather than assuming every matching anchor leads to an equivalent endpoint.
Consider these trade-offs:
- Privacy and expectations: An early request can disclose a visitor's IP address and request metadata to the destination before they choose to navigate. Avoid speculating on sensitive destinations and be especially cautious with third-party links. Review your privacy commitments and applicable requirements.
- Bandwidth and server load: Requests may be wasted when visitors never follow the links. Consider mobile and constrained connections, destination cost, and the number of candidates on each page.
- Cache and freshness: A prefetched response may not be reused, and reuse may be constrained by browser behavior and response caching policy. Check whether a later navigation receives content appropriate to the user's current state.
- Authentication: A destination may behave differently for signed-in and signed-out users. Test both paths without exposing session data in shared traces. Do not assume speculative requests always include—or always omit—credentials; check the relevant browser behavior and your application.
- Application side effects: GET endpoints should not change application state. Audit the destination and redirect chain for actions such as logging out or modifying records. Even read-only requests can affect analytics, rate limits, or logs.
- Measurement: Distinguish speculative requests from actual navigation where your browser and server instrumentation allow it. Do not count every page request as a click or confirmed page view.
Prerendering requires a separate review: destination scripts and rendering may run before the visitor sees the page. Do not extrapolate a safe prefetch result to prerendering without testing the destination's behavior and current browser restrictions.
Common Mistakes
- Selecting every link at once. Broad selectors make it difficult to predict requests and can include sensitive or expensive destinations.
- Treating a matching selector as a promise. Matching makes a link a candidate, not a guaranteed request or cache hit.
- Inferring clicks from server logs. Record navigation events separately from speculative traffic, and document the limits of that measurement.
- Testing only a warm cache or one signed-in session. Cache state, authentication, settings, and browser version can change the result.
- Using production action URLs as test targets. Keep the initial experiment isolated and read-only.
Best Practices
Roll out to a small, explicitly selected group of destinations after the local experiment. Verify the current specification and official documentation for each browser you intend to support, then repeat the test in the configurations that matter to your audience. Monitor request volume, errors, and navigation behavior separately, and retain a way to remove or narrow the rule if the results are undesirable.
Conclusion
Speculation rules are a way to nominate links for advance work, not a record of user intent. Use a controlled comparison to see which requests occur in your test browser, then review privacy, side effects, authentication, and resource costs before enabling rules on real pages.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.