New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
There should be a seamless way to get rid of broken service worker state #43163
Comments
Update It seems that even safety worker approach doesn't unless I cleared sw cache:
And as docs suggest to keep safety worker live, if I do so, I see below error:
|
On further investigation I found that since we are also using Now since our service worker was in error state and we wanted to get rid of that worker, we replaced the contents of
I got this fixed by passing my Angular Service Worker (which is now in a new file with name
|
Thx for the update, @naveedahmed1! I am going to close this, since I don't think there is anything that can be done on our side (ServiceWorkers have some limitations/idiosyncrasies and we have to live with them 😃). AFAICT, the methods described in https://angular.io/guide/service-worker-devops should work (if applied correctly). |
@gkalpak it seems that the safety worker approach doesn't work unless the user refreshed the page. Some of our clients are reporting they are seeing below error in console::
Refreshing the page fixes the issue. Is there any way to prompt user to refresh page if the service worker is broken? |
Yes, that's expected. There is no way to replace an activated ServiceWorker unless a new ServiceWorker script is downloaded and that only happens a new navigations.
The Angular SW does (partially or completely) deactivate itself if it detects that it is in a broken state. But if you provide your own SW script (for example, by concatenating the Angular SW with a different one), how would you detect that the SW is broken? |
What if the page is reloaded automatically after service worker detects that its in broken state? When the service worker is in a broken state, and the JavaScript bundle needed to render that page is missing. In that case what user sees is a page with just top navigation bar and empty body contents. Even clicking the navigation links doesn't work. In that case I believe its more useful to automatically refresh that page bypassing service worker (or after unregistering sw). |
Theoretically, that shouldn't happen with the Angular SW, because when it detects that it is broken it will forward requests to the network. If it is an unrecoverable state, it will also emit on SwUpdate#unrecoverable (so the app could do the reload). Automatically reloading could be a bad UX (for example, it could cause the user to lose work). |
I see, I recently added below code to my
Is it the correct way to handle such situations? In my case, I removed the old js bundles from the server, which is causing the issue for the users. I added above code in latest version, and I hope it will help avoiding such situations in future. I have also replaced the contents of my old service worker with contents of safety worker. Is there anything else I should do? |
Regarding the
Other than that, it looks fine. |
Thanks for your feedback @gkalpak, really appreciate. Here's my clear cache code, I am clearing just Angular Service Worker cache:
What you are suggesting is, we don't need to clear SW cache as well, SW should be able to handle that automatically, right? |
Yes, you shouldn't need to clear all SW caches. The SW may be serving multiple app versions simultaneously (i.e. serving different versions to different tabs). If a single version is broken, you don't need to delete all caches (since some may correspond to good versions). The SW will stop using the caches corresponding to the broken version (and will eventually remove them, since they are unused). |
Thank you so much once again @gkalpak :) |
@gkalpak nothing worked until we updated the safety worker to the follow:
Before that, new service worker was installed successfully but it was randomly showing contents from old cache. For example our service worker caches home page https://www.mustakbil.com/
The question is why new service worker was serving the request from the cache of old service worker. |
Hm...yeah, it makes sense to have the safety worker remove the ngsw caches. Feel free to submit a PR to augment the safety worker with that functionality. |
…ular#43324) clear angular service worker cache in safety worker to ensure stale or broken contents are not served in future requests Fixes angular#43163 PR Close angular#43324
…ular#43324) clear angular service worker cache in safety worker to ensure stale or broken contents are not served in future requests Fixes angular#43163 PR Close angular#43324
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Which @angular/* package(s) are the source of the bug?
service-worker
Is this a regression?
No
Description
I faced a strange issue with Angular Service worker. My website users were being served with old version of the app which was never getting updated.
If I close browser window, open again and navigate to https://www.mustakbil.com it serves the cached version of the app from service worker. If I refresh the page it loads new version of the app but when I close the browser and reopen it shows same old version of the app.
When I debug the service worker, it shows Driver state: EXISTING_CLIENTS_ONLY. Here's the output of
/ngsw/state
I tried the approach of
safety worker
as suggested in Angular docs (https://angular.io/guide/service-worker-devops#safety-worker) and got rid of my old worker.Docs suggest
For most sites, this means that you should serve the safety worker at the old Service Worker URL forever.
, this means we should rename our main worker. I did the same. Now when I deployed my app, I noticed that safety worker always kicks in; and my new service worker doesn't get registered as a result I see below error in my console log:So apparently its now stuck in safety worker.
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
Anything else?
No response
The text was updated successfully, but these errors were encountered: