Avr*_*dis 7 javascript caching service-worker cacheapi
我正在使用ServiceWorker并且在开发模式下工作得很好,我的问题是在生产模式下我的包名是使用哈希生成的,例如1234das3123ad5.bundle.js,因此服务工作者不会缓存它.我的sw代码如下:
self.addEventListener('install', function(event) {
// pre cache a load of stuff:
event.waitUntil(
caches.open('mycache').then(function(cache) {
return cache.addAll([
'/dist/bundle.js',
'/dist/app.css',
'/dist/index.html',
'https://cdnjs.cloudflare.com/ajax/libs/antd/2.7.2/antd.css'
]);
})
)
});
Run Code Online (Sandbox Code Playgroud)
在Cache API的文档中,我没有看到任何关于如何实现这一点的示例.
显然我可以缓存dist文件夹下的所有内容,例如:
self.addEventListener('install', function(event) {
// pre cache a load of stuff:
event.waitUntil(
caches.open('mycache').then(function(cache) {
return cache.addAll([
'/dist/',
]);
})
)
});
Run Code Online (Sandbox Code Playgroud)
但我发现它不是一个优雅,长期良好的解决方案.这是在Cache中使用通配符的方法吗?有点像'/dist/*.bundle.js'?
Web没有文件夹的概念,特别是浏览器无法知道以前缀开头的所有有效URL.
您的网站的其余部分必须以某种方式处理修改资产的URL更改,那么为什么不在服务工作者中使用相同的解决方案?
这就是我为我的博客所做的 - Django的静态文件管理器添加了正确的URL https://jakearchibald.com/sw.js
| 归档时间: |
|
| 查看次数: |
3281 次 |
| 最近记录: |