forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

sw.js100 lines, 4.4 KiBHistoryRawDownload
1// Forge service worker. Served at /sw.js, so its scope is the whole origin. It shows Web Push
2// notifications while no tab is open and opens the link of a clicked one. No inline code. The only
3// thing it caches is the offline page: app pages are always fetched fresh. A full page load that
4// finds the server down (or a proxy answering 502, 503 or 504 while forge restarts) gets that page
5// instead of the browser's error page. Everything else passes straight through to the network.
6
7// Bump the version when the offline page changes, so the new one is precached on install.
8var OFFLINE_CACHE = 'forge-offline-v2';
9var OFFLINE_URL = '/assets/offline.html';
10// The offline page and the two files it loads. They are precached: while the server is down the
11// browser could not fetch them for the offline page otherwise.
12var OFFLINE_FILES = [OFFLINE_URL, '/assets/offline.js', '/assets/main.css'];
13var RESTART_STATUS = [502, 503, 504];
14
15self.addEventListener('install', function (event) {
16 event.waitUntil(
17 caches.open(OFFLINE_CACHE)
18 .then(function (cache) { return cache.addAll(OFFLINE_FILES.map(function (u) { return new Request(u, { cache: 'reload' }); })); })
19 .catch(function () { /* the server is down: the page is fetched again on the next install */ })
20 .then(function () { return self.skipWaiting(); })
21 );
22});
23
24self.addEventListener('activate', function (event) {
25 event.waitUntil((async function () {
26 var names = await caches.keys();
27 await Promise.all(names.filter(function (n) { return n.indexOf('forge-offline-') === 0 && n !== OFFLINE_CACHE; })
28 .map(function (n) { return caches.delete(n); }));
29 await self.clients.claim();
30 })());
31});
32
33// Navigations only, and only GET: the response is the network's, unless the network failed or the
34// server is restarting. Subresources, the event stream and form posts are not touched, except the
35// three offline-page files above (bare URLs, no query): network first, the precache when it fails.
36self.addEventListener('fetch', function (event) {
37 var req = event.request;
38 var asset = new URL(req.url);
39 if (req.method === 'GET' && req.mode !== 'navigate' && asset.origin === self.location.origin &&
40 asset.search === '' && OFFLINE_FILES.indexOf(asset.pathname) >= 0) {
41 event.respondWith((async function () {
42 var res = null;
43 try { res = await fetch(req); } catch (e) { res = null; }
44 if (res && RESTART_STATUS.indexOf(res.status) < 0) { return res; }
45 var cached = await caches.match(asset.pathname);
46 return cached || res || Response.error();
47 })());
48 return;
49 }
50 if (req.mode !== 'navigate' || req.method !== 'GET') { return; }
51 event.respondWith((async function () {
52 var res = null;
53 try {
54 res = await fetch(req);
55 } catch (e) {
56 res = null;
57 }
58 if (res && RESTART_STATUS.indexOf(res.status) < 0) { return res; }
59 var offline = await caches.match(OFFLINE_URL);
60 return offline || res || Response.error();
61 })());
62});
63
64self.addEventListener('push', function (event) {
65 var data = {};
66 try {
67 data = event.data ? event.data.json() : {};
68 } catch (e) {
69 data = {};
70 }
71 var title = typeof data.title === 'string' && data.title ? data.title : 'Forge';
72 var options = {
73 body: typeof data.body === 'string' ? data.body : '',
74 tag: typeof data.tag === 'string' ? data.tag : undefined,
75 icon: '/assets/icons/icon-192.png',
76 badge: '/assets/icons/icon-192.png',
77 data: { url: typeof data.url === 'string' ? data.url : '/notifications' }
78 };
79 event.waitUntil(self.registration.showNotification(title, options));
80});
81
82// Focuses an open Forge tab and asks it to soft-navigate to the link, or opens a new window.
83self.addEventListener('notificationclick', function (event) {
84 event.notification.close();
85 var raw = event.notification.data && event.notification.data.url;
86 var target = typeof raw === 'string' && raw.charAt(0) === '/' && raw.charAt(1) !== '/' ? raw : '/notifications';
87 var url = new URL(target, self.location.origin).href;
88 event.waitUntil((async function () {
89 var windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
90 for (var i = 0; i < windows.length; i++) {
91 var client = windows[i];
92 if (new URL(client.url).origin === self.location.origin) {
93 await client.focus();
94 client.postMessage({ type: 'forge-navigate', url: target });
95 return;
96 }
97 }
98 await self.clients.openWindow(url);
99 })());
100});