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');