From ae8e0aba81864bf93fdbbc01cdc5784800b93eff Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Tue, 25 Jun 2019 08:28:24 -0400 Subject: [PATCH] fix Response construction in Edge, fix #8391 (#8392) Edge does not support constructing responses with readable streams. --- src/util/tile_request_cache.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/util/tile_request_cache.js b/src/util/tile_request_cache.js index 00c68e25dd2..f0f66cbd5a9 100644 --- a/src/util/tile_request_cache.js +++ b/src/util/tile_request_cache.js @@ -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; @@ -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) {