Skip to content

Commit

Permalink
fix Response construction in Edge, fix #8391 (#8392) (#8394)
Browse files Browse the repository at this point in the history
Edge does not support constructing responses with readable streams.
  • Loading branch information
ansis authored Jun 25, 2019
1 parent f5e2bc3 commit c49dd31
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/util/tile_request_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ export type ResponseOptions = {
headers: window.Headers
};


let responseConstructorSupportsReadableStream;
function prepareBody(response: Response, callback) {
if (responseConstructorSupportsReadableStream === undefined) {
try {
new Response(new ReadableStream()); // eslint-disable-line no-undef
responseConstructorSupportsReadableStream = true;
} catch (e) {
// Edge
responseConstructorSupportsReadableStream = false;
}
}

if (responseConstructorSupportsReadableStream) {
callback(response.body);
} else {
response.blob().then(callback);
}
}

export function cachePut(request: Request, response: Response, requestTime: number) {
if (!window.caches) return;

Expand All @@ -38,9 +58,11 @@ export function cachePut(request: Request, response: Response, requestTime: numb
const timeUntilExpiry = new Date(options.headers.get('Expires')).getTime() - requestTime;
if (timeUntilExpiry < MIN_TIME_UNTIL_EXPIRY) return;

const clonedResponse = new window.Response(response.body, options);
prepareBody(response, body => {
const clonedResponse = new window.Response(body, options);

window.caches.open(CACHE_NAME).then(cache => cache.put(stripQueryParameters(request.url), clonedResponse));
window.caches.open(CACHE_NAME).then(cache => cache.put(stripQueryParameters(request.url), clonedResponse));
});
}

function stripQueryParameters(url: string) {
Expand Down

0 comments on commit c49dd31

Please sign in to comment.