如何导入工作箱defaultRouter

时间:2018-05-11 16:27:54

标签: service-worker progressive-web-apps workbox

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

2 个答案:

答案 0 :(得分:1)

我也偶然发现了同一个问题。

似乎DefaultRouter已替换为Router https://developers.google.com/web/tools/workbox/reference-docs/latest/workbox.routing.Router 并进行初始化,您必须执行new workbox.routing.Router()

答案 1 :(得分:0)

现在我发现旁观有趣: 代替:

const router = new DefaultRouter();

我用:

router = new workbox.routing.constructor();