Skip to content

Add speculation rules prefetch#11426

Merged
domenic merged 17 commits intomainfrom
speculation-rules
Sep 10, 2025
Merged

Add speculation rules prefetch#11426
domenic merged 17 commits intomainfrom
speculation-rules

Conversation

@domenic
Copy link
Copy Markdown
Member

@domenic domenic commented Jul 3, 2025

Other spec PRs that should be merged shortly after this one:


This ports the relevant text from https://github.com/WICG/nav-speculation/blob/90ccea636881ab7c9b4aabd04bfa233ef33477ee/speculation-rules.bs, with minor improvements and additions. It does not port the actual navigational prefetching algorithms, which are left in https://wicg.github.io/nav-speculation/prefetch.html for now.

Part of #11123.

Notable changes from that document:

  • Allows treating prerender rules as prefetch rules.

  • Allows the presence of target_hint (a prerender-specific speculation rule key), but ignores it.

  • Follows the precedent set by import maps a bit more closely in how it integrates with the script processing model. Normatively, it fires error events on the global object for the few things that cause complete parsing failures. (Chromium recently changed to do this.)

  • Adds conformance requirements for speculation rules JSON documents, following the precedent set by import maps.

  • Adds explicit callouts for when the implementation may report warnings for parsing failures, following the precedent set by import maps.

  • Fixes a few minor bugs in the parsing algorithms. E.g., the previous text did not correctly deal with how CSS selector parsing returned a list, or called the slightly wrong algorithm for parsing URL patterns.

  • Simplified the processing model for the Speculation-Rules HTTP header. The previous text added various concepts around pending fetches and periodic implementation-defined processing of those fetches, all designed to allow the implementation to de-prioritize these fetches in favor of other things that happen during loading. This version instead explicitly allows an implementation-defined wait time, uses a separate task source which user agents can deprioritize, and explicitly excludes non-top-level-traversables from caring about the header.

  • Be explicit about when to consider speculative loads, instead of using vague language about "whenever something relevant changes".

  • Give an explicit algorithm for how speculative load candidates are grouped and redundant ones coalesced, instead of stating that it must be done somehow.

  • Add re-parsing of <script type=speculationrules> on base URL changes, to match the Chromium implementation.

  • Remove the re-parsing of <script type=speculationrules> on text content changes, to match the Chromium implementation and also match <script type=importmap>.

  • Slightly shorten some of the security and privacy considerations, e.g., by not mentioning mixed content concerns.

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


/acknowledgements.html ( diff )
/browsers.html ( diff )
/browsing-the-web.html ( diff )
/document-lifecycle.html ( diff )
/document-sequences.html ( diff )
/iana.html ( diff )
/index.html ( diff )
/indices.html ( diff )
/infrastructure.html ( diff )
/interaction.html ( diff )
/links.html ( diff )
/nav-history-apis.html ( diff )
/references.html ( diff )
/scripting.html ( diff )
/semantics.html ( diff )
/urls-and-fetching.html ( diff )
/webappapis.html ( diff )
/speculative-loading.html ( diff )

@domenic domenic added addition/proposal New features or enhancements topic: navigation labels Jul 3, 2025
@domenic domenic marked this pull request as draft July 3, 2025 06:59
@domenic domenic force-pushed the speculation-rules branch from 7ff9848 to c23dcae Compare July 9, 2025 06:51
@domenic domenic marked this pull request as ready for review July 9, 2025 06:54
@domenic domenic added the do not merge yet Pull request must not be merged per rationale in comment label Jul 9, 2025
@domenic domenic force-pushed the speculation-rules branch from c23dcae to 6ea2451 Compare July 9, 2025 06:58
@domenic
Copy link
Copy Markdown
Member Author

domenic commented Jul 9, 2025

The spec text here is done and ready for review!

Marking "do not merge yet" until I get the other prerequisites listed in the OP done (mostly PRs against other specs).

domenic added a commit to whatwg/fetch that referenced this pull request Jul 10, 2025
Part of whatwg/html#11426, for requests initiated by the `Speculation-Rules` HTTP header.
@domenic domenic force-pushed the speculation-rules branch 4 times, most recently from 33f0b13 to 647afa5 Compare July 11, 2025 02:42
domenic added a commit to WICG/nav-speculation that referenced this pull request Jul 11, 2025
whatwg/html#11426 upstreams most of the speculation rules specification to HTML. Remove the upstreamed portions, and add references to the PR and PR preview.
@domenic
Copy link
Copy Markdown
Member Author

domenic commented Jul 18, 2025

This is now fully ready for review, including all prerequisites. I'll keep "do not merge yet" to reflect that we need to get #11442 reviewed and merged first, but that's a pretty small change...

aarongable pushed a commit to chromium/chromium that referenced this pull request Jul 18, 2025
This CL introduces error handling for <script type=speculationrules>,
mirroring the behavior of <script type=importmap>.

Specifically, it adds error events for two cases:
- Inline speculation rules with unparsable JSON.
- External speculation rules, which are not yet supported.

This is verified by new web platform tests.

This follows the spec change at
whatwg/html#11426.

Change-Id: I9b0776b86059f6c8734d57c17f50ed26e89215da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6758353
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488632}
aarongable pushed a commit to chromium/chromium that referenced this pull request Jul 18, 2025
This CL introduces error handling for <script type=speculationrules>
with invalid ruleset-level tags. This changes the behavior: previously
we would ignore such tags and preload with the null tag. But now, we
error the entire <script type=speculationrules>, since it is better to
avoid preloading if the web developer is expecting it to get a certain
tag and we cannot fulfill that request.

This matches the spec update at
whatwg/html#11426.

Bug: None
Change-Id: I5d2211510bd4e3a3ecce15010325b411f18fde3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6746174
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Reviewed-by: Danil Somsikov <dsv@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488633}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 18, 2025
This CL introduces error handling for <script type=speculationrules>,
mirroring the behavior of <script type=importmap>.

Specifically, it adds error events for two cases:
- Inline speculation rules with unparsable JSON.
- External speculation rules, which are not yet supported.

This is verified by new web platform tests.

This follows the spec change at
whatwg/html#11426.

Change-Id: I9b0776b86059f6c8734d57c17f50ed26e89215da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6758353
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488632}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 18, 2025
This CL introduces error handling for <script type=speculationrules>
with invalid ruleset-level tags. This changes the behavior: previously
we would ignore such tags and preload with the null tag. But now, we
error the entire <script type=speculationrules>, since it is better to
avoid preloading if the web developer is expecting it to get a certain
tag and we cannot fulfill that request.

This matches the spec update at
whatwg/html#11426.

Bug: None
Change-Id: I5d2211510bd4e3a3ecce15010325b411f18fde3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6746174
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Reviewed-by: Danil Somsikov <dsv@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488633}
jonathan-j-lee pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 19, 2025
This CL introduces error handling for <script type=speculationrules>
with invalid ruleset-level tags. This changes the behavior: previously
we would ignore such tags and preload with the null tag. But now, we
error the entire <script type=speculationrules>, since it is better to
avoid preloading if the web developer is expecting it to get a certain
tag and we cannot fulfill that request.

This matches the spec update at
whatwg/html#11426.

Bug: None
Change-Id: I5d2211510bd4e3a3ecce15010325b411f18fde3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6746174
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Reviewed-by: Danil Somsikov <dsv@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488633}
jonathan-j-lee pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 19, 2025
This CL introduces error handling for <script type=speculationrules>,
mirroring the behavior of <script type=importmap>.

Specifically, it adds error events for two cases:
- Inline speculation rules with unparsable JSON.
- External speculation rules, which are not yet supported.

This is verified by new web platform tests.

This follows the spec change at
whatwg/html#11426.

Change-Id: I9b0776b86059f6c8734d57c17f50ed26e89215da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6758353
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488632}
jcscottiii pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 21, 2025
This CL introduces error handling for <script type=speculationrules>
with invalid ruleset-level tags. This changes the behavior: previously
we would ignore such tags and preload with the null tag. But now, we
error the entire <script type=speculationrules>, since it is better to
avoid preloading if the web developer is expecting it to get a certain
tag and we cannot fulfill that request.

This matches the spec update at
whatwg/html#11426.

Bug: None
Change-Id: I5d2211510bd4e3a3ecce15010325b411f18fde3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6746174
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Reviewed-by: Danil Somsikov <dsv@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488633}

Co-authored-by: Domenic Denicola <domenic@chromium.org>
jcscottiii pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 21, 2025
This CL introduces error handling for <script type=speculationrules>,
mirroring the behavior of <script type=importmap>.

Specifically, it adds error events for two cases:
- Inline speculation rules with unparsable JSON.
- External speculation rules, which are not yet supported.

This is verified by new web platform tests.

This follows the spec change at
whatwg/html#11426.

Change-Id: I9b0776b86059f6c8734d57c17f50ed26e89215da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6758353
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1488632}
Comment thread source Outdated
<p><span>Register speculation rules</span> given <var>global</var>,
<var>newResult</var>, and true.</p>

<p class="note" id="note-update-speculation-rules-queue-errors">When updating speculation rules, as opposed to registering them for the first time, we ensure that any <code data-x="event-error">error</code> events are queued as tasks, instead of synchronously fired. Although synchronously executing <code data-x="event-error">error</code> event handlers is OK when inserting <code>script</code> elements, it's best if other modifications do not cause such synchronous script execution.</p>
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.

Provided that the synchronous event is fired during post-connection steps, not just insertion steps! Not sure if that's worth a mention, probably not, but I figured I'd just beat the dead horse while I'm here.

@whatwg whatwg deleted a comment from diegomendez5214-max Sep 7, 2025
@domenic
Copy link
Copy Markdown
Member Author

domenic commented Sep 9, 2025

Alright, I think this is ready to merge! I'll give folks another 24 hours or so to comment before doing so, but am excited to get this merged (as well as the CSP and Fetch PRs). Then I'll move on to working on the prefetch spec port.

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

Labels

addition/proposal New features or enhancements topic: navigation

Development

Successfully merging this pull request may close these issues.

7 participants