如何缓存动态URL-Workbox-Service Worker

时间:2019-10-17 09:08:48

标签: javascript php workbox

我正在尝试缓存动态URL。

它像这样正常工作:

workbox.routing.registerRoute(
  'https://www.testestest.com/?Q=Start',
  workbox.strategies.networkFirst({
      networkTimeoutSeconds: 3,
      cacheName: 'dynamicentry1',
      plugins: [
        new workbox.expiration.Plugin({
          maxEntries: 50,
        }),
      ],
  })
);

但是如果我想获得这样的动态内容:

workbox.routing.registerRoute(
  'https://www.testestest.com/?Q=Start&testid=',
  workbox.strategies.networkFirst({
      networkTimeoutSeconds: 3,
      cacheName: 'dynamicentry1',
      plugins: [
        new workbox.expiration.Plugin({
          maxEntries: 50,
        }),
      ],
  })
);

如果您单击https://www.testestest.com/?Q=Start&testid=1,则该站点应被动态缓存。 我该怎么办?

谢谢你们

1 个答案:

答案 0 :(得分:0)

您可以使用regular expression而不是字符串来设置路由,并获得所需的行为。 https://developers.google.com/web/tools/workbox/guides/route-requests#matching_a_route_with_a_regular_expression

上的Workbox文档中有更多信息。
workbox.routing.registerRoute(
  new RegExp('/\\?Q=Start'),
  workbox.strategies.networkFirst({
      networkTimeoutSeconds: 3,
      cacheName: 'dynamicentry1',
      plugins: [
        new workbox.expiration.Plugin({
          maxEntries: 50,
        }),
      ],
  })
);