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

I have fixed the above issue, now ngsw.json is getting generated in dist folder and I am seeing that no service worker registered is coming. Here is the screen shot. 5th

I am also confused about 'ngsw-worker.js' or 'woker-basic.min.js'. Like which one I should be using? Thanks, Rahul

rahulsahay19 avatar Feb 01 '18 13:02 rahulsahay19

@rahulsahay19 Try to post a question @ http://stackoverflow.com

odahcam avatar Feb 01 '18 15:02 odahcam

@odahcam not a problem. Its fixed. Thanks!

rahulsahay19 avatar Feb 02 '18 08:02 rahulsahay19

@rahulsahay19 , can I know what the fix is?

zendizmo avatar Feb 08 '18 05:02 zendizmo

@zendizmo In my case, my ngsw-config.json file was not on latest schema. When I took the sample config, then it worked later on moved my changes in there!

rahulsahay19 avatar Feb 08 '18 07:02 rahulsahay19

This issue is still marked as open. Can I get a solid confirmation that Angular CLI now fully supports Service Worker? Has anyone put a larger Angular app in production with Service Worker enabled successfully? Cheers

bjornharvold avatar Feb 08 '18 13:02 bjornharvold

Apparently there are cases that prevent PWA work properly in Angular, but they are not clearly stated in documents or any errors get returned while building your app. If you are only dealing with Angular's own packages you are supposed to be fine, when you are using external libraries (even ones developed by Google's own people, like AngularFire2) chances are it will fail by default, then you search and patch your code to make it work.

So I believe now it's rather a problem of properly informing erroneous cases & documentation issue than service worker part of Angular itself. Still it didn't work for me as is.

bogacg avatar Feb 08 '18 14:02 bogacg

@bjornharvold, for me it doesn't 😢

odahcam avatar Feb 08 '18 15:02 odahcam

One more question wanted to check, whether below urls can be relative.

{ "name": "api-performance", "urls": [ "https://www.xyz.com/Security/auth/.well-known/openid-configuration", "https://www.xyz.com/Security/auth/interaction/7bc4260e-b1df-4e6c-8b17-" ] } So, If i am deploying app in different environments, this xyz can be replaced accordingly with that environment. Thanks.

rahulsahay19 avatar Feb 14 '18 07:02 rahulsahay19

There own example did not work, when applying an update service like described in https://angular.io/guide/service-worker-communications#checking-for-updates. So probably more an Angular ticket and not a problem of angular-cli?

mszalbach avatar Feb 14 '18 12:02 mszalbach

Maybe both? This seems to be a different issue. 🤔

odahcam avatar Feb 14 '18 13:02 odahcam

@markus-heinisch I had the same problem as you. I solved it using a manifest.json with the following:

    "external": {         
        "urls": [               
                {"url": "http://fonts.googleapis.com/icon?family=Material+Icons"}
        ]     
   }

abariatti avatar Feb 21 '18 17:02 abariatti

@markus-heinisch @alxhub You know the problem with this approach is, you need to enable to download .json file from server, which is not a good idea. I mean you can have meaningful json files at server side, which you don't want to get downloaded upon any http request and manifest.webManifest approach is giving unable to detect manifest error.

rahulsahay19 avatar Feb 23 '18 04:02 rahulsahay19

@abariatti Thanx for your tip. I have tested with some config changes and found a working solution in file ngsw-config.json:

"urls": [ "https://fonts.gstatic.com/s/materialicons/**/*.woff2", "https://fonts.googleapis.com/icon?family=Material+Icons" ]

markus-heinisch avatar Feb 24 '18 16:02 markus-heinisch

I am using Angular CLI 1.7.3 and Service Worker 5.2.8. With an existing app It doesn't work. However, in new CLI app works fine

javid-abd avatar Mar 10 '18 17:03 javid-abd

alright, I found the issue. You should check again what 3rd party libraries or dependencies you have installed. in my case, I have installed particle.js via npm, and it somehow stopped service workers from working. I'm trying to find a compatible way to use both of them

javid-abd avatar Mar 11 '18 06:03 javid-abd

I've solved this issue with a workaround, what it does is register the service worker and simulate the messages that ServiceWorkerModule sends. I've created a little service with this behaviour, you can check it here: https://github.com/Pedro-vk/EthKudos/blob/abb2104e4338e5b7529e7b50838d9ab787c386fc/src/app/shared/service-worker.service.ts

Pedro-vk avatar Apr 16 '18 16:04 Pedro-vk

I tried Pedro-vk's solution. Worked better than other solutions - on Chrome. Now note, that I've been trying to multiple solutions on the same environment. My goal was: get a signal hasUpdates$=true so I can display a div for the user to see there is a new version.

Works great and out of the box (sort of... )

What I managed to pinpoint is on both (FireFox and Chrome) workers are active, do a checkForUpdates and download the new altered files.

What I cannot put my finger on is that on Chrome signaling works as expected on this line of code:

 if (message.data.type === 'UPDATE_AVAILABLE') 
{
          console.log("UPDATE_AVAILABLE");
          this._onUpdate.next();

 }

But Firefox messages never get 'UPDATE_AVAILABLE', but always 'STATUS'

Stuck...

Some print screens:

image image

IgorceJovanoski avatar May 02 '18 13:05 IgorceJovanoski

@Argentan 's solution about regiter service worker in main.ts worked perfect to me!

Tecaa avatar Jun 13 '18 20:06 Tecaa

@Argentan saved me hours too! Using latest Angular/CLI, the SW was registered in app,module and was not working. Applying @Argentan's fix above got me a running SW. Thank you!

ThumbGen avatar Jun 14 '18 19:06 ThumbGen