Search code examples
angularservice-workerangular-service-worker

Service worker not detected


I'm build an angular 9 PWA with service worker. I followed the official guide to abilitate service worker and everything went fine until today, when I noticed that my app has no longer the sw features (cache, instalability etc)

Lighthouse states:

Does not register a service worker that controls page and start_url

Fact is, I did not touched anything about it, so I cannot figure out what's wrong.

ngsw.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)"
        ]
      }
    }
  ],
  "dataGroups": [
    {
      "name": "configs",
      "urls": [
        "active-profile",
        "configurations"
      ],
      "cacheConfig": {
        "maxSize": 1,
        "maxAge": "2d",
        "strategy": "performance"
      }
    },
    {
      "name": "searches",
      "urls": [
        "search"
      ],
      "cacheConfig": {
        "maxSize": 1,
        "maxAge": "15m",
        "strategy": "performance"
      }
    }
  ]
}

here the manifest:

{
  "name": "Shootbook",
  "short_name": "Shootbook",
  "theme_color": "#fafafa",
  "background_color": "#fff",
  "display": "standalone",
  "scope": "/",
  "start_url": "/",
  "icons": [
    {
      "src": "assets/icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png",
      "purpose": "any maskable"
    },
    {
      "src": "assets/icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png",
      "purpose": "any maskable"
    },
    {
      "src": "assets/icons/icon-128x128.png",
      "sizes": "128x128",
      "type": "image/png",
      "purpose": "any maskable"
    },
    {
      "src": "assets/icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png",
      "purpose": "any maskable"
    },
    {
      "src": "assets/icons/icon-152x152.png",
      "sizes": "152x152",
      "type": "image/png",
      "purpose": "any maskable"
    },
    {
      "src": "assets/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png",
      "purpose": "any maskable"
    },
    {
      "src": "assets/icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png",
      "purpose": "any maskable"
    },
    {
      "src": "assets/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png",
      "purpose": "any maskable"
    }
  ]
}

Any ideas?


Solution

  • I found the solution: it seems that Angular waits for the app to be "stable" (whatever this means...). To override this behaviour just set registrationStrategy to 'registerImmediately':

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