Constructor

ExpressRoute

new ExpressRoute(input)

Constructor for ExpressRoute.

Parameter

input

Object

Values in input have the following properties:

Parameter

path

String

The path to use for routing. If the path contains named parameters, then an Object that maps parameter names to their corresponding value will be passed to the handler via 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'.

Extends
Route

Examples

// Any same-origin requests that start with /path/to and end with one
// additional path segment will match this route, with the last path
// segment passed along to the handler via params.file.
const route = new workbox.routing.ExpressRoute({
  path: '/path/to/:file',
  handler: ({event, params}) => {
    // params.file will be set based on the request URL that matched.
    return caches.match(params.file);
  },
});

const router = new workbox.routing.Router();
router.registerRoute({route});
// Any cross-origin requests for https://example.com will match this route.
const route = new workbox.routing.ExpressRoute({
  path: 'https://example.com/path/to/:file',
  handler: ({event}) => return caches.match(event.request),
});

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