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
@angular/pwa doesn't clean up previous service worker version's cached content #41728
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
Setup new CLI project ng new my-pwa-app
Add PWA support ng add @angular/pwa --project my-pwa-app
Build production version of application and run locally (observe cache size)
Make small (non-functional) change to the application (just to change file hashing) - representing a new version of your application being deployed.
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.
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
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.)
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 😱 😇
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).
Activity
[-]@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[/+]gkalpak commentedon Apr 20, 2021
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 WorkersDevTools panel and pressing thestopbutton 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.)
gkalpak commentedon Apr 28, 2021
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...
jessicajaniuk commentedon Apr 30, 2021
Thanks @gkalpak. Can you elaborate on these rare conditions? How rare are they?
gkalpak commentedon Jun 22, 2021
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).
fix(service-worker): ensure obsolete caches are always cleaned up
angular-automatic-lock-bot commentedon Jul 25, 2021
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.