Skip to content

service-worker: configure response status codes to treat as failed/offline with freshness strategy #45954

Closed as not planned

Description

@anton-marchenko

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

Anything else?

https://angular.io/guide/service-worker-config#datagroups

https://stackoverflow.com/questions/69132411/angular-service-worker-return-cached-response-if-server-returns-error

Activity

  1. 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
  2. added this to the needsTriage milestone on May 11, 2022
  3. gkalpak commented on May 12, 2022

    @gkalpak
    Member

    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 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.

  4. 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
  5. added
    featureLabel used to distinguish feature request from other issues
    on May 12, 2022
  6. modified the milestones: needsTriage, Backlog on May 12, 2022
  7. angular-robot commented on May 13, 2022

    @angular-robot
    Contributor

    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.

  8. anton-marchenko commented on May 16, 2022

    @anton-marchenko
    Author

    @gkalpak
    As I understand from documentation, voters should just set 👍 for the topic post. Is it correct?

    image

  9. gkalpak commented on May 16, 2022

    @gkalpak
    Member

    Yes, that's right, @anton-marchenko.

  10. added
    feature: under considerationFeature request for which voting has completed and the request is now under consideration
    and removed on May 24, 2022
  11. anton-marchenko commented on May 31, 2022

    @anton-marchenko
    Author

    @gkalpak
    Seems like we have 20+ upvotes.

    Could you move the request to your consideration list?

  12. gkalpak commented on May 31, 2022

    @gkalpak
    Member

    Interesting 😃
    Would anyone be interested in working on a pull request by any chance? 😁

  13. ameryousuf commented on Dec 28, 2022

    @ameryousuf
    Contributor

    @gkalpak Can I work on this issue?

  14. JeanMeche commented on Aug 29, 2025

    @JeanMeche
    Member

    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

  15. angular-automatic-lock-bot commented on Sep 29, 2025

    @angular-automatic-lock-bot

    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.

  16. locked and limited conversation to collaborators on Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: service-workerIssues related to the @angular/service-worker packagefeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under consideration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions