8000 Return a HTTP 206 response as-is (#1721) · GoogleChrome/workbox@cd5ef34 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd5ef34

Browse files
authored
Return a HTTP 206 response as-is (#1721)
1 parent 79f9056 commit cd5ef34

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/workbox-range-requests/createPartialResponse.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ import './_version.mjs';
2020
* Given a `Request` and `Response` objects as input, this will return a
2121
* promise for a new `Response`.
2222
*
23+
* If the original `Response` already contains partial content (i.e. it has
24+
* a status of 206), then this assumes it already fulfills the `Range:`
25+
* requirements, and will return it as-is.
26+
*
2327
* @param {Request} request A request, which should contain a Range:
2428
* header.
25-
* @param {Response} originalResponse An original response containing the full
26-
* content.
29+
* @param {Response} originalResponse A response.
2730
* @return {Promise<Response>} Either a `206 Partial Content` response, with
2831
* the response body set to the slice of content specified by the request's
2932
* `Range:` header, or a `416 Range Not Satisfiable` response if the
@@ -47,6 +50,12 @@ async function createPartialResponse(request, originalResponse) {
4750
});
4851
}
4952

53+
if (originalResponse.status === 206) {
54+
// If we already have a 206, then just pass it through as-is;
55+
// see https://github.com/GoogleChrome/workbox/issues/1720
56+
return originalResponse;
57+
}
58+
5059
const rangeHeader = request.headers.get('range');
5160
if (!rangeHeader) {
5261
throw new WorkboxError('no-range-header');

test/workbox-range-requests/node/createPartialResponse.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,13 @@ describe(`[workbox-range-requests] createPartialResponse`, function() {
5959
const expectedBlob = constructBlob(101);
6060
expect(responseBlob._text).to.eql(expectedBlob._text);
6161
});
62+
63+
it(`should handle being passed a Response with a status of 206 by returning it as-is`, async function() {
64+
const originalPartialResponse = new Response('expected text', {status: 206});
65+
const createdPartialResponse = await createPartialResponse(VALID_REQUEST, originalPartialResponse);
66+
67+
// We should get back the exact same response.
68+
expect(createdPartialResponse).to.eql(originalPartialResponse);
69+
});
6270
});
6371
});

0 commit comments

Comments
 (0)
0