- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经实现了 Workbox 来使用 webpack 生成我的服务 worker 。
这很好用 - 我可以确认 revision
运行时在生成的服务 worker 中更新 yarn run generate-sw
(package.json: "generate-sw": "workbox inject:manifest"
)。
问题是 - 我注意到我的客户在新版本后没有更新缓存。
即使在更新服务 worker 几天后,我的客户仍在缓存旧代码,新代码只会在几次刷新和/或取消注册服务 worker 后缓存。
对于每个版本,const CACHE_DYNAMIC_NAME = 'dynamic-v1.1.0'
已更新。
如何确保客户端在新版本发布后立即更新缓存?
serviceWorker-base.js
importScripts('workbox-sw.prod.v2.1.3.js')
const CACHE_DYNAMIC_NAME = 'dynamic-v1.1.0'
const workboxSW = new self.WorkboxSW()
// Cache then network for fonts
workboxSW.router.registerRoute(
/.*(?:googleapis)\.com.*$/,
workboxSW.strategies.staleWhileRevalidate({
cacheName: 'google-font',
cacheExpiration: {
maxEntries: 1,
maxAgeSeconds: 60 * 60 * 24 * 28
}
})
)
// Cache then network for css
workboxSW.router.registerRoute(
'/dist/main.css',
workboxSW.strategies.staleWhileRevalidate({
cacheName: 'css'
})
)
// Cache then network for avatars
workboxSW.router.registerRoute(
'/img/avatars/:avatar-image',
workboxSW.strategies.staleWhileRevalidate({
cacheName: 'images-avatars'
})
)
// Cache then network for images
workboxSW.router.registerRoute(
'/img/:image',
workboxSW.strategies.staleWhileRevalidate({
cacheName: 'images'
})
)
// Cache then network for icons
workboxSW.router.registerRoute(
'/img/icons/:image',
workboxSW.strategies.staleWhileRevalidate({
cacheName: 'images-icons'
})
)
// Fallback page for html files
workboxSW.router.registerRoute(
(routeData)=>{
// routeData.url
return (routeData.event.request.headers.get('accept').includes('text/html'))
},
(args) => {
return caches.match(args.event.request)
.then((response) => {
if (response) {
return response
}else{
return fetch(args.event.request)
.then((res) => {
return caches.open(CACHE_DYNAMIC_NAME)
.then((cache) => {
cache.put(args.event.request.url, res.clone())
return res
})
})
.catch((err) => {
return caches.match('/offline.html')
.then((res) => { return res })
})
}
})
}
)
workboxSW.precache([])
// Own vanilla service worker code
self.addEventListener('notificationclick', function (event){
let notification = event.notification
let action = event.action
console.log(notification)
if (action === 'confirm') {
console.log('Confirm was chosen')
notification.close()
} else {
const urlToOpen = new URL(notification.data.url, self.location.origin).href;
const promiseChain = clients.matchAll({ type: 'window', includeUncontrolled: true })
.then((windowClients) => {
let matchingClient = null;
let matchingUrl = false;
for (let i=0; i < windowClients.length; i++){
const windowClient = windowClients[i];
if (windowClient.visibilityState === 'visible'){
matchingClient = windowClient;
matchingUrl = (windowClient.url === urlToOpen);
break;
}
}
if (matchingClient){
if(!matchingUrl){ matchingClient.navigate(urlToOpen); }
matchingClient.focus();
} else {
clients.openWindow(urlToOpen);
}
notification.close();
});
event.waitUntil(promiseChain);
}
})
self.addEventListener('notificationclose', (event) => {
// Great place to send back statistical data to figure out why user did not interact
console.log('Notification was closed', event)
})
self.addEventListener('push', function (event){
console.log('Push Notification received', event)
// Default values
const defaultData = {title: 'New!', content: 'Something new happened!', openUrl: '/'}
const data = (event.data) ? JSON.parse(event.data.text()) : defaultData
var options = {
body: data.content,
icon: '/images/icons/manifest-icon-512.png',
badge: '/images/icons/badge128.png',
data: {
url: data.openUrl
}
}
console.log('options', options)
event.waitUntil(
self.registration.showNotification(data.title, options)
)
})
caches.keys().then(cacheNames => {
cacheNames.forEach(cacheName => {
caches.delete(cacheName);
});
});
最佳答案
我认为您的问题与以下事实有关:当您对应用程序进行更新并部署时,会安装新的服务人员,但未激活。这解释了为什么会发生这种情况的行为。
原因是registerRoute
函数还注册 fetch
listeners ,但是在新的 service worker 启动之前不会调用这些 fetch listeners 。另外,您的问题的答案:不,您不需要自己删除缓存。 Workbox 负责这些。
让我知道更多细节。当您部署新代码时,如果用户关闭您网站的所有选项卡并在此之后打开一个新选项卡,它会在 2 次刷新后开始工作吗?如果是这样,那就是它应该如何工作。在您提供更多详细信息后,我将更新我的答案。
我建议您阅读以下内容:https://redfin.engineering/how-to-fix-the-refresh-button-when-using-service-workers-a8e27af6df68并遵循第三种方法。
关于javascript - 新版本上的 Workbox 更新缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60912127/
我以使用文件库的方式实现。 然后我有以下代码: 更新根:一个HtmlRoot super updateRoot: anHtmlRoot. anHtmlRoot title: self title. a
这里有一个问题要问我的父亲。从 Excel 5 到现在他使用 Excel 2002,他在 Excel 中使用 VBA 已经有二十多年了。 由于他在Excel中使用VBA很长时间,并且广泛使用记录宏的功
我正在编写一个程序来在本地备份我的 Google 相册库,上周代码运行良好,但在过去的几天里,我反复从 API 客户端收到错误消息,说“photoslibrary v1”不存在在 Google 的 A
谁能建议如何解决这两个问题? - 无法升级 Jenkins 和 SVN 插件 - 无法连接到 svn 我正在 Windows 64 位机器上设置 Jenkins。它被配置为作为 Windows 服务运
这个问题已经有答案了: Trouble with UTF-8 characters; what I see is not what I stored (5 个回答) 已关闭 6 年前。 它在我的旧服务
我无法在 Chrome 新版本中打开弹出窗口。我单击打开模式对话框弹出窗口的链接,但弹出窗口没有出现。我猜测这是 google chrome 新版本的问题。 function DownloadRepo
我试图找到一种方法来结合“NewReleases”ResponseGroup 和关键字搜索。 我正在使用 PHP Soap Library . 我知道它不再维护,因为这里是 new version ,
我在谷歌分析中创建了新帐户 并发现它有新的用户界面:没有 View ,无法添加目标。如何添加? 最佳答案 刚刚打开 的切换事件 在转换报告中将其标记为 转换 : 关于google-analytics
我从 Codecanyon 购买(许可)了一些不在 GIT 中维护的代码。我现在对它做了很多更改(使用 GIT)。很快原始代码的作者将发布一个新版本。我会想在不丢失我自己的情况下 merge 他们的更
首先,我指的是这个问题( LINK ),它对我没有帮助,因为当时框架的版本较低,所以我开始新问题。 所以我正在使用 Grails 2.4.3 框架并尝试在其中使用 Drools 规则引擎。Java版本
这个问题已经有答案了: Firefox Web Console Disabled? (4 个回答) 已关闭 9 年前。 我在 Mac OSX 上运行 Firefox 28.0,并尝试使用控制台和 Fi
我遇到了有关应用内购买的问题。我之前通过应用内购买启动了我的应用程序 v1.0。现在我通过应用内购买上传了 v1.1。该应用程序显示“等待审核”状态。但是In-App purchase还是显示“Wai
我们都知道 - 这是列出我们最喜欢的软件的每个新版本带来的变化的阅读。每当它捆绑为文件(Changes.txt、CHANGES、WhatsNew.txt 等)或出现在安装程序中时,这通常是我们在安装/
我想将 hibernate-3.5-1.Final 与此插件一起使用,这里我的依赖项应该是什么。它似乎正在拿起一组较旧的 jar ,但现在失败了。
我注意到昨天发布了OpenCV的新版本v2.3。 我无法使用cmake以及编译Linux和Unix源代码的基本说明在Mac中安装此版本。 我在执行make指令时获得了以下错误消息: /Users/jo
我正在将 POI jar 从旧版本升级到新版本 ( 3.10.1 )。我在构建文件中看到 poi-contrib.jar 位于类路径上,并且版本为 2.5.1-final-20040804。(我不确定
前一段时间我创建了this帖子,我和我的同事找到了两个不同的答案(都有效): 第一个解决方案是使用 Intent callIntent = new Intent (“android.intent.ac
起初集群中的所有代理都可以正常启动和工作,但有时其中一个代理会遇到问题。并且会出现一些现象: 整个集群挂了,生产者和消费者也不工作,因此网络流量从监视器降为零; 使用kafka-topic.sh描述t
在 Chrome 更新之前,我的代码运行良好。 我对我的服务器进行了 ajax 调用。我的服务器收到调用,将 JSON 返回给客户端,但答案始终为空。当我查看 Fiddler 时,我从服务器得到了答案
我从事一个项目已有 1 年,并定期进行重要更新。上次在 3 天前,我存档并生成了一个 IPA,但在加载屏幕后,应用程序崩溃了。但是当我运行该应用程序时,它工作正常,一切都像以前一样工作。 检查下面的链
我是一名优秀的程序员,十分优秀!