Angular Serviceworker doesn't cache index.html

67 views Asked by At

My app doesn't work when offline, because the angular serviceworker doesn't cache my index.html (it does cache my js, css, manifest and ico files). This only happens as long as my outputPath is within my git/nx directory. If the outputPath is outside this directory, the index.html gets cached by the serviceworker.

This can clearly be observed in the chrome cache storage.

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

NGSW Debug Info doesn´t show any errors:

NGSW Debug Info:

Driver version: 15.2.7
Driver state: NORMAL ((nominal))
Latest manifest hash: d00a80d860dae8cade766b28147236c702b1e264
Last update check: never

=== Version d00a80d860dae8cade766b28147236c702b1e264 ===

Clients: 077bde91-523b-4306-aa45-392b39fc200f

=== Idle Task Queue ===
Last update tick: 1s11u
Last update run: never
Task queue:
 * init post-load (update)
 * init post-load (cleanup)
 * initialization(d00a80d860dae8cade766b28147236c702b1e264)
 * activate: cleanup-old-sw-caches

Debug log:

Knowing that I can just set my outputPath to be outside my Repo to make it work is a workaround for now, but get's problematic with CI/CD.

Did anyone ever have this problem? How can I get further information on what's happening to my service worker here?

1

There are 1 answers

0
JonasK On BEST ANSWER

I had my serviceworker extended by my own javascript to listen to calls from the sync API. Therefore I extended the serviceworker like this

ServiceWorkerModule.register('sw-master.js', ...)

and then in sw-master.js I called the Angular serviceworker:

importScripts('./ngsw-worker.js');

Something about this seemed to not have worked correctly in some cases. Since I don´t need the sync API anymore I changed the ServiceWorkerModule to call ngsw-worker.js directly and now my app works correctly when offline.

No idea how this had anything to do with the build/dist directoy, probably just a false interpretation on my end.