Angular-Serviceworker:离线时 start_url 不响应 200

Luk*_*ggy 4 lighthouse firebase-hosting angular angular-service-worker

版本

  • "@angular/common": "~9.0.0"
  • "@angular/service-worker": "~9.0.0"

描述

我使用 Service-Worker 实现了ng add @angular/pwa --project [app],Lighthouse 会将 Web 应用程序识别为 PWA。突然,在部署到 Firebase 托管“安装”通知后没有弹出,所以我检查了开发控制台。

清单仍然像往常一样出现:

在此处输入图片说明

但没有缓存任何内容:

在此处输入图片说明

运行 Lighthouse-Audit 时,出现以下错误:

start_url does not respond with a 200 when offline
Run Code Online (Sandbox Code Playgroud)

ngsw-config.json

{
  "$schema": "./node_modules/@angular/service-worker/config/schema.json",
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html",
          "/manifest.webmanifest",
          "/*.css",
          "/*.js"
        ]
      }
    }, {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**",
          "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
        ]
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

manifest.webmanifest

{
  "name": "App",
  "short_name": "App",
  "theme_color": "#5787b2",
  "background_color": "#fafafa",
  "display": "standalone",
  "scope": "/",
  "start_url": "/",
  "icons": [
    {
      "src": "assets/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Luk*_*ggy 5

所以我在这个类似问题的评论中找到了我的问题的解决方案:https : //stackoverflow.com/a/61152966/8581106

app.module.ts添加registrationStrategy: 'registerImmediately'到这样的ServiceWorker登记:

ServiceWorkerModule.register('ngsw-worker.js', { enabled: env.production, registrationStrategy: 'registerImmediately' }),
Run Code Online (Sandbox Code Playgroud)