From 01128f5b5d7e6cbca8f4844672b51565c19ba8e7 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Wed, 23 Jun 2021 15:27:51 +0300 Subject: [PATCH] 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 --- packages/service-worker/worker/src/driver.ts | 18 +++++++++++------- .../service-worker/worker/test/happy_spec.ts | 3 ++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/service-worker/worker/src/driver.ts b/packages/service-worker/worker/src/driver.ts index 895fed51b63..ea3f0798abb 100644 --- a/packages/service-worker/worker/src/driver.ts +++ b/packages/service-worker/worker/src/driver.ts @@ -542,14 +542,8 @@ export class Driver implements Debuggable, UpdateSource { // Successfully loaded from saved state. This implies a manifest exists, so // 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(); - try { - await this.cleanupCaches(); - } catch (err) { - // Nothing to do - cleanup failed. Just log it. - this.debugger.log(err, 'cleanupCaches @ init post-load'); - } }); } catch (_) { // 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` // 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 // for that manifest. Object.keys(manifests).forEach((hash: ManifestHash) => { diff --git a/packages/service-worker/worker/test/happy_spec.ts b/packages/service-worker/worker/test/happy_spec.ts index 21955a5de40..7e15572fc8e 100644 --- a/packages/service-worker/worker/test/happy_spec.ts +++ b/packages/service-worker/worker/test/happy_spec.ts @@ -402,7 +402,8 @@ describe('Driver', () => { expect(driver['latestHash']).toBeNull(); // 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)); server.assertSawRequestFor('/ngsw.json'); server.assertSawRequestFor('/foo.txt');