You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
service-worker: configure response status codes to treat as failed/offline with freshness strategy #45954
Which @angular/* package(s) are the source of the bug?
service-worker
Is this a regression?
No
Description
Expected behaviour: freshness strategy should return to user fetching requested data only if request was successful.
If request is unsuccessful (has 500, 504 and other codes) it should return cached result instead of bad result. And bad result should not be cached at all.
But in reality:
freshness strategy return for user bad results.
Interestingly, freshness strategy works as expected if web-server absolutely died (network error: connection refused). I mean, all configured in dataGroup requests begin to return from cache and web-application works despite dead backend. It is different with particular bad request (as I described above).
Performance strategy works as expected (it returns cached results firstly)
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
No response
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 11.2.19
Node: 14.8.2
OS: darwin x64
Angular: 11.2.14
Reacted by Anton Marchenko, Andrey Rastorguev, Oleg Chernyavsky, Aleksandr Skotarev, Aleksandra Potekhina, Igor Katsuba, Lukonin Arseny, Воробьев Дмитрий, Alexander Shilov, Nikita Ershov and 29 more
changed the title [-]ngsw: 500-code requests are caching with freshness strategy[/-][+]ngsw: 500-code requests return to user with freshness strategy[/+]on May 11, 2022
Regarding the caching of bad results:
Data-groups will only cache good results, unless we're talking about opaque responses. With opaque responses there is a default behavior per strategy, but you can also configure it per data-group using the cacheOpaqueResponses option.
Now, regarding returning cached data for "bad" (i.e. non-2XX) responses:
I can see your point there. Currently, the fresheness strategy will only fall back to the cache if the fetch fails (i.e. if there is no response), such as when there is no internet connection. I can see however how you might want to handle some other response codes (such as 502 or 504) in the same way.
While it would be quite a breaking change, maybe we could make it a configurable option (i.e. a list of response HTTP codes that would result in returning cached results).
TBH, I don't expect there to be a huge demand for this feature, but I am going to keep this as a feature request and see what others think.
changed the title [-]ngsw: 500-code requests return to user with freshness strategy[/-][+]service-worker: configure response status codes to treat as failed/offline with freshness strategy[/+]on May 12, 2022
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
Activity
[-]ngsw: 500-code requests are caching with freshness strategy[/-][+]ngsw: 500-code requests return to user with freshness strategy[/+]gkalpak commentedon May 12, 2022
Thx for reporting this, @anton-marchenko 👍
Regarding the caching of bad results:
Data-groups will only cache good results, unless we're talking about opaque responses. With opaque responses there is a default behavior per strategy, but you can also configure it per data-group using the cacheOpaqueResponses option.
Now, regarding returning cached data for "bad" (i.e. non-2XX) responses:
I can see your point there. Currently, the
freshenessstrategy will only fall back to the cache if the fetch fails (i.e. if there is no response), such as when there is no internet connection. I can see however how you might want to handle some other response codes (such as 502 or 504) in the same way.While it would be quite a breaking change, maybe we could make it a configurable option (i.e. a list of response HTTP codes that would result in returning cached results).
TBH, I don't expect there to be a huge demand for this feature, but I am going to keep this as a feature request and see what others think.
[-]ngsw: 500-code requests return to user with freshness strategy[/-][+]service-worker: configure response status codes to treat as failed/offline with freshness strategy[/+]angular-robot commentedon May 13, 2022
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
anton-marchenko commentedon May 16, 2022
@gkalpak
As I understand from documentation, voters should just set 👍 for the topic post. Is it correct?
gkalpak commentedon May 16, 2022
Yes, that's right, @anton-marchenko.
anton-marchenko commentedon May 31, 2022
@gkalpak
Seems like we have 20+ upvotes.
Could you move the request to your consideration list?
gkalpak commentedon May 31, 2022
Interesting 😃
Would anyone be interested in working on a pull request by any chance? 😁
ameryousuf commentedon Dec 28, 2022
@gkalpak Can I work on this issue?
JeanMeche commentedon Aug 29, 2025
With #63364 we've announced that the service-worker package in now in maintenance mode and will not receive feature PRs and feature requests.
The message is also visible here: https://angular.dev/ecosystem/service-workers
angular-automatic-lock-bot commentedon Sep 29, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.