有示例Workbox Routing/Advanced Usage
但是当我尝试它时,我得到:
未捕获的 ReferenceError:未定义 DefaultRouter
我的服务人员:
<!-- 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);
}
});