Skip to content

Commit 93abc35

Browse files
gkalpakalxhub
authored andcommittedJun 27, 2019
fix(service-worker): cache opaque responses when requests exceeds timeout threshold (#30977)
PR Close #30977
1 parent d7be38f commit 93abc35

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed
 

‎packages/service-worker/worker/src/data.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ export class DataGroup {
378378

379379
// If the network fetch times out or errors, fall back on the cache.
380380
if (res === undefined) {
381-
ctx.waitUntil(this.safeCacheResponse(req, networkFetch));
381+
ctx.waitUntil(this.safeCacheResponse(req, networkFetch, true));
382382

383383
// Ignore the age, the network response will be cached anyway due to the
384384
// behavior of freshness.
@@ -434,9 +434,10 @@ export class DataGroup {
434434
}
435435
}
436436

437-
private async safeCacheResponse(req: Request, res: Promise<Response>): Promise<void> {
437+
private async safeCacheResponse(req: Request, res: Promise<Response>, okToCacheOpaque?: boolean):
438+
Promise<void> {
438439
try {
439-
await this.cacheResponse(req, await res, await this.lru());
440+
await this.cacheResponse(req, await res, await this.lru(), okToCacheOpaque);
440441
} catch {
441442
// TODO: handle this error somehow?
442443
}

‎packages/service-worker/worker/test/data_spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,36 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
259259
expect(await makeRequest(scope, '/refresh/data')).toEqual('this is refreshed data');
260260
serverUpdate.assertNoOtherRequests();
261261
});
262+
263+
it('caches opaque responses on refresh', async() => {
264+
// Make the initial request and populate the cache.
265+
expect(await makeRequest(scope, '/fresh/data')).toBe('this is fresh data');
266+
server.assertSawRequestFor('/fresh/data');
267+
server.clearRequests();
268+
269+
// Update the server state and pause the server, so the next request times out.
270+
scope.updateServerState(serverUpdate);
271+
serverUpdate.pause();
272+
const [res, done] =
273+
makePendingRequest(scope, new MockRequest('/fresh/data', {mode: 'no-cors'}));
274+
275+
// The network request times out after 1,000ms and the cached response is returned.
276+
await serverUpdate.nextRequest;
277+
scope.advance(2000);
278+
expect(await res).toBe('this is fresh data');
279+
280+
// Unpause the server to allow the network request to complete and be cached.
281+
serverUpdate.unpause();
282+
await done;
283+
284+
// Pause the server to force the cached (opaque) response to be returned.
285+
serverUpdate.pause();
286+
const [res2] = makePendingRequest(scope, '/fresh/data');
287+
await serverUpdate.nextRequest;
288+
scope.advance(2000);
289+
290+
expect(await res2).toBe('');
291+
});
262292
});
263293
});
264294
})();

0 commit comments

Comments
 (0)