From f582620d5b0787064f17a1521f55142d090845f0 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Thu, 30 Nov 2017 09:51:09 -0800 Subject: [PATCH] fix(service-worker): use relative path for ngsw.json Not every application is served from the domain root. The Service Worker made a bad assumption that it would be, and so requested /ngsw.json from the domain root. This change corrects this assumption, and requests ngsw.json without the leading slash. This causes the request to be interpreted relative to the SW origin, which will be the application root. --- packages/service-worker/worker/src/driver.ts | 4 ++-- packages/service-worker/worker/test/happy_spec.ts | 10 +++++----- packages/service-worker/worker/testing/mock.ts | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/service-worker/worker/src/driver.ts b/packages/service-worker/worker/src/driver.ts index a80b69ede5..6d2f229190 100644 --- a/packages/service-worker/worker/src/driver.ts +++ b/packages/service-worker/worker/src/driver.ts @@ -580,8 +580,8 @@ export class Driver implements Debuggable, UpdateSource { * Retrieve a copy of the latest manifest from the server. */ private async fetchLatestManifest(): Promise { - const res = await this.safeFetch( - this.adapter.newRequest('/ngsw.json?ngsw-cache-bust=' + Math.random())); + const res = + await this.safeFetch(this.adapter.newRequest('ngsw.json?ngsw-cache-bust=' + Math.random())); if (!res.ok) { if (res.status === 404) { await this.deleteAllCaches(); diff --git a/packages/service-worker/worker/test/happy_spec.ts b/packages/service-worker/worker/test/happy_spec.ts index 5599af47cc..14b404926b 100644 --- a/packages/service-worker/worker/test/happy_spec.ts +++ b/packages/service-worker/worker/test/happy_spec.ts @@ -158,7 +158,7 @@ export function main() { expect(await scope.startup(true)).toEqual(true); await scope.resolveSelfMessages(); await driver.initialized; - server.assertSawRequestFor('/ngsw.json'); + server.assertSawRequestFor('ngsw.json'); server.assertSawRequestFor('/foo.txt'); server.assertSawRequestFor('/bar.txt'); server.assertSawRequestFor('/redirected.txt'); @@ -170,7 +170,7 @@ export function main() { async_it('initializes prefetched content correctly, after a request kicks it off', async() => { expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo'); await driver.initialized; - server.assertSawRequestFor('/ngsw.json'); + server.assertSawRequestFor('ngsw.json'); server.assertSawRequestFor('/foo.txt'); server.assertSawRequestFor('/bar.txt'); server.assertSawRequestFor('/redirected.txt'); @@ -230,7 +230,7 @@ export function main() { scope.updateServerState(serverUpdate); expect(await driver.checkForUpdate()).toEqual(true); - serverUpdate.assertSawRequestFor('/ngsw.json'); + serverUpdate.assertSawRequestFor('ngsw.json'); serverUpdate.assertSawRequestFor('/foo.txt'); serverUpdate.assertSawRequestFor('/redirected.txt'); serverUpdate.assertNoOtherRequests(); @@ -263,7 +263,7 @@ export function main() { scope.updateServerState(serverUpdate); expect(await driver.checkForUpdate()).toEqual(true); - serverUpdate.assertSawRequestFor('/ngsw.json'); + serverUpdate.assertSawRequestFor('ngsw.json'); serverUpdate.assertSawRequestFor('/foo.txt'); serverUpdate.assertSawRequestFor('/redirected.txt'); serverUpdate.assertNoOtherRequests(); @@ -331,7 +331,7 @@ export function main() { scope.advance(12000); await driver.idle.empty; - serverUpdate.assertSawRequestFor('/ngsw.json'); + serverUpdate.assertSawRequestFor('ngsw.json'); serverUpdate.assertSawRequestFor('/foo.txt'); serverUpdate.assertSawRequestFor('/redirected.txt'); serverUpdate.assertNoOtherRequests(); diff --git a/packages/service-worker/worker/testing/mock.ts b/packages/service-worker/worker/testing/mock.ts index 52100a628c..7ec168841d 100644 --- a/packages/service-worker/worker/testing/mock.ts +++ b/packages/service-worker/worker/testing/mock.ts @@ -73,7 +73,7 @@ export class MockServerStateBuilder { } withManifest(manifest: Manifest): MockServerStateBuilder { - this.resources.set('/ngsw.json', new MockResponse(JSON.stringify(manifest))); + this.resources.set('ngsw.json', new MockResponse(JSON.stringify(manifest))); return this; }