Skip to content

Implement Early Hints preload - #70207

Open
yoavweiss wants to merge 1 commit into
WebKit:mainfrom
yoavweiss:early_hints_preload
Open

yoavweiss wants to merge 1 commit into
WebKit:mainfrom
yoavweiss:early_hints_preload

Conversation

@yoavweiss

@yoavweiss yoavweiss commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

00e84c2

Implement Early Hints preload
https://bugs.webkit.org/show_bug.cgi?id=320314

Reviewed by NOBODY (OOPS!).

Update the NetworkProcess to preload resources advertised with `Link: rel=preload`
in top-level navigation first HTTP 103 response.

High-level details:
* Preloaded resources are cached in a per-navigation cache, and served only when the
  consuming fetch matches on URL, CORS mode, credentials mode, cache partition and
  Vary-dependent request headers.
* The preloads also seed the network cache, keyed on the preload request.
* Consumed preloaded entries report an "early-hints" initiatorType to Resource Timing.
* Preloads that go through cross-origin redirects get dropped.
* Currently, scripts aren't loaded under CSP strict-dynamic. That's something we should
  fix in a followup.

The feature is behind the LinkPreloadEarlyHintsEnabled preference, testable and
off by default.

Imported WPT baselines are updated. Unsupported cases are skipped in
TestExpectations: modulepreload, the embed/object destination, and in-flight
consumption. Early Hints is Cocoa-only for now (soup receives 103s but doesn't
forward them), so the imported suite is skipped on glib.

Tests: http/wpt/loading/early-hints/preload-cross-site.h2.window.html
       http/wpt/loading/early-hints/preload-main-frame-only.h2.window.html
       http/wpt/loading/early-hints/preload-redirect.h2.window.html
       imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window.html

Spec: https://html.spec.whatwg.org/multipage/#process-early-hint-headers

* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml: Add the LinkPreloadEarlyHintsEnabled preference gating the feature.
* Source/WebCore/loader/LinkHeader.cpp:
(WebCore::parameterValueIsOptional): Added; identifies parameters (crossorigin) whose value may be omitted.
(WebCore::parseParameterName): Accept a valueless crossorigin rather than returning no name.
(WebCore::LinkHeader::LinkHeader): Don't invalidate the header when crossorigin has no value.
* Source/WebCore/loader/ResourceTimingInformation.cpp:
(WebCore::ResourceTimingInformation::addResourceTiming): Report an "early-hints" initiator type when the load came from an early hints preload.
* Source/WebCore/page/csp/ContentSecurityPolicy.h: Declare decisionForSupportedPreload.
* Source/WebCore/page/csp/ContentSecurityPolicy.cpp:
(WebCore::ContentSecurityPolicy::decisionForSupportedPreload const): Whether a 103 preload's `as` destination is allowed; nullopt for a destination we don't preload, false for a script we cannot clear because strict-dynamic defers the check.
* Source/WebCore/platform/ReferrerPolicy.cpp:
(WebCore::parseReferrerPolicy): Export so the NetworkProcess can parse a 103's Referrer-Policy.
* Source/WebCore/platform/ReferrerPolicy.h: Export parseReferrerPolicy.
* Source/WebCore/platform/network/NetworkLoadMetrics.cpp:
(WebCore::NetworkLoadMetrics::NetworkLoadMetrics): Take the new fromEarlyHints flag.
(WebCore::NetworkLoadMetrics::updateFromFinalMetrics): Preserve fromEarlyHints when merging interim and final metrics.
(WebCore::NetworkLoadMetrics::isolatedCopy const): Copy fromEarlyHints.
* Source/WebCore/platform/network/NetworkLoadMetrics.h:
(WebCore::NetworkLoadMetrics::isEarlyHints const): Accessor for the new flag.
* Source/WebKit/NetworkProcess/EarlyHintsPreloadCache.h: Added.
(WebKit::EarlyHintsPreloadCache::Entry::releaseBuffer): Take ownership of the parked body.
* Source/WebKit/NetworkProcess/EarlyHintsPreloadCache.cpp: Added.
(WebKit::EarlyHintsPreloadCache::Entry::Entry): Store the preload's fetch mode, cookie policy and varying request headers alongside the response.
(WebKit::EarlyHintsPreloadCache::EarlyHintsPreloadCache): Set up the expiration timer.
(WebKit::EarlyHintsPreloadCache::registerNavigation): Record the origin and cache partition that issued the 103, resetting the cache when either changes.
(WebKit::EarlyHintsPreloadCache::store): Park a completed preload, rejecting stores from a superseded navigation.
(WebKit::EarlyHintsPreloadCache::take): Return a parked preload whose URL, mode, cookie policy, cache partition and varying headers match the consumer.
(WebKit::EarlyHintsPreloadCache::pruneForFinalResponse): Drop parked preloads the final response's CSP disallows.
(WebKit::EarlyHintsPreloadCache::clear): Drop a frame's, or all, parked preloads.
(WebKit::EarlyHintsPreloadCache::clearExpiredEntries): Timer callback evicting navigations past their expiry.
(WebKit::EarlyHintsPreloadCache::addInFlightPreload):
(WebKit::EarlyHintsPreloadCache::settleInFlightPreload):
(WebKit::EarlyHintsPreloadCache::hasMatchingInFlightPreload const):
(WebKit::EarlyHintsPreloadCache::addInFlightPreloadWaiter):
(WebKit::EarlyHintsPreloadCache::settleAll):
* Source/WebKit/NetworkProcess/EarlyHintsPreloadTask.h: Added.
* Source/WebKit/NetworkProcess/EarlyHintsPreloadTask.cpp: Added.
(WebKit::EarlyHintsPreloadTask::create): Create a speculative load carrying its mode, cookie policy and hinting origin.
(WebKit::EarlyHintsPreloadTask::EarlyHintsPreloadTask): Ditto.
(WebKit::EarlyHintsPreloadTask::start): Start the network load and self-retain until it completes.
(WebKit::EarlyHintsPreloadTask::willSendRedirectedRequest): Follow a same-origin redirect; abandon the preload on a cross-origin one.
(WebKit::EarlyHintsPreloadTask::didReceiveResponse): Only park a successful (2xx) response.
(WebKit::EarlyHintsPreloadTask::didReceiveBuffer): Buffer the body, abandoning the preload past the size cap.
(WebKit::EarlyHintsPreloadTask::didFinishLoading): Seed the network cache from the preload's own request, then park the response for later consumption.
(WebKit::EarlyHintsPreloadTask::didFailLoading): Discard the preload on failure.
(WebKit::EarlyHintsPreloadTask::complete): Tear down the load and drop the self-reference.
* Source/WebKit/NetworkProcess/NetworkResourceLoader.cpp:
(WebKit::NetworkResourceLoader::startRequest): Consume a matching early hints preload before consulting the disk cache.
(WebKit::NetworkResourceLoader::retrieveEarlyHintsPreload): Added; serve a parked preload to a subresource, without requiring a network cache.
(WebKit::NetworkResourceLoader::didReceiveResponse): When the navigation received early hints, prune parked preloads against the final response's CSP.
(WebKit::NetworkResourceLoader::start): Clear the frame's preloads when a new main-frame navigation begins.
(WebKit::NetworkResourceLoader::willSendRedirectedRequestInternal): On a cross-origin main-frame redirect, discard preloads and restart early hints processing.
(WebKit::NetworkResourceLoader::sendResultForCacheEntry): Mark the finish metrics as early-hints-served.
(WebKit::NetworkResourceLoader::didReceiveInformationalResponse):
(WebKit::NetworkResourceLoader::constructPreconnectRequest):
(WebKit::NetworkResourceLoader::startPreconnectTask):
(WebKit::NetworkResourceLoader::startPreloadTask):
(WebKit::NetworkResourceLoader::handleEarlyHintsResponse):
* Source/WebKit/NetworkProcess/NetworkResourceLoader.h: Declare retrieveEarlyHintsPreload and track whether this load was served from the early hints preload cache.
* Source/WebKit/NetworkProcess/NetworkSession.cpp:
(WebKit::NetworkSession::NetworkSession): Own the early hints preload cache.
(WebKit::NetworkSession::earlyHintsPreloadCache): Checked accessor for the cache.
(WebKit::NetworkSession::lowMemoryHandler): Drop parked preloads under memory pressure.
* Source/WebKit/NetworkProcess/NetworkSession.h: Declare the early hints preload cache.
* Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in: Serialize the fromEarlyHints metrics flag.
* Source/WebKit/Sources.txt: Build the new files.
* Source/WebKit/WebKit.xcodeproj/project.pbxproj: Add the new files.
* LayoutTests/TestExpectations: Skip the early hints cases WebKit does not support.
* LayoutTests/platform/glib/TestExpectations: Skip the imported early hints suite; the feature is Cocoa-only.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/404-with-early-hints.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/coep-early-hints-require-corp-final-none.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-absent-final-absent.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-absent-final-allowed.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-allowed-final-absent.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-allowed-final-allowed.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/multiple-early-hints-responses.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-fetchpriority.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-finished-before-final-response.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-finished-while-receiving-final-response-body.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-initiator-type.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/redirect-cross-origin-between-early-hints.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/redirect-same-origin-between-early-hints.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/redirect-same-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-no-referrer.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-origin-when-cross-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-same-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-unsafe-url.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/preload-no-store.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/no-store.js: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/no-store.js.headers: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/w3c-import.log:
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/w3c-import.log:
* LayoutTests/http/wpt/loading/early-hints/preload-redirect.h2.window.js: Added; a preload redirected cross-origin is abandoned, so the document's own request loads the resource.
(promise_test.async t):
* LayoutTests/http/wpt/loading/early-hints/preload-redirect.h2.window.html: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-redirect.h2.window-expected.txt: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-main-frame-only.h2.window.js: Added; a 103 delivered on a child navigable navigation is ignored.
(promise_test.async t):
* LayoutTests/http/wpt/loading/early-hints/preload-main-frame-only.h2.window.html: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-main-frame-only.h2.window-expected.txt: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-cross-site.h2.window.js: Added; a preload parked during one site's navigation is not served to a fetch from another site.
(openWindow):
(messageFrom):
(messageFrom.return.new.Promise):
* LayoutTests/http/wpt/loading/early-hints/preload-cross-site.h2.window.html: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-cross-site.h2.window-expected.txt: Added.
* LayoutTests/http/wpt/loading/early-hints/resources/preload-loader.h2.py: Added; sends a 103 preload and gives it a head start before the final response.
(_preload_path):
(handle_headers):
(main):
* LayoutTests/http/wpt/loading/early-hints/resources/preload-test.html: Added; shared page that consumes the hinted resource and reports back to the runner.
(waitUntilFetched):
* LayoutTests/http/wpt/loading/early-hints/resources/status.py: Added; non-blocking check for whether a recording endpoint was fetched.
(_stash_dir):
(main):
* LayoutTests/http/wpt/loading/early-hints/resources/redirect.py: Added.
(_stash_dir):
(main):
* LayoutTests/http/wpt/loading/early-hints/resources/recorded-script.py: Added.
(_stash_dir):
(main):
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window.js: Added.
* LayoutTests/http/tests/ipc/ipc-fetch-task-message-crash.html:
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-in-flight-when-consumed.h2.window-expected.txt:

00e84c2

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ❌ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-debug ✅ 🧪 jsc-wpe
✅ 🧪 jsc-x86-64 ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🛠 🧪 jsc-debug-arm64 ✅ 🛠 ios-safer-cpp ✅ 🧪 mac-AS-debug-wk2 ✅ 🛠 gtk
✅ 🛠 vision ❌ 🧪 mac-wk2-stress ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@yoavweiss
yoavweiss requested a review from cdumez as a code owner July 26, 2026 09:56
@yoavweiss yoavweiss self-assigned this Jul 26, 2026
@yoavweiss yoavweiss added the New Bugs Unclassified bugs are placed in this component until the correct component can be determined. label Jul 26, 2026
@yoavweiss
yoavweiss force-pushed the early_hints_preload branch from 9fcf6d8 to 23bc79e Compare July 26, 2026 10:43
@yoavweiss
yoavweiss requested a review from achristensen07 July 26, 2026 10:44
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

iOS Safer C++ Build #42498 (9fcf6d8)

❌ Found 1 failing file with 2 issues. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jul 26, 2026
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

iOS Safer C++ Build #42500 (23bc79e)

❌ Found 1 failing file with 2 issues. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

macOS Safer C++ Build #123313 (23bc79e)

❌ Found 1 failing file with 2 issues. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@yoavweiss yoavweiss removed the merging-blocked Applied to prevent a change from being merged label Jul 26, 2026
@yoavweiss
yoavweiss force-pushed the early_hints_preload branch from 23bc79e to d1d5cba Compare July 26, 2026 13:04
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

iOS Safer C++ Build #42505 (d1d5cba)

❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jul 26, 2026
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

macOS Safer C++ Build #123318 (d1d5cba)

❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@yoavweiss yoavweiss removed the merging-blocked Applied to prevent a change from being merged label Jul 26, 2026
@yoavweiss
yoavweiss force-pushed the early_hints_preload branch from d1d5cba to e1257fd Compare July 26, 2026 14:18
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jul 26, 2026
@yoavweiss yoavweiss removed the merging-blocked Applied to prevent a change from being merged label Jul 27, 2026
@yoavweiss
yoavweiss force-pushed the early_hints_preload branch from e1257fd to c14efa0 Compare July 27, 2026 20:55
@yoavweiss
yoavweiss requested a review from achristensen07 July 27, 2026 20:56
@yoavweiss
yoavweiss force-pushed the early_hints_preload branch 2 times, most recently from 8fc1a32 to 0527aa3 Compare July 27, 2026 21:33
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Sep 11, 2026
@yoavweiss yoavweiss removed the merging-blocked Applied to prevent a change from being merged label Sep 13, 2026
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Sep 13, 2026
@yoavweiss yoavweiss removed the merging-blocked Applied to prevent a change from being merged label Sep 13, 2026
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Sep 13, 2026
@yoavweiss yoavweiss removed the merging-blocked Applied to prevent a change from being merged label Sep 15, 2026
@yoavweiss
yoavweiss requested a review from a team as a code owner September 15, 2026 05:54
https://bugs.webkit.org/show_bug.cgi?id=320314

Reviewed by NOBODY (OOPS!).

Update the NetworkProcess to preload resources advertised with `Link: rel=preload`
in top-level navigation first HTTP 103 response.

High-level details:
* Preloaded resources are cached in a per-navigation cache, and served only when the
  consuming fetch matches on URL, CORS mode, credentials mode, cache partition and
  Vary-dependent request headers.
* The preloads also seed the network cache, keyed on the preload request.
* Consumed preloaded entries report an "early-hints" initiatorType to Resource Timing.
* Preloads that go through cross-origin redirects get dropped.
* Currently, scripts aren't loaded under CSP strict-dynamic. That's something we should
  fix in a followup.

The feature is behind the LinkPreloadEarlyHintsEnabled preference, testable and
off by default.

Imported WPT baselines are updated. Unsupported cases are skipped in
TestExpectations: modulepreload, the embed/object destination, and in-flight
consumption. Early Hints is Cocoa-only for now (soup receives 103s but doesn't
forward them), so the imported suite is skipped on glib.

Tests: http/wpt/loading/early-hints/preload-cross-site.h2.window.html
       http/wpt/loading/early-hints/preload-main-frame-only.h2.window.html
       http/wpt/loading/early-hints/preload-redirect.h2.window.html
       imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window.html

Spec: https://html.spec.whatwg.org/multipage/#process-early-hint-headers

* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml: Add the LinkPreloadEarlyHintsEnabled preference gating the feature.
* Source/WebCore/loader/LinkHeader.cpp:
(WebCore::parameterValueIsOptional): Added; identifies parameters (crossorigin) whose value may be omitted.
(WebCore::parseParameterName): Accept a valueless crossorigin rather than returning no name.
(WebCore::LinkHeader::LinkHeader): Don't invalidate the header when crossorigin has no value.
* Source/WebCore/loader/ResourceTimingInformation.cpp:
(WebCore::ResourceTimingInformation::addResourceTiming): Report an "early-hints" initiator type when the load came from an early hints preload.
* Source/WebCore/page/csp/ContentSecurityPolicy.h: Declare decisionForSupportedPreload.
* Source/WebCore/page/csp/ContentSecurityPolicy.cpp:
(WebCore::ContentSecurityPolicy::decisionForSupportedPreload const): Whether a 103 preload's `as` destination is allowed; nullopt for a destination we don't preload, false for a script we cannot clear because strict-dynamic defers the check.
* Source/WebCore/platform/ReferrerPolicy.cpp:
(WebCore::parseReferrerPolicy): Export so the NetworkProcess can parse a 103's Referrer-Policy.
* Source/WebCore/platform/ReferrerPolicy.h: Export parseReferrerPolicy.
* Source/WebCore/platform/network/NetworkLoadMetrics.cpp:
(WebCore::NetworkLoadMetrics::NetworkLoadMetrics): Take the new fromEarlyHints flag.
(WebCore::NetworkLoadMetrics::updateFromFinalMetrics): Preserve fromEarlyHints when merging interim and final metrics.
(WebCore::NetworkLoadMetrics::isolatedCopy const): Copy fromEarlyHints.
* Source/WebCore/platform/network/NetworkLoadMetrics.h:
(WebCore::NetworkLoadMetrics::isEarlyHints const): Accessor for the new flag.
* Source/WebKit/NetworkProcess/EarlyHintsPreloadCache.h: Added.
(WebKit::EarlyHintsPreloadCache::Entry::releaseBuffer): Take ownership of the parked body.
* Source/WebKit/NetworkProcess/EarlyHintsPreloadCache.cpp: Added.
(WebKit::EarlyHintsPreloadCache::Entry::Entry): Store the preload's fetch mode, cookie policy and varying request headers alongside the response.
(WebKit::EarlyHintsPreloadCache::EarlyHintsPreloadCache): Set up the expiration timer.
(WebKit::EarlyHintsPreloadCache::registerNavigation): Record the origin and cache partition that issued the 103, resetting the cache when either changes.
(WebKit::EarlyHintsPreloadCache::store): Park a completed preload, rejecting stores from a superseded navigation.
(WebKit::EarlyHintsPreloadCache::take): Return a parked preload whose URL, mode, cookie policy, cache partition and varying headers match the consumer.
(WebKit::EarlyHintsPreloadCache::pruneForFinalResponse): Drop parked preloads the final response's CSP disallows.
(WebKit::EarlyHintsPreloadCache::clear): Drop a frame's, or all, parked preloads.
(WebKit::EarlyHintsPreloadCache::clearExpiredEntries): Timer callback evicting navigations past their expiry.
(WebKit::EarlyHintsPreloadCache::addInFlightPreload):
(WebKit::EarlyHintsPreloadCache::settleInFlightPreload):
(WebKit::EarlyHintsPreloadCache::hasMatchingInFlightPreload const):
(WebKit::EarlyHintsPreloadCache::addInFlightPreloadWaiter):
(WebKit::EarlyHintsPreloadCache::settleAll):
* Source/WebKit/NetworkProcess/EarlyHintsPreloadTask.h: Added.
* Source/WebKit/NetworkProcess/EarlyHintsPreloadTask.cpp: Added.
(WebKit::EarlyHintsPreloadTask::create): Create a speculative load carrying its mode, cookie policy and hinting origin.
(WebKit::EarlyHintsPreloadTask::EarlyHintsPreloadTask): Ditto.
(WebKit::EarlyHintsPreloadTask::start): Start the network load and self-retain until it completes.
(WebKit::EarlyHintsPreloadTask::willSendRedirectedRequest): Follow a same-origin redirect; abandon the preload on a cross-origin one.
(WebKit::EarlyHintsPreloadTask::didReceiveResponse): Only park a successful (2xx) response.
(WebKit::EarlyHintsPreloadTask::didReceiveBuffer): Buffer the body, abandoning the preload past the size cap.
(WebKit::EarlyHintsPreloadTask::didFinishLoading): Seed the network cache from the preload's own request, then park the response for later consumption.
(WebKit::EarlyHintsPreloadTask::didFailLoading): Discard the preload on failure.
(WebKit::EarlyHintsPreloadTask::complete): Tear down the load and drop the self-reference.
* Source/WebKit/NetworkProcess/NetworkResourceLoader.cpp:
(WebKit::NetworkResourceLoader::startRequest): Consume a matching early hints preload before consulting the disk cache.
(WebKit::NetworkResourceLoader::retrieveEarlyHintsPreload): Added; serve a parked preload to a subresource, without requiring a network cache.
(WebKit::NetworkResourceLoader::didReceiveResponse): When the navigation received early hints, prune parked preloads against the final response's CSP.
(WebKit::NetworkResourceLoader::start): Clear the frame's preloads when a new main-frame navigation begins.
(WebKit::NetworkResourceLoader::willSendRedirectedRequestInternal): On a cross-origin main-frame redirect, discard preloads and restart early hints processing.
(WebKit::NetworkResourceLoader::sendResultForCacheEntry): Mark the finish metrics as early-hints-served.
(WebKit::NetworkResourceLoader::didReceiveInformationalResponse):
(WebKit::NetworkResourceLoader::constructPreconnectRequest):
(WebKit::NetworkResourceLoader::startPreconnectTask):
(WebKit::NetworkResourceLoader::startPreloadTask):
(WebKit::NetworkResourceLoader::handleEarlyHintsResponse):
* Source/WebKit/NetworkProcess/NetworkResourceLoader.h: Declare retrieveEarlyHintsPreload and track whether this load was served from the early hints preload cache.
* Source/WebKit/NetworkProcess/NetworkSession.cpp:
(WebKit::NetworkSession::NetworkSession): Own the early hints preload cache.
(WebKit::NetworkSession::earlyHintsPreloadCache): Checked accessor for the cache.
(WebKit::NetworkSession::lowMemoryHandler): Drop parked preloads under memory pressure.
* Source/WebKit/NetworkProcess/NetworkSession.h: Declare the early hints preload cache.
* Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in: Serialize the fromEarlyHints metrics flag.
* Source/WebKit/Sources.txt: Build the new files.
* Source/WebKit/WebKit.xcodeproj/project.pbxproj: Add the new files.
* LayoutTests/TestExpectations: Skip the early hints cases WebKit does not support.
* LayoutTests/platform/glib/TestExpectations: Skip the imported early hints suite; the feature is Cocoa-only.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/404-with-early-hints.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/coep-early-hints-require-corp-final-none.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-absent-final-absent.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-absent-final-allowed.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-allowed-final-absent.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/csp-early-hints-allowed-final-allowed.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/multiple-early-hints-responses.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-fetchpriority.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-finished-before-final-response.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-finished-while-receiving-final-response-body.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-initiator-type.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/redirect-cross-origin-between-early-hints.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/redirect-same-origin-between-early-hints.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/redirect-same-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-no-referrer.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-origin-when-cross-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-same-origin.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/referrer-policy-unsafe-url.h2.window-expected.txt: Rebaselined for Early Hints preload support.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window-expected.txt: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/preload-no-store.html: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/no-store.js: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/no-store.js.headers: Added.
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/w3c-import.log:
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/resources/w3c-import.log:
* LayoutTests/http/wpt/loading/early-hints/preload-redirect.h2.window.js: Added; a preload redirected cross-origin is abandoned, so the document's own request loads the resource.
(promise_test.async t):
* LayoutTests/http/wpt/loading/early-hints/preload-redirect.h2.window.html: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-redirect.h2.window-expected.txt: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-main-frame-only.h2.window.js: Added; a 103 delivered on a child navigable navigation is ignored.
(promise_test.async t):
* LayoutTests/http/wpt/loading/early-hints/preload-main-frame-only.h2.window.html: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-main-frame-only.h2.window-expected.txt: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-cross-site.h2.window.js: Added; a preload parked during one site's navigation is not served to a fetch from another site.
(openWindow):
(messageFrom):
(messageFrom.return.new.Promise):
* LayoutTests/http/wpt/loading/early-hints/preload-cross-site.h2.window.html: Added.
* LayoutTests/http/wpt/loading/early-hints/preload-cross-site.h2.window-expected.txt: Added.
* LayoutTests/http/wpt/loading/early-hints/resources/preload-loader.h2.py: Added; sends a 103 preload and gives it a head start before the final response.
(_preload_path):
(handle_headers):
(main):
* LayoutTests/http/wpt/loading/early-hints/resources/preload-test.html: Added; shared page that consumes the hinted resource and reports back to the runner.
(waitUntilFetched):
* LayoutTests/http/wpt/loading/early-hints/resources/status.py: Added; non-blocking check for whether a recording endpoint was fetched.
(_stash_dir):
(main):
* LayoutTests/http/wpt/loading/early-hints/resources/redirect.py: Added.
(_stash_dir):
(main):
* LayoutTests/http/wpt/loading/early-hints/resources/recorded-script.py: Added.
(_stash_dir):
(main):
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-no-store.h2.window.js: Added.
* LayoutTests/http/tests/ipc/ipc-fetch-task-message-crash.html:
* LayoutTests/imported/w3c/web-platform-tests/loading/early-hints/preload-in-flight-when-consumed.h2.window-expected.txt:
@bnham

bnham commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

I implemented 103 rel=preconnect support a few years ago. The reason why I didn't do rel=preload as well is because WebProcess is involved in constructing ResourceRequests without a particularly clean abstraction layer, and proving that the early request sent out by the NetworkProcess would be identical to the one that WebProcess would send out without early hints support seemed hard to do. It seemed like we'd need to do a lot of refactoring to how fetches are implemented in WebProcess to do this in a robust manner.

This is a pretty big patch so it may take a while to understand exactly how this works but let's also tag some other folks here. cc @achristensen07 @youennf

@bnham

bnham commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Thanks for looking in to this. We want to eventually support 103 preloads but I think it requires a good amount of work, and we haven't had a chance to work on it recently.

Regarding this patch, here are some initial thoughts from a quick look at it. First, the priority here has to make sure the optimization is correct and doesn't actually regress performance (e.g. by causing us to make duplicate requests). For instance:

  1. If the preloaded resource is already in disk cache, then we have to use the disk cached resource.
  2. We need to put the preloaded resource in the correct disk cache partition.
  3. If a preloaded request is in flight, then a subsequent request for that resource by the webpage should block on the preloaded request rather than issuing another request.
  4. Probably trickiest: we need to make sure that the preload request we build in NetworkProcess is the same as (or as close to the same as in the ways that matter) the resource request we would have built in WebProcess if this feature was off.

For (1): Unless I'm missing something, I don't think this patch consults the disk cache before sending out the preload request, which is a blocker.

For (2): I suspect something may be wrong here but I haven't fully worked out--the part where retrieveEarlyHintsPreload constructs a cache key without a salt does not seem like something we do anywhere. As a related comment it seems like a non-priority to make this work for ephemeral sessions.

For (3), it seems like some of this machinery is built out, but there are some cases where we can end up with a duplicate request. For instance, it looks like the 103 preload can send out a request for a subresource and then soon after NetworkCache::SpeculativeLoadManager could end up speculatively loading the same subresource.

(4) is what concerns me the most. Right now, the ResourceRequest for subresources is built out in WebProcess (mainly in CachedResourceLoader::requestResource but other paths also modify the resource request). If the NetworkLoadParameters of the request built out by the 103 preload doesn't exactly match the request that the NetworkLoadParameters that the WebProcess would have built for that subresource with early hints off, then we can run in to further duplicate request issues or correctness issues.

Here are some mismatches that I can see that could have bad consequences:

  • Certain HTTP headers that aren't set by EarlyPreloadTask (e.g. WebProcess will set a custom Accept header for certain media types)
  • sameSiteDisposition is not set which can affect SameSite/CrossSite cookie logic
  • hadMainFrameMainResourcePrivateRelayed needs to be conditionally set (this feeds in to whether private relay is allowed to fail open or closed)
  • networkActivityTracker is never set so the load won't get accounting in the Settings app
  • mayBlockNetworkRequest is never set so script tracking privacy protections can never kick in for this request
  • And so on, unfortunately I think we'd have to basically audit all the NetworkLoadParameters fields to get a full understanding

Other issues that I can see:

  • It seems like these requests don't go through content blockers (needs to pass through NetworkLoadChecker::processContentRuleListsForLoad)
  • It seems like these requests don't go through service worker

So stepping back, I think the cleanest way to implement this might be something like:

  1. Factor out the logic used to build NetworkLoadParameters in WebProcess for these early-loadable resource types like script, images, etc.
  2. Have NetworkProcess call that same code to construct the load parameters for early hint preloads.
  3. Instead of all the custom logic in e.g. EarlyHintsPreloadCache, try to integrate the early hints preload with the existing SpeculativeLoadManager. That already does tracking of in-flight requests, making sure duplicate requests aren't sent out, saving the request to the right cache partition, already sits in the load path with service workers, etc. (One note here is that the matching condition that SpeculativeLoadManager uses is exact header equality--this would probably have to be somewhat relaxed to support early hints preload).

It would also be good to get input from @achristensen07 and @youennf since they have dealt with the network stack more than me.

@yoavweiss

Copy link
Copy Markdown
Contributor Author

Thank you so much for the feedback!

I 100% agree that the goal here is to improve performance, not regress it. The cache bypass is a bug, & putting the resource in the right partition and matching in-flight requests should just work.

I agree the first step in breaking this PR up to something that can work is to refactor the request creation from WebProcess into something that can be called from Network process. I'll get on that and send for y'all to review.

A couple of points worthwhile getting agreement on:

  1. You wrote above that ephemeral session support is a non-priority. I think it'd be a shame if Private Browsing users will have slower experience than regular users. I also suspect that could be a way to distinguish them from non-private users. (e.g. through timing of loading a resource that should've been Early-Hints preloaded).
  2. I'm not sure if/how NetworkProcess requests can work with Service Workers, given that they are requested before the Document exists. What we can do is not send EH requests that are a potential SW match. But I wanted to make sure I'm not missing anything and that we're aligned on that point. /cc @youennf

@yoavweiss

Copy link
Copy Markdown
Contributor Author

The first refactoring bits are at #74672

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

Labels

New Bugs Unclassified bugs are placed in this component until the correct component can be determined.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants