1

我正在尝试从 sw-toolbox 移动到 Workbox,我需要做的一件事是缓存来自不同服务器上的 API url 的所有查询字符串结果,我尝试了一些代码,但我没有任何成功了。

这是我最近的尝试:

workbox.routing.registerRoute(
  'https://domain.example-third-party.co.uk/API/' + '(.*)',
  workbox.strategies.cacheFirst({
    cacheName: 'extra',
    plugins: [
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

顺便说一句,我试过不带“(。*)”。

4

1 回答 1

1

在 Workbox v3(根据plugins语法,它看起来像您正在使用的内容)中,您需要传入 RegExp 作为匹配路由的条件。

workbox.routing.registerRoute(
  new RegExp('^https://domain\.example-third-party\.co\.uk/API/'),
  workbox.strategies.cacheFirst({
    cacheName: 'extra',
    plugins: [
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

在https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-sw#migrate_from_hand-crafted_sw-toolbox_to_workbox-sw有一个您可能已经看过的示例,以及路由的一般指南在https://developers.google.com/web/tools/workbox/guides/route-requests的 Workbox v3 中

于 2018-03-09T16:31:36.093 回答