Chrome Dev Summit is back! Visit goo.gle/cds2021 to secure your spot in workshops, office hours and learning lounges!

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

request

(Request or string)

The corresponding request.

url

URL

The URL that matched, if available.

event

ExtendableEvent

The corresponding event that triggered the request.

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 object is supplied in addition to the url, request, and sameOrigin value. However, since the match callback can be invoked outside of a fetch event, matching logic should not assume the event object will always be 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

request

Request

The corresponding request.

url

URL

The request's URL.

event

ExtendableEvent

The corresponding event that triggered the request.

sameOrigin

boolean

The result of comparing url.origin against the current origin.

Returns

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