fix(service-worker): ensure caches are cleaned up when failing to load 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
This commit is contained in:
George Kalpakas 2021-06-23 15:27:51 +03:00 committed by Jessica Janiuk
parent 356dd2107b
commit 01128f5b5d
2 changed files with 13 additions and 8 deletions

View File

@ -542,14 +542,8 @@ export class Driver implements Debuggable, UpdateSource {
// Successfully loaded from saved state. This implies a manifest exists, so // Successfully loaded from saved state. This implies a manifest exists, so
// the update check needs to happen in the background. // the update check needs to happen in the background.
this.idle.schedule('init post-load (update, cleanup)', async () => { this.idle.schedule('init post-load (update)', async () => {
await this.checkForUpdate(); await this.checkForUpdate();
try {
await this.cleanupCaches();
} catch (err) {
// Nothing to do - cleanup failed. Just log it.
this.debugger.log(err, 'cleanupCaches @ init post-load');
}
}); });
} catch (_) { } catch (_) {
// Something went wrong. Try to start over by fetching a new manifest from the // Something went wrong. Try to start over by fetching a new manifest from the
@ -572,6 +566,16 @@ export class Driver implements Debuggable, UpdateSource {
// with a new copy of the manifest has been produced. At this point, the `Driver` // with a new copy of the manifest has been produced. At this point, the `Driver`
// can have its internals hydrated from the state. // can have its internals hydrated from the state.
// Schedule cleaning up obsolete caches in the background.
this.idle.schedule('init post-load (cleanup)', async () => {
try {
await this.cleanupCaches();
} catch (err) {
// Nothing to do - cleanup failed. Just log it.
this.debugger.log(err, 'cleanupCaches @ init post-load');
}
});
// Initialize the `versions` map by setting each hash to a new `AppVersion` instance // Initialize the `versions` map by setting each hash to a new `AppVersion` instance
// for that manifest. // for that manifest.
Object.keys(manifests).forEach((hash: ManifestHash) => { Object.keys(manifests).forEach((hash: ManifestHash) => {

View File

@ -402,7 +402,8 @@ describe('Driver', () => {
expect(driver['latestHash']).toBeNull(); expect(driver['latestHash']).toBeNull();
// Pushing a message initializes the driver (fetches assets). // Pushing a message initializes the driver (fetches assets).
await scope.handleMessage({action: 'foo'}, 'someClient'); scope.handleMessage({action: 'foo'}, 'someClient');
await new Promise(resolve => setTimeout(resolve)); // Wait for async operations to complete.
expect(driver['latestHash']).toEqual(jasmine.any(String)); expect(driver['latestHash']).toEqual(jasmine.any(String));
server.assertSawRequestFor('/ngsw.json'); server.assertSawRequestFor('/ngsw.json');
server.assertSawRequestFor('/foo.txt'); server.assertSawRequestFor('/foo.txt');