2018-04-08 14:32:39 +03:00
|
|
|
import { freeStorage } from '../storage/modifier';
|
2017-07-13 23:15:32 +03:00
|
|
|
import './web_push_notifications';
|
2017-07-18 01:19:17 +03:00
|
|
|
|
2018-03-27 13:32:30 +03:00
|
|
|
function openSystemCache() {
|
|
|
|
return caches.open('mastodon-system');
|
|
|
|
}
|
|
|
|
|
|
|
|
function openWebCache() {
|
2018-03-18 16:14:38 +02:00
|
|
|
return caches.open('mastodon-web');
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:35:13 +02:00
|
|
|
function fetchRoot() {
|
|
|
|
return fetch('/', { credentials: 'include' });
|
|
|
|
}
|
|
|
|
|
2017-07-18 01:19:17 +03: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 13:32:30 +03:00
|
|
|
event.waitUntil(Promise.all([openWebCache(), fetchRoot()]).then(([cache, root]) => cache.put('/', root)));
|
2017-07-18 01:19:17 +03:00
|
|
|
});
|
|
|
|
self.addEventListener('activate', function(event) {
|
|
|
|
event.waitUntil(self.clients.claim());
|
|
|
|
});
|
2018-03-17 13:35:13 +02:00
|
|
|
self.addEventListener('fetch', function(event) {
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
|
|
|
|
if (url.pathname.startsWith('/web/')) {
|
2018-03-18 16:14:38 +02:00
|
|
|
const asyncResponse = fetchRoot();
|
2018-03-27 13:32:30 +03:00
|
|
|
const asyncCache = openWebCache();
|
2018-03-18 16:14:38 +02:00
|
|
|
|
2018-05-14 20:58:11 +03:00
|
|
|
event.respondWith(asyncResponse.then(response => {
|
2018-03-17 13:35:13 +02:00
|
|
|
if (response.ok) {
|
2018-05-15 15:24:50 +03:00
|
|
|
return asyncCache.then(cache => cache.put('/', response.clone()))
|
|
|
|
.then(() => response);
|
2018-03-17 13:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw null;
|
2018-03-27 13:32:30 +03:00
|
|
|
}).catch(() => asyncCache.then(cache => cache.match('/'))));
|
2018-03-18 16:14:38 +02:00
|
|
|
} else if (url.pathname === '/auth/sign_out') {
|
|
|
|
const asyncResponse = fetch(event.request);
|
2018-03-27 13:32:30 +03:00
|
|
|
const asyncCache = openWebCache();
|
2018-03-18 16:14:38 +02:00
|
|
|
|
2018-05-14 20:58:11 +03:00
|
|
|
event.respondWith(asyncResponse.then(response => {
|
2018-03-18 16:14:38 +02:00
|
|
|
if (response.ok || response.type === 'opaqueredirect') {
|
2018-05-14 20:58:11 +03:00
|
|
|
return Promise.all([
|
2018-04-08 14:32:39 +03:00
|
|
|
asyncCache.then(cache => cache.delete('/')),
|
|
|
|
indexedDB.deleteDatabase('mastodon'),
|
2018-05-14 20:58:11 +03:00
|
|
|
]).then(() => response);
|
2018-03-18 16:14:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}));
|
2018-03-27 13:32:30 +03:00
|
|
|
} else if (process.env.CDN_HOST ? url.host === process.env.CDN_HOST : url.pathname.startsWith('/system/')) {
|
2018-05-14 20:58:11 +03:00
|
|
|
event.respondWith(openSystemCache().then(cache => {
|
|
|
|
return cache.match(event.request.url).then(cached => {
|
|
|
|
if (cached === undefined) {
|
|
|
|
return fetch(event.request).then(fetched => {
|
|
|
|
if (fetched.ok) {
|
|
|
|
const put = cache.put(event.request.url, fetched.clone());
|
2018-03-27 13:32:30 +03:00
|
|
|
|
2018-05-14 20:58:11 +03:00
|
|
|
put.catch(() => freeStorage());
|
2018-03-27 13:32:30 +03:00
|
|
|
|
2018-05-14 20:58:11 +03:00
|
|
|
return put.then(() => {
|
|
|
|
freeStorage();
|
|
|
|
return fetched;
|
|
|
|
});
|
|
|
|
}
|
2018-03-27 13:32:30 +03:00
|
|
|
|
2018-05-14 20:58:11 +03:00
|
|
|
return fetched;
|
|
|
|
});
|
|
|
|
}
|
2018-03-27 13:32:30 +03:00
|
|
|
|
2018-05-14 20:58:11 +03:00
|
|
|
return cached;
|
|
|
|
});
|
2018-03-27 13:32:30 +03:00
|
|
|
}));
|
2018-03-17 13:35:13 +02:00
|
|
|
}
|
|
|
|
});
|