首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Workbox generateSW()中设置runtimeCaching的过期时间?

如何在Workbox generateSW()中设置runtimeCaching的过期时间?
EN

Stack Overflow用户
提问于 2021-04-30 04:10:57
回答 1查看 34关注 0票数 0

我正在尝试为runtimeCaching中的资产设置到期时间。举个例子,我有这样的例子:

代码语言:javascript
复制
    {
        // 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()时,我得到:

代码语言:javascript
复制
> 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()设置的文档。

EN

回答 1

Stack Overflow用户

发布于 2021-04-30 04:18:17

在这里找到了答案:

https://developers.google.com/web/tools/workbox/guides/generate-service-worker/workbox-build

对象键是过期,而不是cacheExpiration...

代码语言:javascript
复制
expiration: {
   maxAgeSeconds: 60 * 60 * 24 * 14, // 2 Week
   maxEntries: 100
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67324436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档