Skip to content

Add external <script type=speculationrules>#11697

Open
vickiez wants to merge 12 commits intowhatwg:mainfrom
vickiez:spec-rules-external-script
Open

Add external <script type=speculationrules>#11697
vickiez wants to merge 12 commits intowhatwg:mainfrom
vickiez:spec-rules-external-script

Conversation

@vickiez
Copy link
Copy Markdown

@vickiez vickiez commented Sep 24, 2025

Add external <script type="speculationrules">

Allows a speculation rules script element to have a src pointing to a
file of speculation rules, enabling developers to easily reuse a set of
speculation rules across HTML documents.

WICG/nav-speculation#348

Spec changes:


(See WHATWG Working Mode: Changes for more details.)


/scripting.html ( diff )
/speculative-loading.html ( diff )
/webappapis.html ( diff )

@vickiez vickiez force-pushed the spec-rules-external-script branch from 0f1c473 to 59d995a Compare December 10, 2025 12:51
@vickiez vickiez marked this pull request as ready for review December 10, 2025 13:29
Copy link
Copy Markdown
Collaborator

@noamr noamr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! See initial comments.

source Outdated
data-x="concept-request-url">URL</span> is <var>url</var>, <span
data-x="concept-request-client">client</span> is <var>document</var>'s <span>relevant settings
object</span>, <span data-x="concept-request-destination">destination</span> is "<code
data-x="">speculationrules</code>", and <span data-x="concept-request-mode">mode</span> is
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This destination needs to be defined in the fetch spec.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a change in progress here: whatwg/fetch#1841. Happy to help with that one, is there a proper order for merging the changes across specs?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging them as close together as possible provided they are both unblocked and generally agreed upon is fine. But regarding this PR: is there cross-browser interest in this feature overall? The PR template is pretty scant.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also for some context - this change was meant to be included in the base speculation rules spec change to achieve completeness. I was planning on merging it into Domenic's branch, but his change was merged into HTML first. See the comment here - #11123 (comment)

@domfarolino
Copy link
Copy Markdown
Member

See #11697 (comment), but just so the sentiment doesn't get lost in that thread, let me ask here: is there cross-browser support for this? I'm asking because the PR template is a bit bare and I'm not sure where other folks stand on this PR, although I'm happy to review it.

@hiroshige-g
Copy link
Copy Markdown
Contributor

hiroshige-g commented Mar 4, 2026

For CSP, there are three conceptually separate things being discussed in the context of external speculation rules:

Do we want to settle all of them before implementing (or shipping?) external <script type="speculationrules">? Or do we want to proceed incrementally? Or should we decouple [2] and [3] from the core effort of external <script type="speculationrules">?

I think applying script-src to external <script type="speculationrules"> can be a first incremental step (satisfying [1] in pre-[2] settings, while [2] and [3] can be still separately tackled), but I'd like to hear how the CL author (@vickiez) and CSP folks (@mikewest @antosart) think about this kind of planning.

@antosart
Copy link
Copy Markdown
Member

antosart commented Mar 4, 2026

Proceeding in steps makes sense to me, although it probably makes sense to have an idea of the plan so that decisions we take first don't block us later on.

For [1] and [2], if we want a new CSP directive that applies to speculationrules, defining it right away might make more sense than enforcing script-src temporarily and later changing it. Unless we decide that the new directive will fall back to script-src if not present (which could make perfectly sense), in which case doing [1] first and later [2] sounds good to me.

The discussion on [3] can come later. If the only reason for exempting headers is this one, then a new directive could solve that problem, too.

@vickiez
Copy link
Copy Markdown
Author

vickiez commented Mar 13, 2026

Thank you for clarifying the issues @hiroshige-g, and +1 to @antosart on proceeding incrementally with a plan in mind.

I think it makes sense to use script-src as a fallback to the new directive; sounds like developers did expect CSP to apply to script elements and it already takes effect for inline scripts. This is how I see the plan:

After [1]:

  • Header: exempt from CSP
  • Script element: script-src-elem → script-src → default-src

After [2] introduces a new speculationrules directive:

  • Header: speculationrules → default-src
  • Script element: speculationrules → script-src-elem → script-src → default-src

For [3], the header/script discrepancy can be resolved as part of [2] since a dedicated directive avoids the strict-CSP problem that motivated the header exemption in the first place.

So the plan would be:

  1. Apply initial CSPs to external <script type="speculationrules"> (CL)
  2. Follow-up (Integrate with speculation rules w3c/webappsec-csp#776 + Add the "speculationrules" destination fetch#1841): Introduce the speculationrules directive, applying to both header and script element, with appropriate fallback chain

Does that align with everyone's thinking?

@antosart
Copy link
Copy Markdown
Member

After [2] introduces a new speculationrules directive:

  • Header: speculationrules → default-src
  • Script element: speculationrules → script-src-elem → script-src → default-src

Why not having both resolve in the same way as speculationrules → script-src-elem → script-src → default-src? Seems easier not to have to differentiate between the two.

@vickiez
Copy link
Copy Markdown
Author

vickiez commented Mar 16, 2026

After [2] introduces a new speculationrules directive:

  • Header: speculationrules → default-src
  • Script element: speculationrules → script-src-elem → script-src → default-src

Why not having both resolve in the same way as speculationrules → script-src-elem → script-src → default-src? Seems easier not to have to differentiate between the two.

I agree it is simpler to have the same fallback chain. I was thinking of the friction that motivated header CSP exemption in https://chromestatus.com/feature/5123809745829888, but sites will be able to avoid breakage from strict script-src policies by using the new speculationrules directive. If that sounds good to everyone, here is the revised plan:

After [2] introduces a new speculationrules directive:

  • Header and script element: speculationrules → script-src-elem → script-src → default-src

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants