workbox-sw. Router
Adds a friendly API on top of the router from the workbox-routing module.
Constructor
Router
new Router(revisionedCacheName, handleFetch)
Constructs a light wrapper on top of the underlying Router
.
Parameter |
|
---|---|
revisionedCacheName |
String The cache name used for entries cached via precache(). |
handleFetch |
boolean Determines if the router should handle fetch events. |
Examples
<caption>How to define a simple route with caching
strategy.</caption>
const workboxSW = new WorkboxSW();
workboxSW.router.registerRoute('/about',
workboxSW.strategies.cacheFirst());
<caption>How to define a simple route with custom caching
strategy.</caption>
const workboxSW = new WorkboxSW();
workboxSW.router.registerRoute('/about', (args) => {
// The requested URL
console.log(args.url);
// The FetchEvent to handle
console.log(args.event);
// The parameters from the matching route (Commonly
// used with Regex / Express routes).
console.log(args.params);
// Return a promise that resolves with a Response.
return fetch(args.url);
}));
Methods
registerNavigationRoute
registerNavigationRoute(url, options)
A shortcut used to register a
NavigationRoute instance that will respond to navigation requests using a cache entry for url
.
This is useful when following the App Shell pattern, in which the previously cached shell is returned for all navigations.
The url
value should correspond to an entry that's already in the cache, perhaps a URL that is managed by
precache(). Using a URL that isn't already cached will lead to failed navigations.
Parameter |
|||||||||
---|---|---|---|---|---|---|---|---|---|
url |
String The URL of the already cached HTML resource. |
||||||||
options |
Optional Object Values in
|
registerRoute
registerRoute(capture, handler, method) returns module:workbox-routing.Route
Parameter |
|
---|---|
capture |
(String, RegExp, or module:workbox-routing.matchCallback) The capture for a route can be one of three types:
|
handler |
(function() or module:workbox-runtime-caching.Handler) The handler to use to provide a response if the route matches. The handler argument is ignored if you pass in a Route object, otherwise it's required. |
method |
Optional String Only match requests that use this HTTP method.
|
- Returns
-
module:workbox-routing.Route
The Route object that was registered.