script-content
Validate the body of <script> elements against the spec that governs the value of its type attribute.
Currently supported content formats:
type value | Spec |
|---|---|
importmap | HTML Living Standard § Parse an import map string |
speculationrules | HTML Living Standard § 7.6 Speculation rules |
type attribute matching is ASCII case-insensitive, matching how user agents look up the script's effective MIME type.
type="importmap"
The rule reports the following issues for inline import maps:
- The element body is empty or whitespace-only
- The body cannot be parsed as JSON
- The top-level value is not a JSON object
- The top-level value contains a key other than
imports,scopes, orintegrity importsorscopesis not a JSON object- A specifier map (
imports, or a value insidescopes) contains an empty key - A specifier map address (the value side) is not a string
- A specifier map address is not a URL-like specifier (does not start with
/,./,../, and is not an absolute URL) - A specifier key ends with
/but its address does not integrityis not a JSON object- An
integritykey is not a URL-like specifier - An
integrityvalue is not a string
❌ Examples of incorrect code for this rule
<script type="importmap"></script>
<script type="importmap">
{
"forbidden": {}
}
</script>
<script type="importmap">
{
"imports": {
"dir/": "/path/to/dir"
}
}
</script>
✅ Examples of correct code for this rule
<script type="importmap">
{
"imports": {
"app": "/path/to/app.js",
"dir/": "/path/to/dir/"
},
"scopes": {
"/scope/": {
"x": "./y.js"
}
}
}
</script>
type="speculationrules"
Speculation Rules are defined in the HTML Living Standard § 7.6 (the feature originated as the WICG nav-speculation draft, which now redirects to the HTML Standard). 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
{
"script-content": boolean
}
Validate the body of <script> elements when the type attribute selects a content format that has a spec (currently: importmap, speculationrules).
Default Severity
error