valid-speculation-rules
Validate the body of <script type="speculationrules"> elements against HTML Living Standard § 7.6 Speculation rules (the feature originated as the WICG nav-speculation draft, which now redirects to the HTML Standard). type attribute matching is ASCII case-insensitive, matching how user agents look up the script's effective MIME type.
Split out of the former script-content rule, alongside valid-importmap for type="importmap".
The rule reports the following issues for inline speculation rules:
- The element body is empty, whitespace-only, or cannot be parsed as JSON
- The top-level value is not a JSON object, or has no
prefetchorprerenderproperty - A top-level key other than
tag,prefetch, orprerenderis present - A
prefetch/prerendervalue is not a JSON array, or a rule in it is not a JSON object - A rule has a key other than
source,urls,where,relative_to,eagerness,referrer_policy,tag,requires,expects_no_vary_search, ortarget_hint sourceis not a string, or is a value other thanlistordocument- A list rule (explicit or inferred from
urls) is missingurls, or has awhere - A document rule (explicit or inferred from
where) is missingwhere, or hasurls - A rule has no
sourceand its source cannot be inferred (it has neitherurlsnorwhere, or has both) urlsis not a JSON array, is empty, or contains a non-string or empty-string itemeagernessis not a string, or is a value other thanimmediate,eager,moderate, orconservativewhereis not a JSON object, or does not contain exactly one predicate (and,or,not,href_matches, orselector_matches)- An
and/orpredicate is not a JSON array, or is empty - An
href_matches/selector_matchespattern is not a string or an array of strings, is empty, or contains an empty-string item
❌ Examples of incorrect code for this rule
<script type="speculationrules">
{
"prefetch": [{ "source": "list" }]
}
</script>
<script type="speculationrules">
{
"prefetch": [{ "source": "document", "where": {} }]
}
</script>
✅ Examples of correct code for this rule
<script type="speculationrules">
{
"prefetch": [
{
"source": "document",
"where": {
"and": [{ "href_matches": "/*" }, { "not": { "selector_matches": ".no-prefetch" } }]
},
"eagerness": "moderate"
}
],
"prerender": [{ "source": "list", "urls": ["/next"] }]
}
</script>
Interface
{
"valid-speculation-rules": boolean
}
Validate the body of <script type="speculationrules"> elements against the Speculation Rules spec.
Default Severity
error