Skip to content

Commit 01ec5fd

Browse files
alan-agius4benlesh
authored andcommitted
fix(service-worker): Cache-Control: no-cache on assets breaks service worker (angular#25408)
At the moment `cacheAge` can we undefined when having `Cache-Control` set to `no-cache` due the mapping method in `needToRevalidate` Closes angular#25442 PR Close angular#25408
1 parent be2cf4d commit 01ec5fd

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ export abstract class AssetGroup {
193193
cacheDirectives.forEach(v => v[0] = v[0].toLowerCase());
194194

195195
// Find the max-age directive, if one exists.
196-
const cacheAge = cacheDirectives.filter(v => v[0] === 'max-age').map(v => v[1])[0];
196+
const maxAgeDirective = cacheDirectives.find(v => v[0] === 'max-age');
197+
const cacheAge = maxAgeDirective ? maxAgeDirective[1] : undefined;
197198

198-
if (cacheAge.length === 0) {
199+
if (!cacheAge) {
199200
// No usable TTL defined. Must assume that the response is stale.
200201
return true;
201202
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const dist =
2828
.addFile('/lazy/unchanged1.txt', 'this is unchanged (1)')
2929
.addFile('/lazy/unchanged2.txt', 'this is unchanged (2)')
3030
.addUnhashedFile('/unhashed/a.txt', 'this is unhashed', {'Cache-Control': 'max-age=10'})
31+
.addUnhashedFile('/unhashed/b.txt', 'this is unhashed b', {'Cache-Control': 'no-cache'})
3132
.build();
3233

3334
const distUpdate =
@@ -621,6 +622,13 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
621622
server.assertNoOtherRequests();
622623
});
623624

625+
async_it(`doesn't error when 'Cache-Control' is 'no-cache'`, async() => {
626+
expect(await makeRequest(scope, '/unhashed/b.txt')).toEqual('this is unhashed b');
627+
server.assertSawRequestFor('/unhashed/b.txt');
628+
expect(await makeRequest(scope, '/unhashed/b.txt')).toEqual('this is unhashed b');
629+
server.assertNoOtherRequests();
630+
});
631+
624632
async_it('avoid opaque responses', async() => {
625633
expect(await makeRequest(scope, '/unhashed/a.txt', 'default', {
626634
credentials: 'include'

0 commit comments

Comments
 (0)