01What is Speculation Rules Generator?
Speculation Rules Generator builds the JSON that goes inside a <script type="speculationrules"> tag. That JSON tells the browser which pages to prefetch or prerender before the user clicks. It can also check a rule set you already have, using the parsing rules in the HTML Standard. The syntax is strict, and a broken rule fails quietly. The browser drops it and you only notice when navigations stay slow. A rule set is a JSON object with up to three keys: prefetch, prerender and tag. Prefetch downloads the next page's HTML. Prerender goes further and renders the whole page in a hidden tab, so it can appear at once on click. Each key holds an array of rules. A list rule names fixed URLs in a urls array. A document rule uses a where condition to match links on the current page. Conditions are built from href_matches, which takes URL patterns, and selector_matches, which takes CSS selectors. You can combine them with and, or and not. In the builder you pick the action, the source and the eagerness for each rule. For a document rule you enter include patterns, exclude patterns and selectors to exclude. The tool joins them into an and condition, wrapping each exclusion in not. You can add a tag, a referrer policy and the anonymous-client-ip-when-cross-origin requirement. The output updates as you type and comes as a script tag or bare JSON. Eagerness controls when speculation starts. Immediate starts as soon as the rules are seen. Conservative waits for pointer or touch down. Moderate and eager sit between, based on hover time on desktop. If you leave it unset, list rules default to immediate and document rules to conservative. The validator flags a rule with both urls and where, an unknown key, a non-http URL, a bad eagerness value or relative_to on a document rule. It also flags a condition with more than one predicate key. The browser drops only the broken rule and keeps the rest, so the tool counts valid rules the same way. A worked example: choose prerender, document source, include /*, exclude /logout and /cart/*, and exclude the selector .no-prerender, with moderate eagerness. The result is a where clause with an and of four conditions, and the validator reports one valid rule. Limits: if a document rule has no include patterns, the builder uses /* so it matches every same-site link. URL pattern syntax is checked only where the browser supports URLPattern. expects_no_vary_search is checked only for being a string. Chrome and Edge support speculation rules from version 109. Firefox has them in Technology Preview, and Safari has them behind a flag.
Front-end developers who want faster page navigations on Chromium browsers. The tool runs entirely in your browser, so your data never leaves your device.
02How to use Speculation Rules Generator
- Pick prefetch or prerender and a list or document source
- Enter URLs, or include and exclude patterns and selectors
- Choose an eagerness level, or keep the default
- Check the validation messages under the output
- Copy the script tag into your page, or copy the JSON to serve as a file
03Worked examples
- Input:
Builder: prerender, document rule, include /*, exclude /logout and /cart/*, exclude selector .no-prerender, eagerness moderate - Result:
{"prerender":[{"where":{"and":[{"href_matches":"/*"},{"not":{"href_matches":"/logout"}},{"not":{"href_matches":"/cart/*"}},{"not":{"selector_matches":".no-prerender"}}]},"eagerness":"moderate"}]} 1 valid rule, no issues. - The default preset
- Input:
Builder: prefetch, list rule, URLs https://shop.example.net/sale and /pricing, referrer policy strict-origin, anonymous IP required, rule tag cross-site, rule set tag v2 - Result:
{"tag":"v2","prefetch":[{"urls":["https://shop.example.net/sale","/pricing"],"referrer_policy":"strict-origin","requires":["anonymous-client-ip-when-cross-origin"],"tag":"cross-site"}]} 1 valid rule, no issues. - Eagerness left unset, so list rules use immediate
- Input:
Builder: prerender, document rule, include /*, eagerness immediate - Result:
{"prerender":[{"where":{"href_matches":"/*"},"eagerness":"immediate"}]} Warning $.prerender[0]: Immediate prerender of every matching link can be expensive. Chrome allows 10 immediate prerenders at a time. - Valid, but a warning
- Input:
Builder: prerender, document rule, all include, exclude and selector boxes empty, eagerness immediate - Result:
{"prerender":[{"eagerness":"immediate"}]} Error $.prerender[0]: Cannot tell the source: give urls (list rule) or where (document rule), not both or neither. - Edge case: add an include pattern such as /* to fix it
- Input:
Validate {"prefetch":[{"urls":["/a","mailto:[email protected]"]}],"prerender":[{"where":{"href_matches":"/*","selector_matches":"a"}},{"where":{"href_matches":"/docs/*"},"eagerness":"hover"},{"where":{"href_matches":"/blog/*"},"eagerness":"moderate"}]} - Result:
$.prefetch[0]: "mailto:[email protected]" is not an http or https URL. $.prerender[0].where: A condition needs exactly one of and, or, not, href_matches, selector_matches; found href_matches, selector_matches. $.prerender[1]: eagerness must be one of immediate, eager, moderate, conservative. 1 valid rule. - Only the /blog/* rule survives
- Input:
Validate {"prerender":[{"urls":["/a"],"where":{"href_matches":"/*"}}]} - Result:
$.prerender[0]: Cannot tell the source: give urls (list rule) or where (document rule), not both or neither.
04Common use cases
- Making article and product links load instantly on hover
- Prefetching the likely next step in a sign-up or checkout flow
- Excluding logout, cart and other side-effect URLs from prerendering
- Checking why Chrome DevTools shows a rule set as invalid
- Writing a JSON file to serve with the Speculation-Rules header
- Prefetching a page on another origin while asking for an anonymous client IP
- Reviewing a rule set from a CMS plugin before turning it on site-wide
05Tips
- Exclude URLs that change state, such as logout or add-to-cart links, since prerendering loads them for real
- Start with moderate or conservative eagerness for prerender; Chrome allows only 2 of those at a time and drops the oldest
- relative_to belongs next to href_matches in a document rule, not at the rule level
- A served rules file needs Content-Type application/speculationrules+json
- Keep at least one include pattern, such as /*, so the document rule has a where clause
06Related concepts
prerender, prefetch, eagerness, href_matches, URL Pattern, Speculation-Rules header.
Frequently Asked Questions
What is Speculation Rules Generator?
Speculation Rules Generator is a free, browser-based developer tool on AIDToolStack. Build <script type="speculationrules"> JSON for prefetch and prerender with list or document rules and eagerness, then validate it. It requires no installation or sign-up and works entirely in your browser.
Is Speculation Rules Generator free to use?
Yes. No sign-up, no fees and no usage cap.
Does Speculation Rules Generator store my data?
No. It runs in your browser; nothing is uploaded.