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
[Service Worker] Can't update sw if in SAFE_MODE #22087
Comments
Once the SW enters a degraded state (SAFE_MODE), it will stay there - it's not possible to exit such a state from the application. A restart of the SW should end up in a good state though, provided |
Thanks for investigating 😊. What do you think about a function on A current workaround would be to call |
Im experiencing the same issue.
My logic looks like this: public checkUpdates() {
this.swUpdate$ = this.swUpdate.available.subscribe(event => {
console.log('[App] Update available: current version is', event.current, 'available version is', event.available);
if (event.current.hash !== event.available.hash) {
const snackbarRef = this.snackbar.open('New version available!', 'Refresh');
snackbarRef.onAction().pipe(take(1)).subscribe(() => {
this.winRef.nativeWindow.location.reload();
});
} else {
this.snackbar.open('Your version is the newest!');
}
});
this.activateUpdate$ = this.swUpdate.activated.subscribe(event => {
console.log('[App] Update activated: old version was', event.previous, 'new version is', event.current);
});
}
public checkForUpdate() {
console.log('[App] checkForUpdate started');
this.swUpdate.checkForUpdate()
.then(() => {
console.log('[App] checkForUpdate completed');
})
.catch(err => {
console.error(err);
});
}
public activateUpdate() {
console.log('[App] activateUpdate started');
this.swUpdate.activateUpdate()
.then(() => {
console.log('[App] activateUpdate completed');
this.winRef.nativeWindow.location.reload();
})
.catch(err => {
console.error(err);
});
} In this way i can make a button in my app which can be tapped by the user for check updates UPDATE: Everything works fine, if i close chrome devtools! Opened devtools screws up service worker update mechanism. Check if you can manage to work sw update after closing devtools! Hope this helps |
Hopefully the DevTools related issues will be fixed by #22883, @petivagyok16. But it does seem that after entering Also, I can't think of any reason not to process @alxhub, wdyt? |
I was just hit by this on my dev machine and had a hell of a time figuring out what happened. Unregistering the service worker wasn't enough... had to unregister AND clear my cache in Chrome. If this were to ever affect an end user they wouldn't stand a chance. |
Hi .. same problem here : ngsw-worker.js:2270 Uncaught (in promise) Error: Invariant violated (assignVersion): want AppVersion for null but not loaded If i clear chache and hit CTRL + F5 the service worker throw error and app crash. |
As mentioned above, the current behavior is by design. The degraded modes will not persist across instances, so once the browser terminates the SW instance after a period of inactivity, the mode will go back to See, also, #31109 for a related discussion about the
So, I am going to close this issue as That said, I think it might be reasonable to expose the driver state as a service (or maybe expose the whole debug info (including the driver state) as JSON). Free free to open a feature request for that. An ugly work-around for now, would be to request /ngsw/state and parse the text content to extract the driver state. E.g.: fetch('/ngsw/state').
then(res => res.text()).
then(text => (text.match(/Driver state: (\w+)/) || [, 'UNKNOWN'])[1]); |
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. |
I'm submitting a...
Current behavior
When the service worker is installed / initialized the first time, it fetches the
ngsw.json
. If the content ofngsw.json
is not an valid json the sw goes into SAFE_MODE:angular/packages/service-worker/worker/src/driver.ts
Lines 320 to 325 in c8a1a14
From thereon it is impossible to update the service worker. If you call
SwUpdate.checkForUpdate()
it never resolves or rejects and no new fetch tongsw.json
is performed due to the fact that messages are ignored in SAFE_MODE:angular/packages/service-worker/worker/src/driver.ts
Lines 189 to 193 in c8a1a14
Replacing the wrong
ngsw.json
with the correct one does only resolve this issue if:ngsw.json
did not contain json. for example it contains html)ngsw.json
did contain a json. ex:{ "test": 123 }
).This also triggers the following error:
Error: Invariant violated (initialize): latest hash null has no known manifest
This issue occurred on our firebase hosted app. It seems a cache sometimes returns a html page with a 2xx status instead of the
ngsw.json
and broke our service worker. We also needed to clear the cache and unregister the service worker on some mobile clients. It might be that firebase sends sometimes a wrong json file or there might be another way to lock the service worker in the SAFE_MODE withlatest hash null
error.Expected behavior
SwUpdate.checkForUpdate()
when the sw is in SAFE_MODEngsw.json
? I'm not sure when and if this would be the appropriated way to handle this issue.Minimal reproduction of the problem with instructions
Repo: https://github.com/sean-nicholas/test-angular-service-worker
npm run build
cd
into `dist``ngsw.json
toasdjknkfdnjgdfkaslkdm
{ "test": 123 }
http-server
(or something similar) indist
folderUnexpected token a in JSON at position 0
TypeError: Cannot convert undefined or null to object
Check for SW Update
ButtonChecking
will stay true and not fall back to false after a few msngsw.json
with the correct dataFor non json case:
For json case:
Error: Invariant violated (initialize): latest hash null has no known manifest
occuresCheck for SW Update
seems to work but if you reload it fails (does not resolve) againWhat is the motivation / use case for changing the behavior?
The service worker should not lock itself into a state were it can not be updated anymore.
Environment
The text was updated successfully, but these errors were encountered: