From 96cdf035d8118a23ca1dc4ca713ad02f93cde8fa Mon Sep 17 00:00:00 2001 From: Alex Wiese Date: Mon, 24 Feb 2020 12:04:22 +0000 Subject: [PATCH] fix(service-worker): treat 503 as offline (#35595) Prior to this commit the service worker only treated 504 errors as "effectively offline". This commit changes the behaviour to treat both 503 (Service Unavailable) and 504 as "offline". Fixes #35571 PR Close #35595 --- packages/service-worker/worker/src/driver.ts | 4 ++-- .../service-worker/worker/test/happy_spec.ts | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/service-worker/worker/src/driver.ts b/packages/service-worker/worker/src/driver.ts index 2091bd8d95..fce947b436 100644 --- a/packages/service-worker/worker/src/driver.ts +++ b/packages/service-worker/worker/src/driver.ts @@ -684,7 +684,7 @@ export class Driver implements Debuggable, UpdateSource { /** * Retrieve a copy of the latest manifest from the server. * Return `null` if `ignoreOfflineError` is true (default: false) and the server or client are - * offline (detected as response status 504). + * offline (detected as response status 503 (service unavailable) or 504 (gateway timeout)). */ private async fetchLatestManifest(ignoreOfflineError?: false): Promise; private async fetchLatestManifest(ignoreOfflineError: true): Promise; @@ -695,7 +695,7 @@ export class Driver implements Debuggable, UpdateSource { if (res.status === 404) { await this.deleteAllCaches(); await this.scope.registration.unregister(); - } else if (res.status === 504 && ignoreOfflineError) { + } else if ((res.status === 503 || res.status === 504) && ignoreOfflineError) { return null; } throw new Error(`Manifest fetch failed! (status: ${res.status})`); diff --git a/packages/service-worker/worker/test/happy_spec.ts b/packages/service-worker/worker/test/happy_spec.ts index 2c990cc3a2..adc674bda9 100644 --- a/packages/service-worker/worker/test/happy_spec.ts +++ b/packages/service-worker/worker/test/happy_spec.ts @@ -12,7 +12,7 @@ import {Driver, DriverReadyState} from '../src/driver'; import {AssetGroupConfig, DataGroupConfig, Manifest} from '../src/manifest'; import {sha1} from '../src/sha1'; import {MockCache, clearAllCaches} from '../testing/cache'; -import {MockRequest} from '../testing/fetch'; +import {MockRequest, MockResponse} from '../testing/fetch'; import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTableForFs} from '../testing/mock'; import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope'; @@ -872,6 +872,21 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope'; expect(await scope.caches.keys()).not.toEqual([]); }); + it('does not unregister or change state when status code is 503 (service unavailable)', + async() => { + expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo'); + await driver.initialized; + spyOn(server, 'fetch').and.callFake((req: Request) => new MockResponse(null, { + status: 503, + statusText: 'Service Unavailable' + })); + + expect(await driver.checkForUpdate()).toEqual(false); + expect(driver.state).toEqual(DriverReadyState.NORMAL); + expect(scope.unregistered).toBeFalsy(); + expect(await scope.caches.keys()).not.toEqual([]); + }); + describe('cache naming', () => { // Helpers const cacheKeysFor = (baseHref: string) =>