有没有办法使用workbox忽略来自已注册路由下的查询字符串"?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插件之后:我没有看到插件被应用:

发布于 2017-09-19 00:17:22
更新:从Workbox v4.2.0开始,新的Workbox生命周期回调可以帮助覆盖读写操作的默认缓存键:
原始响应:
您应该能够通过编写在配置策略时传入的cachedResponseWillBeUsed plugin来做到这一点:
// 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
);https://stackoverflow.com/questions/46281732
复制相似问题