Skip to content

Instantly share code, notes, and snippets.

@wanderview
Last active January 29, 2021 08:16
Show Gist options
  • Save wanderview/f8ee22bf4b2e2bb2b2076f86f886b153 to your computer and use it in GitHub Desktop.
Save wanderview/f8ee22bf4b2e2bb2b2076f86f886b153 to your computer and use it in GitHub Desktop.
Example "no-op" navigation preload service worker
self.addEventListener('fetch', evt => {
// Fallback for subresource requests or in browsers that do not
// support navigation preload.
if (evt.request.mode !== "navigate" || !evt.preloadResponse)
return;
evt.respondWith(async _ => {
// Try to get the navigation preload response.
let r = await evt.preloadResponse;
// Navigation preload worked, so we are done.
if (r) return r;
// Otherwise we got an unexpected error, so switch to a pass-through
// fetch.
return fetch(evt.request);
}());
});
@tomayac
Copy link

tomayac commented Jan 14, 2021

s/self.addEventListener(evt => {/self.addEventListener('fetch', evt => {

@wanderview
Copy link
Author

Fixed. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment