workbox - 为路由手动执行 workbox.expiration.Plugin.deleteCacheAndMetadata()
问题描述
该方法workbox.expiration.Plugin.deleteCacheAndMetadata()
最近已添加到工作箱中。如何为我设置缓存的特定路线触发此功能?
这里的用例是当客户端上发生某些操作时,我需要删除缓存。我可以直接从客户端删除缓存,但这会留下元数据(在 indexedDB 中)。所以我宁愿向服务人员发送消息并使用工作箱方法进行清理。如何获得正确的过期插件实例来调用该方法?
解决方案
我认为要记住的主要事情是workbox.expiration.Plugin
实例与路由无关——它被传递给给定的策略。它可以存在于路由定义之外,您可以稍后在代码中引用它。
这是一个例子:
const expirationPlugin = new workbox.expiration.Plugin({
maxEntries: 20,
}),
workbox.routing.registerRoute(
new RegExp('/images/'),
workbox.strategies.cacheFirst({
cacheName: 'image-cache',
plugins: [
expirationPlugin,
],
})
);
self.addEventListener('message', (event) => {
if (event.data === 'clear-cache') {
expirationPlugin.deleteCacheAndMetadata();
}
});
推荐阅读
- python - 合并两个 dfs 并合并 nan 值的结果
- python-3.x - 并排排列文本文件以制作矩阵文件
- c++ - 即使我只是检查“超出范围”值而不访问它,为什么会发生分段错误
- unity3d - 当时间流逝时,如何使这个敌人的目标更快地进行
- javascript - 获取节点 Js 中重复 JSON 对象中值相同的电子邮件
- ios - 在范围内找不到“ContentView”
- c++ - 在调用函数中可以访问对字符串文字的返回引用(作为参数传递),但是,存储引用的返回对象为空
- c# - 测试 WCF - 没有端点监听
- byte-buddy - byte-buddy Advice 或 Intercept 构造函数调用
- android - 将 GCC 编译为静态工具链和插件是否支持互斥?