Skip to content

Commit

Permalink
Prefer response.status === 200 to response.ok (#1805)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick authored Dec 20, 2018
1 parent b294993 commit bebeb49
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
6 changes: 3 additions & 3 deletions packages/workbox-core/_private/cacheWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const matchWrapper = async ({

/**
* This method will call cacheWillUpdate on the available plugins (or use
* response.ok) to determine if the Response is safe and valid to cache.
* status === 200) to determine if the Response is safe and valid to cache.
*
* @param {Object} options
* @param {Request} options.request
Expand Down Expand Up @@ -204,7 +204,7 @@ const _isResponseSafeToCache = async ({request, response, event, plugins}) => {

if (!pluginsUsed) {
if (process.env.NODE_ENV !== 'production') {
if (!responseToCache.ok) {
if (!responseToCache.status === 200) {
if (responseToCache.status === 0) {
logger.warn(`The response for '${request.url}' is an opaque ` +
`response. The caching strategy that you're using will not ` +
Expand All @@ -216,7 +216,7 @@ const _isResponseSafeToCache = async ({request, response, event, plugins}) => {
}
}
}
responseToCache = responseToCache.ok ? responseToCache : null;
responseToCache = responseToCache.status === 200 ? responseToCache : null;
}

return responseToCache ? responseToCache : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import '../_version.mjs';

export default {
/**
* Return return a response (i.e. allow caching) if the
* response is ok (i.e. 200) or is opaque.
* Returns a valid response (to allow caching) if the status is 200 (OK) or
* 0 (opaque).
*
* @param {Object} options
* @param {Response} options.response
Expand All @@ -20,7 +20,7 @@ export default {
* @private
*/
cacheWillUpdate: ({response}) => {
if (response.ok || response.status === 0) {
if (response.status === 200 || response.status === 0) {
return response;
}
return null;
Expand Down
43 changes: 22 additions & 21 deletions test/workbox-core/node/_private/test-cacheWrapper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,29 @@ describe(`workbox-core cacheWrapper`, function() {
expect(cacheResponse).to.equal(putResponse);
});

it(`should not cache opaque responses by default`, async function() {
const testCache = await caches.open('TEST-CACHE');
const cacheOpenStub = sandbox.stub(global.caches, 'open');
const cachePutStub = sandbox.stub(testCache, 'put');
cacheOpenStub.callsFake(async (cacheName) => {
return testCache;
});
const putRequest = new Request('/test/string');
const putResponse = new Response('Response for /test/string', {
// The mock doesn't allow a status of zero due to a bug
// so mock to 1.
status: 1,
});
await cacheWrapper.put({
cacheName: 'TODO-CHANGE-ME',
request: putRequest,
response: putResponse,
});
// This covers opaque responses (0) and partial content responses (206).
for (const status of [0, 206]) {
it(`should not cache response.status of ${status} by default`, async function() {
const cacheName = 'test-cache';
const testCache = await caches.open(cacheName);
const cacheOpenStub = sandbox.stub(global.caches, 'open').resolves(testCache);
const cachePutSpy = sandbox.spy(testCache, 'put');

const putRequest = new Request('/test/string');
const putResponse = new Response('', {
status,
});

expect(cacheOpenStub.callCount).to.equal(0);
expect(cachePutStub.callCount).to.equal(0);
});
await cacheWrapper.put({
cacheName,
request: putRequest,
response: putResponse,
});

expect(cacheOpenStub.callCount).to.equal(0);
expect(cachePutSpy.callCount).to.equal(0);
});
}

devOnly.it(`should not cache POST responses`, async function() {
const testCache = await caches.open('TEST-CACHE');
Expand Down
12 changes: 7 additions & 5 deletions test/workbox-strategies/node/test-cacheOkAndOpaquePlugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {expect} from 'chai';
import plugin from '../../../packages/workbox-strategies/plugins/cacheOkAndOpaquePlugin.mjs';

describe(`[workbox-strategies] cacheOkAndOpaquePlugin`, function() {
it(`should return null if status is not ok and status is not opaque`, function() {
const response = new Response('Hello', {
status: 404,
for (const status of [206, 404]) {
it(`should return null when status is ${status}`, function() {
const response = new Response('Hello', {
status,
});
expect(plugin.cacheWillUpdate({response})).to.equal(null);
});
expect(plugin.cacheWillUpdate({response})).to.equal(null);
});
}

it(`should return Response if status is opaque`, function() {
const response = new Response('Hello', {
Expand Down

0 comments on commit bebeb49

Please sign in to comment.