angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

bug(@angular/pwa): ng add @angular/pwa fails to update angular.json and register service worker

Open kareemwezza opened this issue 10 months ago • 5 comments

Command

add

Is this a regression?

  • [x] Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

20

Description

After running ng add @angular/pwa, the application fails to register the service worker. Specifically, the browser attempts to fetch ngsw-worker.js but receives a 404 Not Found error. Investigation shows that the angular.json file was either not updated with the serviceWorker: true flag or the build process is not generating the required service worker files in the output directory.

The issue is resolved only after manually editing angular.json. This indicates the schematic is failing to write the necessary configuration to the workspace file even though it reports success.

Image

Expected Behavior

The ng add schematic should correctly configure angular.json so that ng build generates ngsw-worker.js and ngsw.json in the production output. The service worker should be successfully registered and visible in chrome://serviceworker-internals/.

Environment Angular: 21.0.8 Angular CLI: 21.0.5 Node: 24.12.0 OS: darwin arm64

Minimal Reproduction

1. Run ng add @angular/pwa --project your-project-name.
2. Run a production build: ng build --configuration production.
3. Serve the dist/ folder using a static server (e.g., http-server).
4. Open the browser and check the Network Tab.
5. Observed Error: A request for /ngsw-worker.js fails with a 404 error. 
Image

Exception or Error

Always getting Network error 404 while trying to request ngsw-worker.js file.

Your Environment

Angular CLI       : 21.0.5
Angular           : 21.0.8
Node.js           : 24.12.0
Package Manager   : pnpm 10.28.1
Operating System  : darwin arm64

Anything else relevant?

Usin Pnpm Package menager Issue happens woth http-server and default ng server

kareemwezza avatar Jan 30 '26 13:01 kareemwezza

Do you have a production configuration? The service worker options are only added in the production configuration.

alan-agius4 avatar Jan 30 '26 14:01 alan-agius4

The app has 3 build configurations 'dev', 'tst' and 'liv'. Our Dev team moved from 'production' to 'liv' naming.

kareemwezza avatar Jan 30 '26 14:01 kareemwezza

In this case we should issue a warning since the schematic is looking for a production configuration.

alan-agius4 avatar Jan 30 '26 14:01 alan-agius4

Can't agree more, Thanks for your support.

kareemwezza avatar Jan 30 '26 14:01 kareemwezza

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.