Skip to main content

PrecacheFallbackPlugin

First added

Workbox

About

PrecacheFallbackPlugin allows you to specify offline fallbacks to be used when a given strategy is unable to generate a response.

It does this by intercepting the handlerDidError plugin callback and returning a precached response, taking the expected revision parameter into account automatically.

Generally, you don’t need to use this plugin directly. Ratherly, you’d use the fallbacks option of the Serwist class.

Options

  • fallbackUrls — Precached URLs to be used as the fallback if the associated strategy can’t generate a response.
  • serwist — Your Serwist instance.

Usage

import { NetworkOnly, PrecacheFallbackPlugin, Serwist } from "serwist";

const serwist = new Serwist({
  // Assuming that your precache list includes "/fallback.html".
  precacheEntries: self.__SW_MANIFEST,
});

serwist.registerCapture(
  /^\/admin\/.*\.html$/,
  new NetworkOnly({
    plugins: [
      new PrecacheFallbackPlugin({
        fallbackUrls: ["/fallback.html"],
        serwist,
      }),
    ],
  }),
);

serwist.addEventListeners();