Skip to content

Fix navigation preload cache hit examples#44138

Merged
pepelsbey merged 2 commits into
mdn:mainfrom
Sean-Kenneth-Doherty:codex/fix-navigation-preload-cache-hit
May 29, 2026
Merged

Fix navigation preload cache hit examples#44138
pepelsbey merged 2 commits into
mdn:mainfrom
Sean-Kenneth-Doherty:codex/fix-navigation-preload-cache-hit

Conversation

@Sean-Kenneth-Doherty
Copy link
Copy Markdown
Contributor

@Sean-Kenneth-Doherty Sean-Kenneth-Doherty commented May 17, 2026

Description

Updates the navigation preload examples so they keep the preloaded response promise alive when a cache match wins. The examples now capture a normalized preloadResponse promise before checking the cache, pass it through the cache-first helper, and use event.waitUntil() on cache hits so the browser-started preload request can settle cleanly.

Motivation

When navigation preload is enabled and the cache wins, the samples returned before the preload promise settled. In Chrome this can produce the warning that the navigation preload request was cancelled before preloadResponse settled, and that waitUntil() or respondWith() should be used to wait for it.

Keeping the promise alive makes the examples safer for readers who copy them into real service workers, while Promise.resolve() keeps the samples tolerant if preloadResponse is unavailable or resolves to undefined.

Additional details

Reference material:

Validation run locally on bd97d19:

  • git diff --check origin/main...HEAD
  • ./node_modules/.bin/prettier --check files/en-us/web/api/fetchevent/preloadresponse/index.md files/en-us/web/api/service_worker_api/using_service_workers/index.md
  • ./node_modules/.bin/markdownlint-cli2 files/en-us/web/api/fetchevent/preloadresponse/index.md files/en-us/web/api/service_worker_api/using_service_workers/index.md
  • MDN pre-commit checks during amend: URL issues, changed xrefs, Prettier, markdownlint, front matter

Related issues and pull requests

Fixes #43055

@github-actions github-actions Bot added Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels May 17, 2026
@Sean-Kenneth-Doherty Sean-Kenneth-Doherty marked this pull request as ready for review May 17, 2026 05:53
@Sean-Kenneth-Doherty Sean-Kenneth-Doherty requested a review from a team as a code owner May 17, 2026 05:53
@Sean-Kenneth-Doherty Sean-Kenneth-Doherty requested review from pepelsbey and removed request for a team May 17, 2026 05:53
@Sean-Kenneth-Doherty

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 17, 2026

Preview URLs (2 pages)

(comment last updated: 2026-05-28 10:58:37)

@Sean-Kenneth-Doherty Sean-Kenneth-Doherty force-pushed the codex/fix-navigation-preload-cache-hit branch from 6d8e326 to bd97d19 Compare May 17, 2026 13:08
Copy link
Copy Markdown
Member

@pepelsbey pepelsbey left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you 🙂

Though, I believe these Promise wrappers are not needed, as event.preloadResponse already a Promise.

addEventListener("fetch", (event) => {
event.respondWith(
(async () => {
const preloadResponsePromise = Promise.resolve(event.preloadResponse);
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.

Suggested change
const preloadResponsePromise = Promise.resolve(event.preloadResponse);
const preloadResponsePromise = event.preloadResponse;

cacheFirst({
request: event.request,
preloadResponsePromise: event.preloadResponse,
preloadResponsePromise: Promise.resolve(event.preloadResponse),
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.

Suggested change
preloadResponsePromise: Promise.resolve(event.preloadResponse),
preloadResponsePromise: event.preloadResponse,

@Sean-Kenneth-Doherty
Copy link
Copy Markdown
Contributor Author

Thanks for the review. I removed the unnecessary Promise.resolve wrappers in 3dfabd2 and now pass/use event.preloadResponse directly while keeping the cache-hit waitUntil handling.

Validation:

  • npm ci
  • npx prettier -c files/en-us/web/api/service_worker_api/using_service_workers/index.md files/en-us/web/api/fetchevent/preloadresponse/index.md
  • npx markdownlint-cli2 files/en-us/web/api/service_worker_api/using_service_workers/index.md files/en-us/web/api/fetchevent/preloadresponse/index.md
  • npm run filecheck -- files/en-us/web/api/service_worker_api/using_service_workers/index.md files/en-us/web/api/fetchevent/preloadresponse/index.md
  • git diff --check

Copy link
Copy Markdown
Member

@pepelsbey pepelsbey left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you 🙂

@pepelsbey pepelsbey merged commit d81234a into mdn:main May 29, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Service Worker navigation preload sample code error

3 participants