【发布时间】:2018-06-17 16:09:34
【问题描述】:
我按照此处Google PWA tutorial 的说明制作了具有离线功能的我自己的应用程序。当我对我的 localhost:3000 运行 Lighthouse 检查时,我收到了一份报告,说一切正常。
请注意,我只缓存了我的索引文件和 svg 图片资源。
self.addEventListener('install', event => {
event.waitUntil(
caches
.open('word-cloud-v1')
.then(cache => {
return cache.addAll([
'/',
'/index.html',
'./images/paper-plane.svg',
'./images/idea.svg',
'./images/desk-lamp.svg',
'./images/stopwatch.svg',
'./images/pie-chart.svg',
])
})
)
})
但是当我离线并尝试运行我的应用程序时,我收到一些文件尚未加载的错误。
所以我回去添加更多文件到缓存。请注意,此文件不是我创建的。
.then(cache => {
return cache.addAll([
'/',
'/index.html',
'./static/js/bundle.js',
'./images/paper-plane.svg',
'./images/idea.svg',
'./images/desk-lamp.svg',
'./images/stopwatch.svg',
'./images/pie-chart.svg',
])
})
虽然离线功能现在可以正常工作,但我还看到了一堆其他随机生成的文件,这些文件是在我尚未明确缓存的构建文件夹中创建的。那么我应该在 Service Worker 中缓存哪些文件以便它们以离线模式显示?
TLDR;除了/、/index.html 和图像之外,我们还应该缓存哪些文件,以便我们可以使用离线功能?
【问题讨论】:
标签: service-worker offline-caching pwa