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