Angular Universal 与 Angular Service Worker

zga*_*evi 7 service-worker server-side-rendering angular-universal angular

我正在开发一个同时使用SSR和SW的项目。

SSR 在首次渲染和爬虫读取页面时完美运行。

另一方面,Service Worker 也工作得很好。

但是 Service Worker 缓存index.html文件后出现问题。

以下是重现问题的步骤:

  1. 当您首先打开页面时,您会看到 SSR 渲染了 html,并且立即得到结果
  2. 然后 Service Worker 缓存所有内容
  3. 当您刷新或更改页面时,您看不到SSR渲染的html,因为index.html是由Service Worker缓存的
  4. 如果从缓存中删除 index.html Service Worker 会崩溃

我使用的是 Angular 7.0

这是我正在使用的ngsw-config.json :

{
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "updateMode": "prefetch",
      "resources": {
        "files": ["/favicon.ico", "/index.html", "/*.css", "/*.js"]
      }
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": ["/assets/**"]
      }
    }
  ],
  "dataGroups": [
    {
      "name": "api",
      "urls": ["https://example.com/**"],
      "cacheConfig": {
        "maxSize": 30,
        "maxAge": "1d",
        "timeout": "30s",
        "strategy": "freshness"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)