From cd5ef346c6403aafa4cabc99a57d09f442db7860 Mon Sep 17 00:00:00 2001 From: Jeffrey Posnick Date: Thu, 18 Oct 2018 14:10:47 -0400 Subject: [PATCH] Return a HTTP 206 response as-is (#1721) --- .../createPartialResponse.mjs | 13 +++++++++++-- .../node/createPartialResponse.mjs | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/workbox-range-requests/createPartialResponse.mjs b/packages/workbox-range-requests/createPartialResponse.mjs index d9dda27db..4b3052760 100644 --- a/packages/workbox-range-requests/createPartialResponse.mjs +++ b/packages/workbox-range-requests/createPartialResponse.mjs @@ -20,10 +20,13 @@ import './_version.mjs'; * Given a `Request` and `Response` objects as input, this will return a * promise for a new `Response`. * + * If the original `Response` already contains partial content (i.e. it has + * a status of 206), then this assumes it already fulfills the `Range:` + * requirements, and will return it as-is. + * * @param {Request} request A request, which should contain a Range: * header. - * @param {Response} originalResponse An original response containing the full - * content. + * @param {Response} originalResponse A response. * @return {Promise} Either a `206 Partial Content` response, with * the response body set to the slice of content specified by the request's * `Range:` header, or a `416 Range Not Satisfiable` response if the @@ -47,6 +50,12 @@ async function createPartialResponse(request, originalResponse) { }); } + if (originalResponse.status === 206) { + // If we already have a 206, then just pass it through as-is; + // see https://github.com/GoogleChrome/workbox/issues/1720 + return originalResponse; + } + const rangeHeader = request.headers.get('range'); if (!rangeHeader) { throw new WorkboxError('no-range-header'); diff --git a/test/workbox-range-requests/node/createPartialResponse.mjs b/test/workbox-range-requests/node/createPartialResponse.mjs index 70b5c9863..bf7f871c0 100644 --- a/test/workbox-range-requests/node/createPartialResponse.mjs +++ b/test/workbox-range-requests/node/createPartialResponse.mjs @@ -59,5 +59,13 @@ describe(`[workbox-range-requests] createPartialResponse`, function() { const expectedBlob = constructBlob(101); expect(responseBlob._text).to.eql(expectedBlob._text); }); + + it(`should handle being passed a Response with a status of 206 by returning it as-is`, async function() { + const originalPartialResponse = new Response('expected text', {status: 206}); + const createdPartialResponse = await createPartialResponse(VALID_REQUEST, originalPartialResponse); + + // We should get back the exact same response. + expect(createdPartialResponse).to.eql(originalPartialResponse); + }); }); });