@work-bee/fallback

@work-bee/fallback

Middleware that provides a fallback response when the primary strategy fails.

Install

npm install @work-bee/fallback

Usage

import {
  compileConfig,
  strategyNetworkOnly,
  strategyCacheOnly,
} from "@work-bee/core";
import fallbackMiddleware from "@work-bee/fallback";

const fallback = fallbackMiddleware({
  path: "/offline.html",
  statusCodes: [503, 504],
});

const config = compileConfig({
  precache: {
    routes: [{ path: "/offline.html" }],
    strategy: strategyCacheOnly,
  },
  strategy: strategyNetworkOnly,
  middlewares: [fallback],
});

Options

OptionTypeDefaultDescription
pathstringPath to the fallback resource (should be pre-cached)
pathPatternRegExpPattern to match for fallback (alternative to path)
statusCodesnumber[]Error responsesHTTP status codes that trigger the fallback
fallbackStrategyStrategystrategyCacheFirstStrategy used to fetch the fallback resource

Returns

{ after }