我正在尝试为runtimeCaching中的资产设置到期时间。举个例子,我有这样的例子:
{
// https://code.jquery.com/jquery-3.5.1.min.js
urlPattern: /^https:\/\/code\.jquery\.com\/jquery-3\.5\.1.*/,
handler: 'CacheFirst',
options: {
cacheName: 'external-assets',
cacheableResponse: {
statuses: [0, 200]
},
cacheExpiration: {
maxAgeSeconds: 60 * 60 * 24 * 14, // 2 Week
maxEntries: 1
}
}
},在控制台中,当我尝试通过NPM脚本运行generateSW()时,我得到:
> ispot@0.1.0 sw
> node sw_build.js
[Error [ValidationError]: "runtimeCaching[0].options.cacheExpiration" is not allowed] {
_original: {
swDest: './public/service-worker.js',
skipWaiting: true,
clientsClaim: true,
cleanupOutdatedCaches: true,
mode: 'development',
runtimeCaching: [ [Object], [Object], [Object], [Object], [Object], [Object] ]
},
details: [
{
message: '"runtimeCaching[0].options.cacheExpiration" is not allowed',
path: [Array],
type: 'object.unknown',
context: [Object]
}
]
}我发现在Workbox的网站上很难找到关于如何为generateSW()设置的文档。
发布于 2021-04-30 04:18:17
在这里找到了答案:
https://developers.google.com/web/tools/workbox/guides/generate-service-worker/workbox-build
对象键是过期,而不是cacheExpiration...
expiration: {
maxAgeSeconds: 60 * 60 * 24 * 14, // 2 Week
maxEntries: 100
}https://stackoverflow.com/questions/67324436
复制相似问题