-
Notifications
You must be signed in to change notification settings - Fork 26.5k
fix(service-worker): improvements/fixes regarding ServiceWorker caches #42622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
818b56d
to
5eb1a5b
Compare
You can preview 5eb1a5b at https://pr42622-5eb1a5b.ngbuilds.io/. |
5eb1a5b
to
2159d08
Compare
You can preview 2159d08 at https://pr42622-2159d08.ngbuilds.io/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall, I just left a few comments. The most important one is related to a possible typo in the NamedCacheStorage.has
code.
@gkalpak FYI I was going through the commits one-by-one, so some of the comments are marked as "outdated" in GitHub UI, feel free to ignore them if they are not relevant and/or already resolved. |
3631031
to
d0ddf1b
Compare
Thx for the review, @AndrewKushnir ✨ |
You can preview d0ddf1b at https://pr42622-d0ddf1b.ngbuilds.io/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
…aintainability This commit makes some minor refactorings to improve the code readability and maintainability, including: - Avoiding code duplication. - Using more descriptive variable names. - Using `async/await` instead of `Promise#then()`. - Accessing variables directly instead of via `this` when possible.
This commit includes the ServiceWorker version in the debug info shown at `/ngsw/state` to make it easier to know what version of the ServiceWorker script is controlling the page.
…ables `CacheDatabase` uses the un-prefixed table names to interact with database tables. However, the `list()` method returns the raw, prefixed table names (which are not useful, since they cannot be used to open/delete a table). This commit fixes this by removing the prefix from the cache names returned by the `list()` method. NOTE: This method is currently not used anywhere, so this change does not affect the ServiceWorker behavior.
This commit improves the cache names generated by the ServiceWorker by making them shorter and non-repetitive. In particular, the following changes are made: - Data-group cache names no longer include the `dynamic` infix, since it does not add any value. Before: `ngsw:<...>:data:dynamic:<...>` After: `ngsw:<...>:data:<...>` - `CacheDatabase` table names no longer include the `ngsw:<path>` prefix twice. Before: `ngsw:<path>:db:ngsw:<path>:<...>` After: `ngsw:<path>:db:<...>` NOTE 1: This change will result in different cache names being generated for the same app-versions with the new SericeWorker script. This means that some of the previously cached data will need to be re-downloaded (because the ServiceWorker will not be able to re-use the old caches), but that should be transparent for the end user. While possible, adding logic to allow the ServiceWorker to retrieve data from the old caches is not worth the extra complecity and maintenance cost. NOTE 2: Generating different cache names for some of the caches means that the ServiceWorker will not be able to clean-up some of the old caches. This will be taken care of in a subsequent commit that will rework the clean-up logic to be more robust (covering changes such as this one and other edgecases).
…t the ServiceWorker This commit simplifies/systemizes accessing the `CacheStorage` through a wrapper, with the following benefits: - Ensuring a consistent cache name prefix is used for all caches (without having to repeat the prefix in different places). - Allowing referring to caches using their name without the common cache name prefix. - Exposing the cache name on cache instances, which for example makes it easier to delete caches without having to keep track of the name used to create them.
…roughout the ServiceWorker
…d state Previously, obsolete caches were only cleaned up when successfully loading the stored state. When the state failed to be loaded, cleaning up the caches would be skipped until the next SW initialization. This commit changes this, ensuring that the caches are cleaned up regardless if the stored state was loaded successfully or not.
Previously, the SW was only able to clean up caches for app-versions found in the `Driver`'s `versions` map. If (for some reason) the `Driver` failed to load a valid stored state (including app-versions) and ended up with an [empty `versions` map][1], any obsolete versions would remain in the cache storage. This case was rare but possible. This commit makes the cache clean-up logic more robust by ensuring that all app-version caches are removed unless they are currently used by the SW to serve active clients (with the exception of the latest app-version, which is always retained). Fixes angular#41728 [1]: https://github.com/angular/angular/blob/9de65dbdceac3077881fbc49717f33d0f379e21d/packages/service-worker/worker/src/driver.ts#L515-L529
d0ddf1b
to
612ba53
Compare
You can preview 612ba53 at https://pr42622-612ba53.ngbuilds.io/. |
…ables (#42622) `CacheDatabase` uses the un-prefixed table names to interact with database tables. However, the `list()` method returns the raw, prefixed table names (which are not useful, since they cannot be used to open/delete a table). This commit fixes this by removing the prefix from the cache names returned by the `list()` method. NOTE: This method is currently not used anywhere, so this change does not affect the ServiceWorker behavior. PR Close #42622
This commit improves the cache names generated by the ServiceWorker by making them shorter and non-repetitive. In particular, the following changes are made: - Data-group cache names no longer include the `dynamic` infix, since it does not add any value. Before: `ngsw:<...>:data:dynamic:<...>` After: `ngsw:<...>:data:<...>` - `CacheDatabase` table names no longer include the `ngsw:<path>` prefix twice. Before: `ngsw:<path>:db:ngsw:<path>:<...>` After: `ngsw:<path>:db:<...>` NOTE 1: This change will result in different cache names being generated for the same app-versions with the new SericeWorker script. This means that some of the previously cached data will need to be re-downloaded (because the ServiceWorker will not be able to re-use the old caches), but that should be transparent for the end user. While possible, adding logic to allow the ServiceWorker to retrieve data from the old caches is not worth the extra complecity and maintenance cost. NOTE 2: Generating different cache names for some of the caches means that the ServiceWorker will not be able to clean-up some of the old caches. This will be taken care of in a subsequent commit that will rework the clean-up logic to be more robust (covering changes such as this one and other edgecases). PR Close #42622
…t the ServiceWorker (#42622) This commit simplifies/systemizes accessing the `CacheStorage` through a wrapper, with the following benefits: - Ensuring a consistent cache name prefix is used for all caches (without having to repeat the prefix in different places). - Allowing referring to caches using their name without the common cache name prefix. - Exposing the cache name on cache instances, which for example makes it easier to delete caches without having to keep track of the name used to create them. PR Close #42622
…d state (#42622) Previously, obsolete caches were only cleaned up when successfully loading the stored state. When the state failed to be loaded, cleaning up the caches would be skipped until the next SW initialization. This commit changes this, ensuring that the caches are cleaned up regardless if the stored state was loaded successfully or not. PR Close #42622
…2622) Previously, the SW was only able to clean up caches for app-versions found in the `Driver`'s `versions` map. If (for some reason) the `Driver` failed to load a valid stored state (including app-versions) and ended up with an [empty `versions` map][1], any obsolete versions would remain in the cache storage. This case was rare but possible. This commit makes the cache clean-up logic more robust by ensuring that all app-version caches are removed unless they are currently used by the SW to serve active clients (with the exception of the latest app-version, which is always retained). Fixes #41728 [1]: https://github.com/angular/angular/blob/9de65dbdceac3077881fbc49717f33d0f379e21d/packages/service-worker/worker/src/driver.ts#L515-L529 PR Close #42622
This should also have fixed #20785. |
Update `@angular/*` packages to latest 12.1.x versions (mainly to take advantage of recent ServiceWorker improvements, such as angular#42607 and angular#42622).
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Several improvements and fixes regarding the ServiceWorker caches (naming, accessing, clean-up).
See individual commits for details.
Fixes #41728.
NOTE: Theoretically, this could land on patch as well. But since it changes some of the SW caches names, let's be conservative and land in a minor release.