How to run service worker locally with Angular

前端 未结 4 1292
小蘑菇
小蘑菇 2021-01-12 15:16

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

4条回答
  •  长发绾君心
    2021-01-12 15:44

    It seems that currently we cannot use service worker with ng serve --prod. However we can make a workaround.

    1. We build the project ng build --prod

    2. From the dist location we take the ngsw-worker.js and ngsw.json files and copy them to the src folder.

    3. 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".

    4. We serve – 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!

提交回复
热议问题