MDN self-hosted library · e01.org

Request: isHistoryNavigation property

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
self.addEventListener("fetch", (event) => {
  // …

  if (event.request.isHistoryNavigation) {
    event.respondWith(
      (async () => {
        let response = await caches.match(event.request);
        if (response !== undefined) {
          return response;
        }
        response = await fetch(event.request);
        const responseClone = response.clone();

        event.waitUntil(
          caches
            .open("v1")
            .then((cache) => cache.put(event.request, responseClone)),
        );

        return response;
      })(),
    );
  }

  // …
});