angular: bug(ServiceWorker): multiple apps with ServiceWorker on one domain

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

The installation of a 2nd App/ServiceWorker (with different baseHref) purges the Cache Storage entries for a previously installed App/ServiceWorker, if they are on the same domain. This breaks the 1st installed App. It is not reachable anymore - you will always be redirected to the latest installed App.

Expected behavior

The installation of multiple Apps/ServiceWorkers (with different baseHref) on one domain should be possible.

Minimal reproduction of the problem with instructions

I created the following repo to reproduce the issue: https://github.com/skydever/repro-multiple-ng-apps-one-domain-serviceworker. Follow the steps of the README there.

What is the motivation / use case for changing the behavior?

I have to deploy multiple Angular Apps on one domain.

Environment


Angular version: 5.1.3


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v8.9.1  
- Platform: Windows 

Others:

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 13
  • Comments: 32 (10 by maintainers)

Commits related to this issue

Most upvoted comments

🎉 awesome!! thx a lot to everybody involved in this 👍

I don’t think adding a value to ngsw-config.json is a good idea, as it does not guarantee uniqueness across subpaths. Nor is hashing ngsw.json, since this will ignore the previous caches and always download all assets from the server.

The SW scope should be used instead. In case anyone has missed it, there is a PR in progress for that: #27080

Solution proposed by @petersalomonsen also worked for me. I would suggest adding a namespace field to ngsw-config.json, which if is present when generating ngsw-worker.js is used as a postfix to all occurences of ngsw: in that file.

i.e. if a namespace field is provided in ngsw-config.json with a value of “my-app”, then perform a find/replace on ngsw-worker.js post generation (or during) replacing all occurences of ngsw: with ngsw-my-app:

allowing this namespace value to be provided in tooling would also be useful. e.g. as a question when performing ng add @angular/pwa

I don’t think anyone is working on it and neither have things changed in Angular 7 (afaik). If you want to take a stub at it, I’d be happy to review.

Nobody is actively working on this afaict (there are other higher priority stuff going on). If anyone wants to investigate this further, I’d be happy to review (but it won’t be a trivial undertaking 😱).

@skydever - Service worker works fine for one app but does not get loaded for other app as it does not have baseHref while loading the ngsw.json for other app. So the first default root app works fine with service worker and I have to add dataGroups to ask for fresh copy of other app. We would like to use one domain and allow multiple apps loading based on the route (/app1,/app2). Making manual changes ngsw-worker.js will not be good for prod so I will probably wait for the fix which will allow us to have ngsw.json loaded per app.

I created an issue at the Angular CLI repo too. both are involved somehow, the baseHref option of the CLI needs to be passed to the service worker. I don’t think that the service worker is able to do that without information from the CLI?