Skip to content

Cancelling requests with service worker #48107

Closed as not planned
Closed as not planned

Description

@oliveti

Which @angular/* package(s) are the source of the bug?

service-worker

Is this a regression?

No

Description

When using a service worker and cancelling http requests (via unsubscribe for example), the underlying call made by service worker is not cancelled.

This could cause several issues like reaching maximum number of connections opened to a backend or useless backend load.

To reproduce the issue you can use the project I shared in github which is triggering calls to a slow backend and cancelling the calls except the last one (with and without service worker). You can see that, when using service worker, that last call have to wait for all the "cancelled" calls to be done before being fired.

I attach two screenshots of the demo code you can find in the repository.

Screenshot 2022-11-17 at 15 01 43

Screenshot 2022-11-17 at 15 02 16

Please provide a link to a minimal reproduction of the bug

https://github.com/oliveti/sw-cancellation-demo-2

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.2.9
Node: 16.14.0
Package Manager: npm 9.1.2 
OS: darwin arm64

Angular: 14.2.11
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
... service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.9
@angular-devkit/build-angular   14.2.9
@angular-devkit/core            14.2.9
@angular-devkit/schematics      14.2.9
@angular/cli                    14.2.9
@angular/pwa                    0.5.3
@schematics/angular             14.2.9
rxjs                            7.5.7
typescript                      4.7.4

Anything else?

No response

Activity

  1. added this to the needsTriage milestone on Nov 17, 2022
  2. frankvdb7 commented on Mar 23, 2023

    @frankvdb7

    We're also experiencing this issue. It is very simple to reproduce.

    1. Create logic to do 20+ rxjs http calls to an api that does not return anything
    2. Add timeout rxjs operator on like for example 1 sec
    3. The timeout will cancel the original http request
    4. The service worker does a fetch request for each original http request
    5. The fetch requests have timeouts of by default 300 seconds and do not notice the original request being cancelled.
    6. This leads to 20+ pending requests, which does not allow the website to make any more requests
  3. mhosman commented on Jul 19, 2023

    @mhosman

    I'm facing the same issue. Any workaround?

  4. michaelmarcuccio commented on Jul 19, 2023

    @michaelmarcuccio

    I'm facing the same issue. Any workaround?

    Bypass the service worker for all calls that need to be cancellable. That is the only way I've found to be able to do it.

     private serviceWorkerGlobalBypass() {
        const originalOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function (method, url) {
          originalOpen.apply(this, [method, url]);
    //bypass all put requests
          if (method === 'PUT') {
              this.setRequestHeader('ngsw-bypass', 'true');
          }
        };
      }
    
  5. 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

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

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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions