Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Service worker files ngsw.json / ngsw-worker.json generated when running build --prod. Angular 5

Using angular 5.1.2 Angular-cli 1.6.3

all files are pretty vanilla. dist folder is created

My ngsw-config.json

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

}

my app.module.ts uses

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

am using vanilla environment.ts and environment.prod.ts

main.ts

import './polyfills.ts';
import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';

import {AppModule} from './app/app.module';
import {environment} from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
.then(() => {
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('ngsw-worker.js');
}

})
.catch(err => console.log(err));

Not sure what else to check

like image 576
user2182570 Avatar asked Jan 04 '18 18:01

user2182570


2 Answers

Do you add serviceWorker property to apps first entry in .angular-cli.json? If not, angular-cli not built it.

"apps": [
  {
    ...
    "serviceWorker": true
  }
}

EDIT As of December 2018, .angular-cli.json is now angular.json, and the format for the serviceWorker is now:

{
    ...
    "projects": {
         "my-project-name": {
             "architect": {
                 "build": {
                     "configurations": {
                        "production": {
                           "serviceWorker": true
                        }
                     }
                 }
             }
         }
    }

You'll need to specify serviceWorker: true for every configuration where you want the service worker.

like image 162
kklimczak Avatar answered Oct 20 '22 01:10

kklimczak


After a long search, we found out that the missing --prod flag in our build makes the problem. Exactly this one produces the service-worker files and no additional build flag is able to do it (same with uglifying and the uglifyer was the reason why we had exchanged the --build flag against --env=build earlier). More information about what happens on the --build flag to be found here: Angular 2 with CLI - build for production.

like image 35
seawave_23 Avatar answered Oct 20 '22 01:10

seawave_23