1

有示例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);
  }
});
4

2 回答 2

1

我也偶然发现了同样的问题。

看起来它DefaultRouter已被Router https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.routing.Router取代 并初始化你必须做的new workbox.routing.Router()

于 2019-10-31T19:36:58.947 回答
0

现在我发现只是看起来很有趣的绕过:而不是:

const router = new DefaultRouter();

我用:

router = new workbox.routing.constructor();
于 2018-05-11T16:31:31.203 回答