Search code examples
angularfirebase-hostinglighthouseangular-service-worker

Angular-Serviceworker: start_url does not respond with a 200 when offline


Versions

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

Description

I implemented the service-worker using ng add @angular/pwa --project [app] and Lighthouse would recognise the web-app as a PWA. Suddenly, after one deploy to Firebase hosting the "install" notification wasn´t popping up, so I checked the dev-console.

The manifest was still showing up as usual:

enter image description here

But nothing was being cached:

enter image description here

When running a Lighthouse-Audit, I get following error:

start_url does not respond with a 200 when offline

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)"
        ]
      }
    }
  ]
}

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"
    }
  ]
}

Solution

  • So I found the solution to my problem in this comment to a similar problem: https://stackoverflow.com/a/61152966/8581106

    In app.module.ts add registrationStrategy: 'registerImmediately' to the registration of the ServiceWorker like this:

    ServiceWorkerModule.register('ngsw-worker.js', { enabled: env.production, registrationStrategy: 'registerImmediately' }),