web.dev LIVE is now over! Head to web.dev/live to watch all the sessions, see top Q&A, and more.

Module: workbox-routing

Classes

Methods

registerRoute

static

registerRoute(capture, handler, method) returns module:workbox-routing.Route

Easily register a RegExp, string, or function with a caching strategy to a singleton Router instance.

This method will generate a Route for you if needed and call registerRoute().

Parameter

capture

(RegExp, string, module:workbox-routing.Route~matchCallback, or module:workbox-routing.Route)

If the capture param is a Route, all other arguments will be ignored.

handler

Optional

module:workbox-routing~handlerCallback

A callback function that returns a Promise resulting in a Response. This parameter is required if capture is not a Route object.

method

Optional

string

The HTTP method to match the Route against.

Returns

module:workbox-routing.Route The generated Route(Useful for unregistering).

setCatchHandler

static

setCatchHandler(handler)

If a Route throws an error while handling a request, this handler will be called and given a chance to provide a response.

Parameter

handler

module:workbox-routing~handlerCallback

A callback function that returns a Promise resulting in a Response.

setDefaultHandler

static

setDefaultHandler(handler)

Define a default handler that's called when no routes explicitly match the incoming request.

Without a default handler, unmatched requests will go against the network as if there were no service worker present.

Parameter

handler

module:workbox-routing~handlerCallback

A callback function that returns a Promise resulting in a Response.

Abstract types

handlerCallback

inner

handlerCallback(context) returns Promise containing Response

The "handler" callback is invoked whenever a Router matches a URL to a Route via its match callback. This callback should return a Promise that resolves with a Response.

If a non-empty array or object is returned by the match callback it will be passed in as the handler's context.params argument.

Parameter

context

Object

Values in context have the following properties:

Parameter

url

URL

The URL that matched.

request

Optional

Request

The corresponding request, if available.

event

Optional

FetchEvent

The corresponding event that triggered the request, if available.

params

Optional

Object

Array or Object parameters returned by the Route's match callback. This will be undefined if an empty array or object were returned.

Returns

Promise containing Response The response that will fulfill the request.

matchCallback

inner

matchCallback(context) returns any type

The "match" callback is used to determine if a Route should apply for a particular URL. When matching occurs in response to a fetch event from the client, the event and request objects are supplied in addition to the URL. However, since the match callback can be invoked outside of a fetch event, matching logic should not assume the event or request objects will always be available (unlike URL, which is always available). If the match callback returns a truthy value, the matching route's handler callback will be invoked immediately. If the value returned is a non-empty array or object, that value will be set on the handler's context.params argument.

Parameter

context

Object

Values in context have the following properties:

Parameter

url

URL

The request's URL.

request

Optional

Request

The corresponding request, if available.

event

Optional

FetchEvent

The corresponding event that triggered the request, if available.

Returns

any type To signify a match, return a truthy value.