NavigationPreloadManager

NavigationPreloadManager interface of the the Service Worker API provides methods for managing the preloading of resources with a service worker.

方法

NavigationPreloadManager.enable()
Enables navigation preloading and returns a Promise that resolves.
NavigationPreloadManager.disable()
Disables navigation preloading and returns a Promise that resolves.
NavigationPreloadManager.setHeaderValue()
Sets the value of the Service-Worker-Navigation-Preload header and returns an empty Promise .
NavigationPreloadManager.getState()
返回 Promise that resolves to an object with properties indicating whether preload is enabled and the contents of the Service-Worker-Navigation-Preload .

范例

Feature Detecting and Enabling Navigation Preloading

addEventListener('activate', event => {
  event.waitUntil(async function() {
    if (self.registration.navigationPreload) {
      // Enable navigation preloads!
      await self.registration.navigationPreload.enable();
    }
  }());
});
					

Using a Preloaded Response

The following example shows the implementation of a fetch event that uses a preloaded response.

addEventListener('fetch', event => {
  event.respondWith(async function() {
    // Respond from the cache if we can
    const cachedResponse = await caches.match(event.request);
    if (cachedResponse) return cachedResponse;
    // Else, use the preloaded response, if it's there
    const response = await event.preloadResponse;
    if (response) return response;
    // Else try the network.
    return fetch(event.request);
  }());
});
					

规范

规范 状态 Comment
服务工作者
The definition of 'NavigationPreloadManager' in that specification.
工作草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
NavigationPreloadManager
Chrome 62 Edge 18 Firefox No
不支持 No
Implementation tracked in bug 1290958
IE ? Opera 49 Safari ? WebView Android 62 Chrome Android 62 Firefox Android No
不支持 No
Implementation tracked in bug 1290958
Opera Android 46 Safari iOS ? Samsung Internet Android 8.0
disable
Chrome 62 Edge 18 Firefox No
不支持 No
Implementation tracked in bug 1290958
IE ? Opera 49 Safari ? WebView Android 62 Chrome Android 62 Firefox Android No
不支持 No
Implementation tracked in bug 1290958
Opera Android 46 Safari iOS ? Samsung Internet Android 8.0
enable
Chrome 62 Edge 18 Firefox No
不支持 No
Implementation tracked in bug 1290958
IE ? Opera 49 Safari ? WebView Android 62 Chrome Android 62 Firefox Android No
不支持 No
Implementation tracked in bug 1290958
Opera Android 46 Safari iOS ? Samsung Internet Android 8.0
getState
Chrome 62 Edge 18 Firefox No
不支持 No
Implementation tracked in bug 1290958
IE ? Opera 49 Safari ? WebView Android 62 Chrome Android 62 Firefox Android No
不支持 No
Implementation tracked in bug 1290958
Opera Android 46 Safari iOS ? Samsung Internet Android 8.0
setHeaderValue
Chrome 62 Edge 18 Firefox No
不支持 No
Implementation tracked in bug 1290958
IE ? Opera 49 Safari ? WebView Android 62 Chrome Android 62 Firefox Android No
不支持 No
Implementation tracked in bug 1290958
Opera Android 46 Safari iOS ? Samsung Internet Android 8.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。