【问题标题】:Could Angular service worker update cache key data?Angular Service Worker 可以更新缓存关键数据吗?
【发布时间】:2025-11-26 19:15:01
【问题描述】:

我的应用程序由 express.js 和 index.html 提供服务,由 EJS 模板引擎呈现

所以当我第一次访问我的网站时,index.html 会正确呈现。

但是如果我刷新页面,index.html 会从 service worker 加载,因为

service worker 的缓存是原始 index.html 文件

我想更新一些缓存数据(不是全部)或者如何缓存渲染的 html 文件。

例如:

如果我想更新 index.html 缓存数据,我应该怎么做?

我真的需要别人的帮助。

列出一些角度 sw 配置:

ngsw-config.json

{
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "lazy"(prefetch both have tried),
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html",
          "/*.css",
          "/*.js"
        ]
      }
    }, {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**"
        ]
      }
    }
  ]
}

我的 Dist 项目结构

/dist

--index.html

--*.js

【问题讨论】:

    标签: angular express service-worker


    【解决方案1】:

    好的,我找到一个临时解决方案来解决这个问题:

    将数据组配置添加到 nsgw-config.json

      "dataGroups": [
        {
          "name": "api-performance",
          "urls": [
            "/api/**"(you can ignore this line),
            "/"
          ],
          "cacheConfig": {
            "strategy": "freshness",
            "maxSize": 100,
            "maxAge": "3d",
            "timeout":"2m"
          }
        }
      ]
    

    我知道我们通常配置它来缓存 json api 数据,但我认为它也可以缓存 html/纯文本。

    虽然可行,但有人有更好的主意吗?

    【讨论】: