1

有没有办法使用工作箱从注册路由下方忽略查询字符串“?screenSize =”!如果我可以使用正则表达式,我将如何在下面的场景中编写它?基本上,无论 screenSize 查询字符串是什么,我都希望匹配缓存。

workboxSW.router.registerRoute('https://example.com/data/image?screenSize=980',
workboxSW.strategies.cacheFirst({
    cacheName: 'mycache',
    cacheExpiration: {
        maxEntries: 50
    },
    cacheableResponse: {statuses: [0, 200]}
})
);

在尝试了 cachedResponseWillBeUsed 插件后:我没有看到插件被应用: 在此处输入图像描述

4

5 回答 5

5

更新:从 Workbox v4.2.0 开始,新的cacheKeyWillBeUsed生命周期回调可以帮助覆盖读取和写入操作的默认缓存键:https ://github.com/GoogleChrome/workbox/releases/tag/v4.2.0

原始回复:

您应该可以通过编写一个在配置策略时传入的cachedResponseWillBeUsed插件来做到这一点:

// See https://workboxjs.org/reference-docs/latest/module-workbox-runtime-caching.RequestWrapper.html#.cachedResponseWillBeUsed
const cachedResponseWillBeUsed = ({cache, request, cachedResponse}) => {
  // If there's already a match against the request URL, return it.
  if (cachedResponse) {
    return cachedResponse;
  }

  // Otherwise, return a match for a specific URL:
  const urlToMatch = 'https://example.com/data/generic/image.jpg';
  return caches.match(urlToMatch);
};

const imageCachingStrategy = workboxSW.strategies.cacheFirst({
  cacheName: 'mycache',
  cacheExpiration: {
      maxEntries: 50
  },
  cacheableResponse: {statuses: [0, 200]},
  plugins: [{cachedResponseWillBeUsed}]
});


workboxSW.router.registerRoute(
  new RegExp('^https://example\.com/data/'),
  imageCachingStrategy
);
于 2017-09-18T16:17:22.160 回答
4

另一个答案的基础上,caches.match有一个选项ignoreSearch,所以我们可以简单地使用相同的 url 再试一次:

cachedResponseWillBeUsed = ({cache, request, cachedResponse}) => {
  if (cachedResponse) {
    return cachedResponse;
  }

  // this will match same url/diff query string where the original failed
  return caches.match(request.url, { ignoreSearch: true });
};
于 2018-05-24T19:50:31.403 回答
1

从 v5 开始,基于 aw04 的答案,代码应如下所示:

const ignoreQueryStringPlugin = {
    cachedResponseWillBeUsed: async({cacheName, request, matchOptions, cachedResponse, event}) => {
        console.log(request.url);
        if (cachedResponse) {
            return cachedResponse;
        }

        // this will match same url/diff query string where the original failed
        return caches.match(request.url, {ignoreSearch: true});
    }
};
registerRoute(
    new RegExp('...'),
    new NetworkFirst({
        cacheName: 'cache',
        plugins: [
            ignoreQueryStringPlugin
        ],
    })
);
于 2020-04-23T16:59:14.340 回答
0

ignoreURLParametersMatching参数对我有用:

https://developers.google.com/web/tools/workbox/modules/workbox-precaching#ignore_url_parameters

于 2019-05-15T16:21:21.043 回答
0

您可以使用cacheKeyWillBeUsed简单的修改保存的缓存键以完全忽略查询,并将对 url 的每个响应与任何查询进行匹配。

const ignoreQueryStringPlugin = {
 cacheKeyWillBeUsed: async ({request, mode, params, event, state}) => {
  //here you can extract the fix part of the url you want to cache without the query
  curl = new URL(request.url);
  return curl.pathname;

 }
};

并将其添加到策略中

workbox.routing.registerRoute(/\/(\?.+)?/,new 
 workbox.strategies.StaleWhileRevalidate({
  matchOptions: {
   ignoreSearch: true,
  },
  plugins: [
   ignoreQueryStringPlugin
  ],
}));
于 2021-04-06T12:08:36.647 回答