Constructor

NetworkFirst

new NetworkFirst(input)

Constructor for a new NetworkFirst instance.

Parameter

input

Object

Values in input have the following properties:

Parameter

networkTimeoutSeconds

Optional

number

If set, and a valid network response isn't returned, then the cached response will be returned instead. If there is no previously cached response, then an null response will be returned. This option is meant to combat "lie-fi" scenarios.

requestWrapper

Optional

RequestWrapper

An optional RequestWrapper that is used to configure the cache name and request plugins. If not provided, a new RequestWrapper using the default cache name will be used.

Extends
module:workbox-runtime-caching.Handler

Example

// Set up a route to match any requests made for URLs that end in .txt.
// The requests are handled with a network-first strategy.
const route = new workbox.routing.RegExpRoute({
  regExp: /\.txt$/,
  handler: new workbox.runtimeCaching.NetworkFirst(),
});

const router = new workbox.routing.Router();
router.registerRoute({route});

Method

handle

async

handle(input) returns Promise containing Response

The handle method will be called by the Route class when a route matches a request.

Parameter

input

Object

Values in input have the following properties:

Parameter

event

FetchEvent

The event that triggered the service worker's fetch handler.

Returns

Promise containing Response The response from the network, or if that's not available, a previously cached response.