workbox.routing. Router
The Router can be used to process a FetchEvent through one or more Routes responding with a Request if a matching route exists.
If no route matches a given a request, the Router will use a "default" handler if one is defined.
Should the matching Route throw an error, the Router will use a "catch" handler if one is defined to gracefully deal with issues and respond with a Request.
If a request matches multiple routes, the earliest registered route will be used to respond to the request.
Constructor
Router
new Router()
Initializes a new Router.
Methods
handleRequest
handleRequest(event) returns (Promise containing Response or undefined)
Apply the routing rules to a FetchEvent object to get a Response from an appropriate Route's handler.
Parameter |
|
---|---|
event |
FetchEvent The event from a service worker's 'fetch' event listener. |
- Returns
-
(Promise containing Response or undefined)
A promise is returned if a registered route can handle the FetchEvent's request. If there is no matching route and there's nodefaultHandler
,undefined
is returned.
registerRoute
registerRoute(route)
Registers a route with the router.
Parameter |
|
---|---|
route |
The route to register. |
setCatchHandler
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 |
workbox.routing.Route~handlerCallback A callback function that returns a Promise resulting in a Response. |
setDefaultHandler
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 |
workbox.routing.Route~handlerCallback A callback function that returns a Promise resulting in a Response. |
unregisterRoute
unregisterRoute(route)
Unregisters a route with the router.
Parameter |
|
---|---|
route |
The route to unregister. |