Skip to content

Commit 96cdf03

Browse files
alexwiesemhevery
authored andcommitted
fix(service-worker): treat 503 as offline (angular#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 angular#35571 PR Close angular#35595
1 parent 24f32e3 commit 96cdf03

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/service-worker/worker/src/driver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ export class Driver implements Debuggable, UpdateSource {
684684
/**
685685
* Retrieve a copy of the latest manifest from the server.
686686
* Return `null` if `ignoreOfflineError` is true (default: false) and the server or client are
687-
* offline (detected as response status 504).
687+
* offline (detected as response status 503 (service unavailable) or 504 (gateway timeout)).
688688
*/
689689
private async fetchLatestManifest(ignoreOfflineError?: false): Promise<Manifest>;
690690
private async fetchLatestManifest(ignoreOfflineError: true): Promise<Manifest|null>;
@@ -695,7 +695,7 @@ export class Driver implements Debuggable, UpdateSource {
695695
if (res.status === 404) {
696696
await this.deleteAllCaches();
697697
await this.scope.registration.unregister();
698-
} else if (res.status === 504 && ignoreOfflineError) {
698+
} else if ((res.status === 503 || res.status === 504) && ignoreOfflineError) {
699699
return null;
700700
}
701701
throw new Error(`Manifest fetch failed! (status: ${res.status})`);

packages/service-worker/worker/test/happy_spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {Driver, DriverReadyState} from '../src/driver';
1212
import {AssetGroupConfig, DataGroupConfig, Manifest} from '../src/manifest';
1313
import {sha1} from '../src/sha1';
1414
import {MockCache, clearAllCaches} from '../testing/cache';
15-
import {MockRequest} from '../testing/fetch';
15+
import {MockRequest, MockResponse} from '../testing/fetch';
1616
import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTableForFs} from '../testing/mock';
1717
import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
1818

@@ -872,6 +872,21 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
872872
expect(await scope.caches.keys()).not.toEqual([]);
873873
});
874874

875+
it('does not unregister or change state when status code is 503 (service unavailable)',
876+
async() => {
877+
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
878+
await driver.initialized;
879+
spyOn(server, 'fetch').and.callFake((req: Request) => new MockResponse(null, {
880+
status: 503,
881+
statusText: 'Service Unavailable'
882+
}));
883+
884+
expect(await driver.checkForUpdate()).toEqual(false);
885+
expect(driver.state).toEqual(DriverReadyState.NORMAL);
886+
expect(scope.unregistered).toBeFalsy();
887+
expect(await scope.caches.keys()).not.toEqual([]);
888+
});
889+
875890
describe('cache naming', () => {
876891
// Helpers
877892
const cacheKeysFor = (baseHref: string) =>

0 commit comments

Comments
 (0)