angularprogressive-web-appsservice-worker

Angular app still offline after adding ServiceWorker


I want to make a PWA app with Angular 19 that is available offline. I followed the official documentation to install the ServiceWorker. But when I simulate the offline status in the browser, the app is not available. I have tried running it with ng serve --configuration=production or with npx http-server -p 8080 -c-1 dist/browser but it didn't change anything.

The worker is registered like this:

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(),
    provideZoneChangeDetection({ eventCoalescing: true }),
    provideRouter(routes), provideServiceWorker('ngsw-worker.js', {
            enabled: !isDevMode(),
            registrationStrategy: 'registerWhenStable:30000'
          }),
  ],
};

I have also tried registerImmediately:30000, didn't help either.

Is there anything else I must do to get the worker working?


Solution

  • Apparently it is not enough to run ng build as the documentation states. It needs to be ng build --configuration=production