Skip to content

@angular/pwa doesn't clean up previous service worker version's cached content #41728

Closed

Description

@rustygreen

🐞 bug report

Affected Package

The issue is caused by package @angular/pwa

Is this a regression?

No

Description

I am noticing that with every new version of my application the cache size grows considerably because the Angular Service worker is keeping the previous versions of the application in cache? This means that everytime you release a new version of your application, the new version's resources will be cached for offline use (the expected behavior). However, all of the cached content (scripts, css, HTML, etc) from the previous versions of the application are still stored in cache. This means that the application's cache will grow exponentially with every release of your application.

🔬 Minimal Reproduction

  1. Setup new CLI project ng new my-pwa-app
  2. Add PWA support ng add @angular/pwa --project my-pwa-app
  3. Build production version of application and run locally (observe cache size)
  4. Make small (non-functional) change to the application (just to change file hashing) - representing a new version of your application being deployed.
  5. Build production version of the application and run locally again (NOTE: the cache size will be double what it was before). Repeat steps 4 and 5 and this behavior will continue (cache size will continue to grow, because it is holding onto the previous version's cached files).

Application cache storage (same content, different version):
Version 1: 12.2 MB
Version 2: 23.5 MB
Version 3: 32.9 MB
...etc.

🌍 Your Environment

Angular Version: 11
image

Activity

  1. changed the title [-]@angular/pwa doesn't clean up previous service worker version's cached resources[/-] [+]@angular/pwa doesn't clean up previous service worker version's cached content[/+] on Apr 20, 2021
  2. gkalpak commented on Apr 20, 2021

    @gkalpak
    Member

    The SW does clean up the unused caches, but it does so when it is initialized - i.e. when a new instance of the SW is created. After some time of inactivity, the browser will stop the SW and it will create a new instance of the SW when it is needed again.

    You typically don't see this behavior when following the reproduction steps you mentioned above, because you deploy new versions before the SW is stopped by the browser. You can simulate this (to see the caches being cleaned up) by opening the Application > Service Workers DevTools panel and pressing the stop button next to the active SW. (The next time the SW is started - e.g. when reloading the page - it will clean up any unused caches.)

    While I think the current behavior is sufficient to ensure caches are cleaned up regularly (except in very extreme scenarios, where you deploy multiple versions is short amount of time), I am not opposed to cleaning up more frequently. But I wouldn't want to have the SW check too frequently (for example, on every request), since that sounds wasteful.

    I am going to close this, since the caches are cleaned up and the current behavior in intended, but if anyone wants to propose or work on a specific change regarding when caches are cleaned-up, I would be happy to discuss on a new issue (or review a PR 😉).

    (Also, feel free to continue the discussion below, if anything isn't clear or I've missed something.)

  3. gkalpak commented on Apr 28, 2021

    @gkalpak
    Member

    Actually...I was investigating another issue today and found out that the SW can indeed (under certain rare conditions) leave behind some caches that will never get cleaned up 😱 😇

    Reopening...

  4. added
    area: service-worker
    on Apr 28, 2021
  5. added this to the needsTriage milestone on Apr 28, 2021
  6. added
    P4
    on Apr 30, 2021
  7. modified the milestones: needsTriage, Backlog on Apr 30, 2021
  8. jessicajaniuk commented on Apr 30, 2021

    @jessicajaniuk
    Contributor

    Thanks @gkalpak. Can you elaborate on these rare conditions? How rare are they?

  9. gkalpak commented on Jun 22, 2021

    @gkalpak
    Member

    Sorry, I missed your comment, @jessicajaniuk. I should totally have posted more details on my investigation 😅
    (I was planning on quickly following up with a fix, but got caught up in other stuff 😇)

    I have submitted a fix in #42622 (along with other improvements).

  10. added a commit that references this issue on Jun 24, 2021
    d525746
  11. angular-automatic-lock-bot commented on Jul 25, 2021

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

  12. locked and limited conversation to collaborators on Jul 25, 2021
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

    P4area: service-worker

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions