Workbox Service Worker MaxAgeSeconds查询

问题描述

我为我的网站安排了一名服务人员。但是,我不确定此设置的有效期。

Am目前使用Nextjs进行页面渲染,使用WorkBox和Apollo进行数据管理。 我的工作箱配置:

// File to generate the service worker.
require("dotenv").config()
const workBoxBuild = require("workBox-build")
const { COUNTRY: country,NODE_ENV } = process.env
const urlPattern = new RegExp(`/${country}\/static\/.*/`)

// https://developers.google.com/web/tools/workBox/reference-docs/latest/module-workBox-build#.generateSW
const buildSW = () => {
  return workBoxBuild.generateSW({
    swDest: "public/workBox-worker.js",clientsClaim: true,mode: NODE_ENV,skipwaiting: true,sourcemap: false,runtimeCaching: [
      {
        urlPattern: urlPattern,// Apply a cache-first strategy.
        handler: "CacheFirst",options: {
          cacheName: "Static files caching",expiration: {
            maxEntries: 50,maxAgeSeconds: 3600,},],})
}

buildSW()

我的Service Worker已安装并激活,并已开始缓存文件

Is cached?

我唯一的问题是这里的最大年龄不应该是3600吗?还是我做错了什么?

解决方法

我认为您正在将Cache-Control HTTP headerWorkbox Expiration混淆。

由于服务可以回复请求,因此无论Cache-Control标头如何,它都可以返回文件。您配置的是让Workbox在50或3600秒后将其从缓存中逐出。服务工作者拥有自己的缓存,可以在chrome dev tool

的应用程序标签中找到

看到有关两个人如何相互作用的问题-If you are using Service Workers do you still need cache-control headers?