workbox navigation preload code example

Example 1: navigation Preload example workbox

addEventListener("fetch", event =>
    event.request.mode === "navigate" &&
    new URL(event.request.url, location.origin).pathname !== "/test" &&
    event.respondWith(event.preloadResponse)
);

Example 2: navigation Preload example workbox

self.addEventListener('activate', (event) => {
  if (self.registration && self.registration.navigationPreload) {
    event.waitUntil(
      self.registration.navigationPreload.enable().then(() => {
        // Defaults to Service-Worker-Navigation-Preload: true if not set.
        if (headerValue) {
          self.registration.navigationPreload.setHeaderValue(headerValue);
        }
      })
    );
  }
});

Tags:

Misc Example