Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-cli update service-worker id of undefined error

I've followed the official guide of angular and successfully installed the initial service worker. Next I wanted to tryout the updating behaviour and got a

TypeError: Cannot read property 'id' of undefined

where as id refers to client.id in a method in ngsw-worker.js

async updateClient(client) {
        // Figure out which version the client is on. If it's not on the latest,
        // it needs to be moved.
        const existing = this.clientVersionMap.get(client.id);
....
}

The steps i did are

  1. Register SW, went offline and reloaded (worked), closed tab but kept window open
  2. Updated code in app.cmp.html
  3. Rebuilt project (ng build --prod)
  4. opened new Tab and the app (could observe SW update)
  5. reload app
  6. error

I'm using angular-cli 1.7.4 and all angular libraries including service-worker are on 5.2.10.

Edit 1 08.05.2018

There is an issue posted on github on angular-cli 6 https://github.com/angular/angular/issues/23742

Edit 2 08.05.2018

What I could find out so far:

ng build --prod does not create a new version of ngsw-worker.js (on reloading the new fresh build, chrome does not display the "wait for activation"). When I reload with or without closing the tab prior, the error appears.

When I manually add something to ngsw-worker.js (like a console.log) then the usually lifecycle starts and the old service worker is being replace by the "new" one.

When I reload before the activation is being done, the same error appears. Otherwise it works...

Furthermore trying to remove the service-worker using angular's Fail-safe (removing or renaming the ngsw-config) causes a build error.

Expected to find an ngsw-config.json configuration file in the /Users/hanche/Desktop/Development/playground/toggl2 folder. Either provide one or disable Service Worker in your angular.json configuration file.

Edit 3 & 'Solution' 09.05.2018

This issues is supposed to get fixed if this Pull Request from 1st May 2018 is accepted.

Meanwhile, you can auto-unregister the serviceworker by removing the ngsw.json from the dist/ folder and NOT ngsw-config.json as described in the fail safe section. See reply I got here.

Furthermore it could also be a chrome bug

like image 357
Han Che Avatar asked May 03 '18 18:05

Han Che


2 Answers

In our case the problem was resolved after upgrading to angular 6.0.0. If it's possible for your project, you should give that a try.

I looked at the ngsw-worker.js and it changed quite a bit in 6.0.0 compared to (in our case) 5.2.0.

like image 196
csim Avatar answered Sep 20 '22 20:09

csim


We can work around this error by downgrading service.worker 5.2.10 to 5.2.0 "@angular/service-worker": "5.2.0",

Update

After a second deploy we ran into the same error. Now we have unregistered all service workers in chrome (chrome://serviceworker-internals/) and now it seems to work with version 5.2.10 also. Don't know where the problem lies.

like image 29
ratfury Avatar answered Sep 17 '22 20:09

ratfury