NavigationPreloadManager
The NavigationPreloadManager interface of the Service Worker API provides methods for managing the preloading of resources with a service worker.
Methods
NavigationPreloadManager.enable()-
Enables navigation preloading and returns a
Promisethat resolves. NavigationPreloadManager.disable()-
Disables navigation preloading and returns a
Promisethat resolves. NavigationPreloadManager.setHeaderValue()-
Sets the value of the
Service-Worker-Navigation-Preloadheader and returns an emptyPromise. NavigationPreloadManager.getState()-
Returns a
Promisethat resolves to an object with properties indicating whether preload is enabled and the contents of theService-Worker-Navigation-Preload.
Examples
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); }()); });
Specifications
| Specification |
|---|
| Service Workers 1 # navigation-preload-manager |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
NavigationPreloadManager |
62 |
18 |
No
Implementation tracked in bug 1290958.
|
No |
49 |
No |
62 |
62 |
No
Implementation tracked in bug 1290958.
|
46 |
No |
8.0 |
disable |
62 |
18 |
No
Implementation tracked in bug 1290958.
|
No |
49 |
No |
62 |
62 |
No
Implementation tracked in bug 1290958.
|
46 |
No |
8.0 |
enable |
62 |
18 |
No
Implementation tracked in bug 1290958.
|
No |
49 |
No |
62 |
62 |
No
Implementation tracked in bug 1290958.
|
46 |
No |
8.0 |
getState |
62 |
18 |
No
Implementation tracked in bug 1290958.
|
No |
49 |
No |
62 |
62 |
No
Implementation tracked in bug 1290958.
|
46 |
No |
8.0 |
setHeaderValue |
62 |
18 |
No
Implementation tracked in bug 1290958.
|
No |
49 |
No |
62 |
62 |
No
Implementation tracked in bug 1290958.
|
46 |
No |
8.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/NavigationPreloadManager