大网数据广告活动
Web 标准与前端开发

FetchEvent: preloadResponse property

· 52CE 我爱测

The **`preloadResponse`** read-only property of the FetchEvent interface returns a Promise that resolves to the navigation preload Response if navigation preload was triggered, or `undefined` otherwise.

Service Workers APIservice

The preloadResponse read-only property of the FetchEvent interface returns a Promise that resolves to the navigation preload Response if navigation preload was triggered, or undefined otherwise.

Navigation preload is triggered if navigation preload is enabled, the request is a GET request, and the request is a navigation request (generated by the browser when loading pages and iframes).

A service worker can wait on this promise in its fetch event handler in order to track completion of a fetch request made during service-worker boot.

Value

A Promise that resolves to a Response or otherwise to undefined.

Examples

This code snippet is from Speed up Service Worker with Navigation Preloads.

The ServiceWorkerGlobalScope.fetch_event event handler listens for the fetch event. When fired, the handler calls FetchEvent.respondWith to pass a promise back to the controlled page. This promise will resolve with the requested resource.

If there is a matching URL request in the Cache object, then the code returns a promise for fetching the response from the cache. If no match is found in the cache, the code returns the promise in preloadResponse. If there is no matching cache or preloaded response, the code fetches the response from the network and returns the associated promise.

addEventListener("fetch", (event) => {
  event.respondWith(
    (async () => {
      const preloadResponsePromise = event.preloadResponse;

      // Respond from the cache if we can
      const cachedResponse = await caches.match(event.request);
      if (cachedResponse) {
        // Keep the navigation preload request alive even if we do not use its response.
        event.waitUntil(preloadResponsePromise.catch(() => undefined));
        return cachedResponse;
      }

      // Else, use the preloaded response, if it's there
      const response = await preloadResponsePromise;
      if (response) return response;

      // Else try the network.
      return fetch(event.request);
    })(),
  );
});

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。