I am following angular's best practice in order to make PWA.
After making production build (ng build --prod --aot
), I am also running the service worker from dist, on localhost
:
http-server -p 8080 -c-1 dist
When I am trying to sync the worker with my Angular, using:
navigator.serviceWorker.ready.then(function(swRegistration) {
console.log('swReady');
});
Nothing happens, and seems that SW is not communicating with Angular.
Working with a remote server (uploading dist) does work. So seems that the problem is dist not working with ng serve
.
What am I doing wrong?
It seems that currently we cannot use service worker with ng serve --prod
. However we can make a workaround.
We build the project ng build --prod
From the dist
location we take the ngsw-worker.js
and ngsw.json
files and copy them to the src
folder.
We modify our angular.json
file in order to serve them. We find the property "projects": {"[my-project-name]": {... "architect": {... "build": {... "options": {... "assets": [...
and there we add these two items – "src/ngsw-worker.js", "src/ngsw.json"
.
ng serve --prod
.I have reached till that point. The browser says that the SW is activated and running. The only consideration now is that if we change something in the SW, we need to rebuild again and make the same steps. But I believe we can develop more rapidly.
Good luck!