Reputation: 11
there is the example Workbox Routing/Advanced Usage
But when I tried it I get:
Uncaught ReferenceError: DefaultRouter is not defined
there my service-worker:
<!-- language: lang-js -->
importScripts("https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-sw.js");
workbox.skipWaiting();
workbox.clientsClaim();
workbox.setConfig({
debug: true
});
const router = new DefaultRouter();
router.registerRoute(new RegExpRoute( /\/index\.html/, workbox.strategies.networkFirst()));
router.registerRoute(new RegExpRoute( /main\.min\.js/, workbox.strategies.staleWhileRevalidate()));
self.addEventListener('fetch', (event) => {
const responsePromise = router.handleRequest(event);
if (responsePromise) {
// Router found a route to handle the request
event.respondWith(responsePromise);
} else {
// No route found to handle the request
console.debug('workbox has no route to handle request ', event.request);
}
});
Upvotes: 1
Views: 655
Reputation: 173
I also stumbled upon the same issue.
It looks like that DefaultRouter
has been replaced with Router
https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.routing.Router
and to initialize you have to do new workbox.routing.Router()
.
Upvotes: 1
Reputation: 11
now I find just funny looking bypass: instead:
const router = new DefaultRouter();
I use:
router = new workbox.routing.constructor();
Upvotes: 0