首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在使用workbox时忽略缓存url中的url querystring?

如何在使用workbox时忽略缓存url中的url querystring?
EN

Stack Overflow用户
提问于 2017-09-18 22:23:37
回答 5查看 4.3K关注 0票数 1

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

代码语言:javascript
运行
复制
workboxSW.router.registerRoute('https://example.com/data/image?screenSize=980',
workboxSW.strategies.cacheFirst({
    cacheName: 'mycache',
    cacheExpiration: {
        maxEntries: 50
    },
    cacheableResponse: {statuses: [0, 200]}
})
);

在尝试了cachedResponseWillBeUsed插件之后:我没有看到插件被应用:

EN

Stack Overflow用户

发布于 2017-09-19 00:17:22

更新:从Workbox v4.2.0开始,新的Workbox生命周期回调可以帮助覆盖读写操作的默认缓存键:

原始响应:

您应该能够通过编写在配置策略时传入的cachedResponseWillBeUsed plugin来做到这一点:

代码语言:javascript
运行
复制
// 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
);
票数 5
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46281732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档