2018-07-03 13:50:18 -04:00
|
|
|
// import { freeStorage, storageFreeable } from '../storage/modifier';
|
2017-07-13 16:15:32 -04:00
|
|
|
import './web_push_notifications';
|
2017-07-17 18:19:17 -04:00
|
|
|
|
2018-07-03 13:50:18 -04:00
|
|
|
// function openSystemCache() {
|
|
|
|
// return caches.open('mastodon-system');
|
|
|
|
// }
|
2018-03-27 06:32:30 -04:00
|
|
|
|
|
|
|
function openWebCache() {
|
2018-03-18 10:14:38 -04:00
|
|
|
return caches.open('mastodon-web');
|
|
|
|
}
|
|
|
|
|
2018-03-17 07:35:13 -04:00
|
|
|
function fetchRoot() {
|
2018-05-15 22:59:44 -04:00
|
|
|
return fetch('/', { credentials: 'include', redirect: 'manual' });
|
2018-03-17 07:35:13 -04:00
|
|
|
}
|
|
|
|
|
2018-07-03 13:50:18 -04:00
|
|
|
// const firefox = navigator.userAgent.match(/Firefox\/(\d+)/);
|
|
|
|
// const invalidOnlyIfCached = firefox && firefox[1] < 60;
|
2018-05-15 14:14:30 -04:00
|
|
|
|
2017-07-17 18:19:17 -04:00
|
|
|
// 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.
|
|
|
|
self.addEventListener('install', function(event) {
|
2018-03-27 06:32:30 -04:00
|
|
|
event.waitUntil(Promise.all([openWebCache(), fetchRoot()]).then(([cache, root]) => cache.put('/', root)));
|
2017-07-17 18:19:17 -04:00
|
|
|
});
|
|
|
|
self.addEventListener('activate', function(event) {
|
|
|
|
event.waitUntil(self.clients.claim());
|
|
|
|
});
|
2018-03-17 07:35:13 -04:00
|
|
|
self.addEventListener('fetch', function(event) {
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
|
|
|
|
if (url.pathname.startsWith('/web/')) {
|
2018-03-18 10:14:38 -04:00
|
|
|
const asyncResponse = fetchRoot();
|
2018-03-27 06:32:30 -04:00
|
|
|
const asyncCache = openWebCache();
|
2018-03-18 10:14:38 -04:00
|
|
|
|
2018-05-15 22:59:44 -04:00
|
|
|
event.respondWith(asyncResponse.then(
|
2018-06-20 14:55:17 -04:00
|
|
|
response => {
|
|
|
|
const clonedResponse = response.clone();
|
|
|
|
asyncCache.then(cache => cache.put('/', clonedResponse)).catch();
|
|
|
|
return response;
|
|
|
|
},
|
2018-05-15 22:59:44 -04:00
|
|
|
() => asyncCache.then(cache => cache.match('/'))));
|
2018-03-18 10:14:38 -04:00
|
|
|
} else if (url.pathname === '/auth/sign_out') {
|
|
|
|
const asyncResponse = fetch(event.request);
|
2018-03-27 06:32:30 -04:00
|
|
|
const asyncCache = openWebCache();
|
2018-03-18 10:14:38 -04:00
|
|
|
|
2018-05-14 13:58:11 -04:00
|
|
|
event.respondWith(asyncResponse.then(response => {
|
2018-03-18 10:14:38 -04:00
|
|
|
if (response.ok || response.type === 'opaqueredirect') {
|
2018-05-14 13:58:11 -04:00
|
|
|
return Promise.all([
|
2018-04-08 07:32:39 -04:00
|
|
|
asyncCache.then(cache => cache.delete('/')),
|
|
|
|
indexedDB.deleteDatabase('mastodon'),
|
2018-05-14 13:58:11 -04:00
|
|
|
]).then(() => response);
|
2018-03-18 10:14:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}));
|
2018-07-03 13:50:18 -04:00
|
|
|
} /* else if (storageFreeable && (ATTACHMENT_HOST ? url.host === ATTACHMENT_HOST : url.pathname.startsWith('/system/'))) {
|
2018-05-14 13:58:11 -04:00
|
|
|
event.respondWith(openSystemCache().then(cache => {
|
|
|
|
return cache.match(event.request.url).then(cached => {
|
|
|
|
if (cached === undefined) {
|
2018-05-15 14:14:30 -04:00
|
|
|
const asyncResponse = invalidOnlyIfCached && event.request.cache === 'only-if-cached' ?
|
|
|
|
fetch(event.request, { cache: 'no-cache' }) : fetch(event.request);
|
|
|
|
|
|
|
|
return asyncResponse.then(response => {
|
|
|
|
if (response.ok) {
|
2018-06-20 14:55:17 -04:00
|
|
|
cache
|
|
|
|
.put(event.request.url, response.clone())
|
|
|
|
.catch(()=>{}).then(freeStorage()).catch();
|
2018-05-14 13:58:11 -04:00
|
|
|
}
|
2018-03-27 06:32:30 -04:00
|
|
|
|
2018-05-15 14:14:30 -04:00
|
|
|
return response;
|
2018-05-14 13:58:11 -04:00
|
|
|
});
|
|
|
|
}
|
2018-03-27 06:32:30 -04:00
|
|
|
|
2018-05-14 13:58:11 -04:00
|
|
|
return cached;
|
|
|
|
});
|
2018-03-27 06:32:30 -04:00
|
|
|
}));
|
2018-07-03 13:50:18 -04:00
|
|
|
} */
|
2018-03-17 07:35:13 -04:00
|
|
|
});
|