mdn/content

Service Worker navigation preload sample code error

已關閉

#43,055 建立於 2026年2月8日

 (0 則留言) (0 個反應) (0 位負責人)Markdown (23,221 個分叉)batch import
Content:WebAPIhelp wanted

倉庫指標

星標
 (10,947 顆星)
PR 合併指標
 (平均合併 7天 3小時) (30 天內合併 147 個 PR)

描述

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers

What specific section or headline is this issue about?

Service Worker navigation preload: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#service_worker_navigation_preload

What information was incorrect, unhelpful, or incomplete?

The code...

const responseFromCache = await caches.match(request);
  if (responseFromCache) {
    return responseFromCache;
  }
const preloadResponse = await preloadResponsePromise;
  if (preloadResponse) {
    console.info("using preload response", preloadResponse);
    event.waitUntil(putInCache(request, preloadResponse.clone()));
    return preloadResponse;
  }

... causes chrome console to display the following error: "The service worker navigation preload request was cancelled before 'preloadResponse' settled. If you intend to use 'preloadResponse', use waitUntil() or respondWith() to wait for the promise to settle."

This can be corrected by changing the first block...

if (responseFromCache) {
  // Wait for preload to settle to avoid cancellation warning
  preloadResponsePromise.catch(() => {}); // Ignore errors
  return responseFromCache;
}

What did you expect to see?

N/A

Do you have any supporting links, references, or citations?

Anthropic Claude, given the code and the error, provided the solution.

Do you have anything more you want to share?

No response

MDN metadata

貢獻者指南