Workbox服务工作者maxAgeSeconds查询 [英] Workbox service worker maxAgeSeconds query

查看:112
本文介绍了Workbox服务工作者maxAgeSeconds查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have implemented a service worker for my website. However I am not sure about the expiration setting on this.

我目前正在使用Nextjs进行页面渲染,并使用Workbox和Apollo进行数据管理. 我的工作箱配置:

Am currently using Nextjs for page rendering and Workbox with Apollo for data mangement. My Workbox config:

// 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已安装并激活,并已开始缓存文件.

My service worker is installed and activated and has started caching files.

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

推荐答案

我认为您对工作箱到期.

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

As the service can reply to a request it may return a file regardless of the Cache-Control header. What you have configured is to have Workbox evict things from it's cache after 50 or 3600 secs. The service worker has it's own cache that can be found in the application tab of the chrome dev tool

查看有关这两者如何相互作用的问题-

see this question about how the two interact with each other - If you are using Service Workers do you still need cache-control headers?

这篇关于Workbox服务工作者maxAgeSeconds查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆