Skip to content

Commit d7be38f

Browse files
gkalpakalxhub
authored andcommittedJun 27, 2019
fix(service-worker): cache opaque responses in data groups with freshness strategy (#30977)
Previously, (presummably due to a typo) the `okToCacheOpaque` argument of `DataGroup#cacheResponse()` was essentially never taken into account (since opaque responses have a non-200 status code and thus `res.ok` is always false). This commit fixes the typo, which allows opaque responses to be cached when `okToCacheOpaque` is true (i.e. in data groups using the `freshness` strategy). Fixes #30968 PR Close #30977
1 parent 2d38623 commit d7be38f

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed
 

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,10 @@ export class DataGroup {
481481
* If the request times out on the server, an error will be returned but the real network
482482
* request will still be running in the background, to be cached when it completes.
483483
*/
484-
private async cacheResponse(
485-
req: Request, res: Response, lru: LruList, okToCacheOpaque: boolean = false): Promise<void> {
484+
private async cacheResponse(req: Request, res: Response, lru: LruList, okToCacheOpaque = false):
485+
Promise<void> {
486486
// Only cache successful responses.
487-
if (!res.ok || (okToCacheOpaque && res.type === 'opaque')) {
487+
if (!(res.ok || (okToCacheOpaque && res.type === 'opaque'))) {
488488
return;
489489
}
490490

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
150150
server.assertNoOtherRequests();
151151
});
152152

153+
it('does not cache opaque responses', async() => {
154+
expect(await makeNoCorsRequest(scope, '/api/test')).toBe('');
155+
server.assertSawRequestFor('/api/test');
156+
157+
expect(await makeNoCorsRequest(scope, '/api/test')).toBe('');
158+
server.assertSawRequestFor('/api/test');
159+
});
160+
153161
it('refreshes after awhile', async() => {
154162
expect(await makeRequest(scope, '/api/test')).toEqual('version 1');
155163
server.clearRequests();
@@ -199,6 +207,16 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
199207
serverUpdate.assertNoOtherRequests();
200208
});
201209

210+
it('caches opaque responses', async() => {
211+
expect(await makeNoCorsRequest(scope, '/fresh/data')).toBe('');
212+
server.assertSawRequestFor('/fresh/data');
213+
214+
server.online = false;
215+
216+
expect(await makeRequest(scope, '/fresh/data')).toBe('');
217+
server.assertNoOtherRequests();
218+
});
219+
202220
it('falls back on the cache when server times out', async() => {
203221
expect(await makeRequest(scope, '/fresh/data')).toEqual('this is fresh data');
204222
server.assertSawRequestFor('/fresh/data');
@@ -250,9 +268,18 @@ function makeRequest(scope: SwTestHarness, url: string, clientId?: string): Prom
250268
return done.then(() => resTextPromise);
251269
}
252270

271+
function makeNoCorsRequest(
272+
scope: SwTestHarness, url: string, clientId?: string): Promise<String|null> {
273+
const req = new MockRequest(url, {mode: 'no-cors'});
274+
const [resTextPromise, done] = makePendingRequest(scope, req, clientId);
275+
return done.then(() => resTextPromise);
276+
}
277+
253278
function makePendingRequest(
254-
scope: SwTestHarness, url: string, clientId?: string): [Promise<string|null>, Promise<void>] {
255-
const [resPromise, done] = scope.handleFetch(new MockRequest(url), clientId || 'default');
279+
scope: SwTestHarness, urlOrReq: string | MockRequest,
280+
clientId?: string): [Promise<string|null>, Promise<void>] {
281+
const req = (typeof urlOrReq === 'string') ? new MockRequest(urlOrReq) : urlOrReq;
282+
const [resPromise, done] = scope.handleFetch(req, clientId || 'default');
256283
return [
257284
resPromise.then<string|null>(res => res ? res.text() : null),
258285
done,

0 commit comments

Comments
 (0)
Please sign in to comment.