Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherry-pick edge cache fix to oolong #8394

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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