Navigation Preload Prioritization in Chrome
0 reputation · 21 Jul 2023, 23:16 UTC
0 reputation · 21 Jul 2023, 23:16 UTC
Determine how Chrome’s Service Worker navigation preload feature resolves conflicts when both a preloaded response and a Service Worker‑generated response are available for the same navigation request.
Chrome implements navigation preload starting in version 63. The feature is disabled by default and must be enabled via self.registration.navigationPreload.enable() in the Service Worker’s activate event. When enabled, Chrome initiates a network fetch while the Service Worker boots and still dispatches a fetch event to the worker. The worker can return a custom response or let the preloaded response pass through.
Cache-Control or ETag influence the choice between the two responses?If-None-Match) when a preloaded response returns 304 Not Modified but the Service Worker returns a fresh body?33275 reputation · 22 Jul 2023, 04:11 UTC
Chrome chooses the Service Worker’s Response whenever the fetch event returns a concrete Response object. If the event handler returns undefined, null, or does not return a Response at all, Chrome falls back to the preloaded response that was fetched in parallel.
fetch event, it can either supply its own Response or let the preload win.Cache‑Control, ETag) on either response does not influence this choice; the decision is purely based on whether a Response was returned.304 Not Modified and the Service Worker returns no Response, Chrome serves the 304.activate event:
self.addEventListener('activate', event => {
event.waitUntil(self.registration.navigationPreload.enable());
});
fetch handler, sometimes return a custom Response and sometimes return undefined:
self.addEventListener('fetch', event => {
if (Math.random() < 0.5) {
event.respondWith(new Response('SW', {status: 200}));
} else {
// Let the preload win
}
});
Cache‑Control or ETag headers to both the preload and Service Worker responses and observe that the same rule applies.If‑None‑Match header from the Service Worker or from the preload and confirm that the 304 is used only when no Service Worker Response is supplied.Do you observe the Service Worker ever returning a non‑200 status code (e.g., 404 or 500) in the fetch handler? This can affect how the final navigation is rendered, but it does not change the rule that a Response object overrides the preload.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.