- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 javascript webapp 的服务 worker 中使用 Workbox。想要清除所有内容的整个工作箱/应用程序缓存...基本上回到与第一次将应用程序加载到浏览器之前的状态尽可能相似的状态,可能随后通过 window.location.href 刷新= '/'。
谷歌搜索并查看 SO,我找到了各种从缓存中清除各种内容的方法。我还没有想出一个简单的方法来“清除所有内容并重新开始”。
我在 sw.js 的服务器代码中试过这个:
var logit = true;
if (logit) console.log('Service Worker Starting Up for Caching... Hello from sw.js');
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js');
if (workbox) {
if (logit) console.log(`Yay! Workbox is loaded 🎉`);
} else {
if (logit) console.log(`Boo! Workbox didn't load 😬`);
}
workbox.routing.registerRoute(
// Cache image files
/.*\.(?:mp3|flac|png|gif|jpg|jpeg|svg|mp4)/,
// Use the cache if it's available
workbox.strategies.cacheFirst({
// Use a custom cache name
cacheName: 'asset-cache',
plugins: [
new workbox.expiration.Plugin({
// Cache only 20 images
maxEntries: 20,
// Cache for a maximum of x days
maxAgeSeconds: 3 * 24 * 60 * 60,
})
],
})
);
self.addEventListener('message', function(event){
msg = event.data;
console.log("SW Received Message: " + msg);
if (msg==='clearCache') {
console.log('Clearing Workbox Cache.');
WorkBoxCache = new workbox.expiration.Plugin;
WorkBoxCache.expirationPlugin.deleteCacheAndMetadata();
//WorkBoxCacheServer.clear();
}
});
在客户端与此配对:
navigator.serviceWorker.controller.postMessage("clearCache");
虽然消息显然已通过,但这没有用。此外,这似乎是一个不优雅的解决方案,我认为有一个更简单的解决方案。
如何做到这一点?
如何在浏览器的客户端js中从客户端发起?这在服务器端代码中有什么要求(例如在 sw.js 中)。
谢谢
最佳答案
CacheStorage
可在客户端代码(您注册软件的位置)中访问,因此您可以从那里删除它。
caches.keys().then(cacheNames => {
cacheNames.forEach(cacheName => {
caches.delete(cacheName);
});
});
关于javascript - 清除所有内容的 Workbox 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54376355/
我正在尝试为一个小型静态站点设置一个简单的 Service Worker,但出现 Service Worker 控制台错误:sw.js:59 Uncaught TypeError: workbox.e
我正在开发一个 pwa(带有类星体的 vue 应用程序),我真的很困惑,因为按照指示包含选项会导致我的服务 worker workbox.core.skipWaiting() 中的指令导致Servic
有人可以帮忙告诉我如何使用 workbox-build 缓存谷歌字体吗?我想在文件中包含要预缓存的 url,但这对我来说是不可能的。与普通的 service worker 不同,我所要做的就是在安装事
我正在玩工作箱,但出现以下错误: Skipping file 'app.js' due to size. [Max size supported is 2097152, this file is 47
我在 Wokbox 中遇到了一个似乎是 Sitecore 错误的问题。当“每页项目”下拉菜单设置为“全部”以外的任何内容时,我可以展开工作流程中的“草稿”和“等待批准”列表。 但是当“每页项目数”设置
我是服务人员的新手,并且正在使用Workbox来预缓存我的应用程序 shell 并缓存我的api数据。 Assets 的预缓存工作正常,正在创建和填充缓存。 在我第二次重新加载页面之前,运行时缓存不会
默认情况下 Sitecore 工作框显示项目名称,我想显示“标题”字段(自定义字段)。我怎样才能做到这一点? 最佳答案 我有一个类似的要求,客户需要 Workbox 来显示项目的“路径”而不是它的名称
在某些操作的 sitecore 工作箱中,我们可以通过关闭“禁止消息”属性来允许用户输入一些评论。是否有可能使它们成为必需? 最佳答案 我写了一篇关于为 发表评论的博客文章拒绝 命令是强制性的。您应该
我有一个 Sitecore 工作流程。当我使用项目旁边的“批准”按钮批准项目时,它会要求发表评论。当我使用“批准”(选定)或“批准”(全部)按钮批准多个项目时,它不会要求发表评论。这是为什么?我可以让
我正在使用 create-react-app 和 react-app-rewired-workbox 构建一个 React 应用程序使用工作箱。 我想使用 workbox 的 networkOnly
我想要一个数组来配置部分路径(当响应与请求的 URL 匹配时缓存响应)和过期时间(在 X 秒后使缓存过期),但我无法创建“处理程序” ' 才能正常工作。 这是配置路径和过期时间的数组: const P
我一直在使用 Google 的 workbox library现在有一段时间,在非常基本的水平上。大部分效果很好,但由于某些原因,我的 manifest.json 中的 start_url 未在初始页
我正在使用 Nuxt.js 框架,我的 WorkBox 配置如下所示 workbox: { workboxURL: 'https://cdn.jsdelivr.net/npm/workbox-
我希望使用 Workbox 缓存本地和远程图像 Assets 。目前是否支持,如果支持,如何支持? 基本上我想有以下功能: workboxBuild.injectManifest({ swSr
我已经实现了 Workbox 来使用 webpack 生成我的服务 worker 。 这很好用 - 我可以确认 revision运行时在生成的服务 worker 中更新 yarn run genera
我正在处理我的 PWA 应用程序。 所以我有一个问题,我找不到任何如何解决的信息。 我将工作箱与 webpack InjectManifest 一起使用(但也尝试过 webpack offline-p
如何在 WorkboxSW 中使用以下代码来注册所有每个缓存 URL 的路由。这个每个缓存的 url 包含 ajax,也会发送到服务器! $.ajax({ url : '/MyAPI/Recor
我正在尝试使用 Workbox 缓存整个页面,但它无法按照我的要求工作我只想缓存而不是 html 我想使用 Image、Js、Css 缓存整个页面目前它仅缓存 html workbox.routing
在 javascript webapp 的服务 worker 中使用 Workbox。想要清除所有内容的整个工作箱/应用程序缓存...基本上回到与第一次将应用程序加载到浏览器之前的状态尽可能相似的状态
通常每当我阅读有关 PWA 的博客文章时,该教程似乎只是预缓存每一个 Assets 。但这似乎有点违背应用程序外壳模式,据我了解,它是:缓存最基本的必需品(仅应用程序外壳),并在运行时缓存运行时。 (
我是一名优秀的程序员,十分优秀!