Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script

I am getting the following error

ERROR Error: Uncaught (in promise): TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.
TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.

I used the prompt "ng add @angular/pwa" to add my ngsw-config.json and manifest file. This prompt also added my "serviceWorker" : true statement and my register with environment to my app.module.ts

After verifying everything was added properly I then ran the command "ng build --prod" that built successfully and the dist shown was made dist folder

Then I ran "ng serve --prod" and I am receiving the errors shown in the image errors

Line being used in app.module.ts

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

ngsw-configuration:

{
  "index": "/",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html",
          "/*.css",
          "/*.js"
        ]
      }
    }, {
      "name": "icons",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/icons/**"
        ]
      }
    }
  ]
}

Placement of serviceWorker true in angular.json:

"configurations": {
            "production": {
              "serviceWorker": true,
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
like image 992
Blue Space Avatar asked Nov 30 '18 03:11

Blue Space


People also ask

How do you fix a bad HTTP response code 404 was received when fetching the script?

On the page where these errors are displayed, go to Dev Tools -> Applications -> Clear storage -> Clear site data. The Service Workers has one feature, it really will cache the information.

How to add service worker in Angular 7?

Adding a service worker to your projectlink To set up the Angular service worker in your project, use the CLI command ng add @angular/pwa . It takes care of configuring your application to use service workers by adding the @angular/service-worker package along with setting up the necessary support files.


2 Answers

Since you have mentioned the root as dist folder, your service-worker.js file must be in the dist folder after you run the ng build script.

Add "precache": "sw-precache --verbose --config=sw-precache-config.js" to your package.json scripts object.

Then run: ng build --prod --aot, then npm run precache. Now your service-worker.js file would be present in the dist folder.

Since the angular compiler has compiled your app code into js files and stored it in the dist folder, now you must serve your app from the dist folder.

But the default ng serve doesn't support it. I suggest you use http-server. npm install http-server -g. Then http-server ./dist -o. This opens up your app on the default browser.

like image 148
Ismoil Shifoev Avatar answered Oct 06 '22 17:10

Ismoil Shifoev


Using Angular 10, the solution is just simply to run http-server rather than ng-serve which unfortunately doesn't support service workers.

1. Use NPM to install http-server:

npm install http-server --save-dev

2. Serve your project using http-server

Then (let's assume your project is called my project), run:

# cd into the root folder for your angular project
http-server -p 8080 -c-1 dist/myproject

i.e. this assumes that the exports for your project are located in the folder dist/myproject/ (change if not).

Note: the -c-1 instruction tells HTTPServer to switch off caching - see docs for details.

like image 29
Peter Nixey Avatar answered Oct 06 '22 19:10

Peter Nixey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!