-
Notifications
You must be signed in to change notification settings - Fork 26.2k
fix(service-worker): handle offline mode and ignore invalid only-if-cached
requests
#22883
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
Conversation
You can preview aa7b71c at https://pr22883-aa7b71c.ngbuilds.io/. |
// (See also https://github.com/angular/angular/issues/22362.) | ||
// TODO(gkalpak): Remove once no longer necessary (i.e. fixed in Chrome DevTools). | ||
if ((req.cache as string) === 'only-if-cached' && req.mode !== 'same-origin') { | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's console.error
the first time this happens so it's not a silent failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a failure? It is just that the SW won't handle this request, so it will pass through (and I assume the browser will do the right thing).
(Interestingly, no error is logged when the SW doesn't handle such requests.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if someone is looking in DevTools, expecting to see their request answered (from ServiceWorker)
, that having a message locally there saying "hey, the SW didn't handle this request because DevTools sent it in an invalid way" is beneficial.
I'm okay if we only log the first time it happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed "offline", this is not a request made by the app/user, but a background request made by DevTools (apparently). So, we'll debugger.log()
the first time we see it.
I have pushed another commit. PTAL
You can preview 0535086 at https://pr22883-0535086.ngbuilds.io/. |
Previously, when trying to fetch `ngsw.json` (e.g. during `checkForUpdate()`) while either the client or the server were offline, the ServiceWorker would enter a degrade mode, where only existing clients would be served. This essentially meant that the ServiceWorker didn't work offline. This commit fixes it by differentiating offline errors and not entering degraded mode. The ServiceWorker will remain in the current mode until connectivity to the server is restored. Fixes angular#21636
Under some circumstances (possibly related to opening Chrome DevTools), requests are made with `cache: 'only-if-cached'` and `mode: 'no-cors'`. These request will eventually fail, because `only-if-cached` is only allowed to be used with `mode: 'same-origin'`. This is likely a bug in Chrome DevTools. This commit avoids errors related to such requests by not handling them. Fixes angular#22362
You can preview 5fae6e7 at https://pr22883-5fae6e7.ngbuilds.io/. |
You can preview 0235d68 at https://pr22883-0235d68.ngbuilds.io/. |
You can preview 76109cb at https://pr22883-76109cb.ngbuilds.io/. |
Hi, when can we expect this to be merged so that we can start playing with the service worker offline mode capability:) |
It is marked for merge, so I guess soon 😃 |
Previously, when trying to fetch `ngsw.json` (e.g. during `checkForUpdate()`) while either the client or the server were offline, the ServiceWorker would enter a degrade mode, where only existing clients would be served. This essentially meant that the ServiceWorker didn't work offline. This commit fixes it by differentiating offline errors and not entering degraded mode. The ServiceWorker will remain in the current mode until connectivity to the server is restored. Fixes #21636 PR Close #22883
Under some circumstances (possibly related to opening Chrome DevTools), requests are made with `cache: 'only-if-cached'` and `mode: 'no-cors'`. These request will eventually fail, because `only-if-cached` is only allowed to be used with `mode: 'same-origin'`. This is likely a bug in Chrome DevTools. This commit avoids errors related to such requests by not handling them. Fixes #22362 PR Close #22883
Previously, when trying to fetch `ngsw.json` (e.g. during `checkForUpdate()`) while either the client or the server were offline, the ServiceWorker would enter a degrade mode, where only existing clients would be served. This essentially meant that the ServiceWorker didn't work offline. This commit fixes it by differentiating offline errors and not entering degraded mode. The ServiceWorker will remain in the current mode until connectivity to the server is restored. Fixes #21636 PR Close #22883
Under some circumstances (possibly related to opening Chrome DevTools), requests are made with `cache: 'only-if-cached'` and `mode: 'no-cors'`. These request will eventually fail, because `only-if-cached` is only allowed to be used with `mode: 'same-origin'`. This is likely a bug in Chrome DevTools. This commit avoids errors related to such requests by not handling them. Fixes #22362 PR Close #22883
Hi i created a new angular cli project with the service worker enabled , still i see when app is made offline the 504 error appears. Can anyone suggest on this please:) |
The fix is included in If you don't want to use a version that is not yet marked as stable, you need to wait a bit for the final 6.0 release (currently scheduled for this week). |
@gkalpak , |
can anyone tell me when #22883 is likely to go into a release? |
As much as I remember, it is already in version 5.2.10. |
@Springrbua is right. It is in 5.2.10 and 6.x. |
I have the same problem. I've updated to v6.x
that's the error on Chrome 66.0.3359.139
|
Could be a different problem. Please open a new issue and make sure you provide a minimal reproduction. |
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. |
PR Checklist
Docs have been added / updated (for bug fixes / features)PR Type
What is the current behavior?
This PR fixes two issues:
ngsw.json
(e.g. duringcheckForUpdate()
) while either the client or the server are offline, the ServiceWorker would enter a degrade mode, where only existing clients would be served. This essentially means that the ServiceWorker doesn't work offline.cache: 'only-if-cached'
andmode: 'no-cors'
. These request will eventually fail, becauseonly-if-cached
is only allowed to be used withmode: 'same-origin'
.This is likely a bug in Chrome DevTools. See also Failed to load resource: the server responded with a status of 504 (Gateway Timeout) #22362 (comment).
Issue Number: #21636, #22362
What is the new behavior?
only-if-cached
requests (i.e. withmode !== 'same-origin'
) are not handled by the SW.Does this PR introduce a breaking change?
Other information
Fixes #21636 and #22362.