2017-07-14 05:15:32 +09:00
|
|
|
import './web_push_notifications';
|
2017-07-18 07:19:17 +09:00
|
|
|
|
2018-03-17 20:35:13 +09:00
|
|
|
function fetchRoot() {
|
|
|
|
return fetch('/', { credentials: 'include' });
|
|
|
|
}
|
|
|
|
|
2017-07-18 07:19:17 +09: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-17 20:35:13 +09:00
|
|
|
const promises = Promise.all([caches.open('mastodon-web'), fetchRoot()]);
|
|
|
|
const asyncAdd = promises.then(([cache, root]) => cache.put('/', root));
|
|
|
|
|
|
|
|
event.waitUntil(asyncAdd);
|
2017-07-18 07:19:17 +09:00
|
|
|
});
|
|
|
|
self.addEventListener('activate', function(event) {
|
|
|
|
event.waitUntil(self.clients.claim());
|
|
|
|
});
|
2018-03-17 20:35:13 +09:00
|
|
|
self.addEventListener('fetch', function(event) {
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
|
|
|
|
if (url.pathname.startsWith('/web/')) {
|
|
|
|
event.respondWith(fetchRoot().then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw null;
|
|
|
|
}).catch(() => caches.match('/')));
|
|
|
|
}
|
|
|
|
});
|