Ignore only-if-cached cache mode on Mozilla Firefox prior to version 60 (#7503)

This commit is contained in:
Akihiko Odaki 2018-05-16 03:14:30 +09:00 committed by Eugen Rochko
parent 73f4c61bbd
commit 82582dac2b

View file

@ -13,6 +13,8 @@ function fetchRoot() {
return fetch('/', { credentials: 'include' }); return fetch('/', { credentials: 'include' });
} }
const invalidOnlyIfCached = navigator.userAgent.match(/Firefox\/(\d+)/)[1] < 60;
// Cause a new version of a registered Service Worker to replace an existing one // Cause a new version of a registered Service Worker to replace an existing one
// that is already installed, and replace the currently active worker on open pages. // that is already installed, and replace the currently active worker on open pages.
self.addEventListener('install', function(event) { self.addEventListener('install', function(event) {
@ -54,19 +56,22 @@ self.addEventListener('fetch', function(event) {
event.respondWith(openSystemCache().then(cache => { event.respondWith(openSystemCache().then(cache => {
return cache.match(event.request.url).then(cached => { return cache.match(event.request.url).then(cached => {
if (cached === undefined) { if (cached === undefined) {
return fetch(event.request).then(fetched => { const asyncResponse = invalidOnlyIfCached && event.request.cache === 'only-if-cached' ?
if (fetched.ok) { fetch(event.request, { cache: 'no-cache' }) : fetch(event.request);
const put = cache.put(event.request.url, fetched.clone());
return asyncResponse.then(response => {
if (response.ok) {
const put = cache.put(event.request.url, response.clone());
put.catch(() => freeStorage()); put.catch(() => freeStorage());
return put.then(() => { return put.then(() => {
freeStorage(); freeStorage();
return fetched; return response;
}); });
} }
return fetched; return response;
}); });
} }