【问题标题】:How to set expiration for runtimeCaching in Workbox generateSW()?如何在 Workbox generateSW() 中设置 runtimeCaching 的过期时间?
【发布时间】:2021-04-29 20:10:56
【问题描述】:

我正在尝试在我的 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() 进行设置的文档。

【问题讨论】:

    标签: progressive-web-apps service-worker workbox


    【解决方案1】:

    在这里找到答案:

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

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

    expiration: {
       maxAgeSeconds: 60 * 60 * 24 * 14, // 2 Week
       maxEntries: 100
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 2018-04-29
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      相关资源
      最近更新 更多