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

Existing App does not get Service Worker capabilities

Open bogacg opened this issue 8 years ago • 53 comments

Versions

Angular CLI: 1.6.0
Node: 9.2.0
OS: win32 x64
Angular: 5.1.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

@angular/cli: 1.6.0
@angular-devkit/build-optimizer: 0.0.35
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.41
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.0
@schematics/angular: 0.1.10
@schematics/schematics: 0.0.10
typescript: 2.6.2
webpack: 3.10.0

Repro steps

Have an existing app with no PWA capability and apply steps to upgrade it as described in docs.

Observed behavior

Despite the fact it builds with no error and dist folder has files like ngsw.json & ngsw-worker.js as expected, they are not loaded on network request when browsed. (Tested with incognito mode as well) No service-worker effect at all in app.

Desired behavior

Service Worker added to built app when steps in official docs are applied.

Mention any other details that might be useful

On the same machine, a new app generated with Angular-CLI 1.6.0 with --service-worker flag. That app loads service worker as expected.

bogacg avatar Dec 07 '17 10:12 bogacg

Hi, a quick note, updated to angular 6, firebase 5 and angularfire2 5rc10 and the fix is no longer needed, service workers works out of the box.

Argentan avatar Jun 16 '18 23:06 Argentan

Based on https://github.com/angular/angular-cli/issues/8779#issuecomment-397844943, this seems to have been resolved.

gkalpak avatar Jul 09 '18 16:07 gkalpak

I struggled with something similar for a long time, but my problem was different. I used http://0.0.0.0:8000 as my URL, but then the serviceWorker is not present as navigator.serviceWorker.

I just changed it to http://localhost:8000 and everything works. It must be because localhost is considered a secure origin.

Maybe that will help someone.

skovmand avatar Sep 13 '18 09:09 skovmand

@dirand analysis solved my problem: SW did not get registered because of some external code running intervals in the default routed component.

To detect it, I simply started to comment everything suspicious and then go by exclusion, wrapping the unstable code in this.applicationRef.isStable.subscribe((isStable) {}.

@dirand Thanks for your great contribution, I struggled with this way to much!

michelepatrassi avatar Sep 29 '18 10:09 michelepatrassi

With angular-cli 6.2.5 and angular 6.1.10, this issue still exists. Also related to angular#20970, thanks @dirand 's solution

Teamop avatar Oct 18 '18 08:10 Teamop

As of Angular CLI 7.1.3 and PWA 0.11.3, @Argentan 's fix is still required. Had this problem today and could not for the life of me figure out why this was not working, but had it working in older apps.

joshuaballas avatar Dec 14 '18 17:12 joshuaballas

thanks @ChokeBilly! I had similar messed up default route redirect.

sandgupta23 avatar Jan 13 '19 21:01 sandgupta23

This costs me few hours to find out what is not working. My service worker dont register in whatever.

The problem is, service worker only worked on HTTPS(secure content)

Nyconing avatar Mar 07 '19 01:03 Nyconing

The solution from @Argentan works and is more simple.

BLecha avatar Mar 12 '19 09:03 BLecha

I know its an old old thread and @Argentan's solution has worked for me for most browsers. However for some weird reason it sends back a rejected promise in Safari 12.1.1

This is the error i get:

Promise Rejection: TypeError: TypeError: Type error

Am i the only one who is having this problem?

I get the same error if i try to register Service Worker directly from safari developer console:

navigator.serviceWorker.register('/ngsw-worker.js').then(function(reg){
   console.log("Yes, it did.");
}).catch(function(err) {
   console.log("No it didn't. This happened: ", err)
});

P.S: I am on Angular v8.0.1 and latest NG CLI.

ngsw-conf.json:

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/index.html",
        "/*.css",
        "/*.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/images/*.png"
      ]
    }
  }]
}

hassanasad avatar Jun 17 '19 16:06 hassanasad

Apparently its an issue with the latest Angular 8 release. https://github.com/angular/angular/issues/31061

hassanasad avatar Jun 18 '19 14:06 hassanasad

@hassanasad I am also having the same issue. With Version 8.2.2

Chrome is detecting Manifest but not detecting Service Workers...

Angular CLI: 8.2.2
Node: 10.16.3
OS: linux x64
Angular: 8.2.3

ruslanguns avatar Aug 22 '19 00:08 ruslanguns

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.