Constructor

RegExpRoute

new RegExpRoute(input)

Constructor for RegExpRoute.

Parameter

input

Object

Values in input have the following properties:

Parameter

regExp

RegExp

The regular expression to match against URLs. If the RegExp contains capture groups, then the array of captured values will be passed to the handler as params.

handler

(function() or module:workbox-runtime-caching.Handler)

The handler to use to provide a response if the route matches.

If you wish to use a callback function see handlerCallback for the callback definition.

method

Optional

string

Only match requests that use this HTTP method. Defaults to 'GET' if not specified.

Extends
Route

Example

// Any requests that match the regular expression will match this route, with
// the capture groups passed along to the handler as an array via params.
const route = new workbox.routing.RegExpRoute({
  regExp: new RegExp('^https://example.com/path/to/(\\w+)'),
  handler: {
    handle: ({event, params}) => {
      // params[0], etc. will be set based on the regexp capture groups.
      // Do something that returns a Promise.<Response>, like:
      return caches.match(event.request);
    },
  },
});

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