3
我正在使用ServiceWorker,並且在開發模式下工作完美,我的問題是,在生產模式下,我的軟件包名稱是使用散列生成的,例如, 1234das3123ad5.bundle.js
,所以服務人員沒有緩存它。我SW的代碼如下所示:ServiceWorker/Cache API中的通配符
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'
]);
})
)
});
在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/',
]);
})
)
});
但是,我發現這是一個優雅的,長期良好,解決方案。這是一種在緩存中使用通配符的方法嗎?像'/dist/*.bundle.js'
?
THX傑克,我可能會找到的WebPack的方式來動態生成正確的URL。 –
所以你說的是sw.js是基於通過webpack或sed/awk等其他方法生成的包名稱動態生成/修改的? –
當然,也可以使用'fetch('files.json')加載名稱,然後(r => r.json())' –