Skip to content

Instantly share code, notes, and snippets.

@kamlekar
Last active May 23, 2018 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamlekar/c97246ceb399dcb71db478580c9977e5 to your computer and use it in GitHub Desktop.
Save kamlekar/c97246ceb399dcb71db478580c9977e5 to your computer and use it in GitHub Desktop.
Adding Service worker to Angular 6 project
  • In package.json:

      dependencies: {
          ...
          "@angular/service-worker": "6.0.3",
          ...
      }
    
  • In angular.json:

      "architect": {
          "build": {
              "configurations": {
                  "production": {
                      ...
                      "serviceWorker": true,
                      "ngswConfigPath": "/src/ngsw-config.json",
                      ...
                  }
              }
          }
      }
    
  • In app.module.ts

      imports: [
          ....
          ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production })
          ....
      ],
    
  • Create 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/**"
                  ]
              }
          }]
      }
    

And then npm install or npm update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment