Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
149 lines (124 loc) · 4.33 KB
/
Copy pathsw.js
File metadata and controls
149 lines (124 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* The service worker behind the installed app.
*
* It keeps a deliberately small footprint: pages are always fetched from the
* network, since every one of them is rendered for the signed-in user and
* carries a CSRF token, and only when the network is unreachable does a
* navigation fall back to the offline page cached at install time. Built
* assets under /build are content-hashed, so they are served from the cache
* first and fetched once per deploy.
*
* The page registers this file with the current asset version in its query
* string, so every deploy installs a fresh worker, refreshes the offline page,
* and drops the caches of the previous build.
*/
const version = new URL(self.location.href).searchParams.get('v') ?? 'dev';
const offlineCache = `offline-${version}`;
const assetCache = `assets-${version}`;
const offlineUrl = '/offline.html';
const offlineFiles = [offlineUrl, '/favicon.ico', '/favicon.svg'];
self.addEventListener('push', (event) => {
event.waitUntil(Promise.all([
self.registration.showNotification('/dev/null · Remotes', {
body: 'You have a new message in Remotes.',
icon: '/icon-192.png',
tag: 'devnull-remotes',
silent: true,
data: { url: '/remotes' },
}),
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clients) => {
for (const client of clients) {
client.postMessage({ type: 'remote-push' });
}
}),
]));
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const url = new URL(event.notification.data?.url ?? '/', self.location.origin);
if (url.origin !== self.location.origin) return;
event.waitUntil(self.clients.matchAll({ type: 'window' }).then(async (clients) => {
const client = clients.find((client) => new URL(client.url).pathname.startsWith('/remotes')) ?? clients[0];
if (client) {
await client.navigate(url.href);
await client.focus();
} else {
await self.clients.openWindow(url.href);
}
}));
});
self.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(offlineCache)
.then((cache) => cache.addAll(offlineFiles.map((file) => new Request(file, { cache: 'reload' }))))
.then(() => self.skipWaiting()),
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((key) => key !== offlineCache && key !== assetCache)
.map((key) => caches.delete(key)),
),
)
.then(() => self.clients.claim()),
);
});
self.addEventListener('fetch', (event) => {
const { request } = event;
if (request.method !== 'GET') {
return;
}
if (request.mode === 'navigate') {
event.respondWith(navigate(request));
return;
}
const url = new URL(request.url);
if (url.origin !== self.location.origin) {
return;
}
if (url.pathname.startsWith('/build/')) {
event.respondWith(asset(request));
} else if (offlineFiles.includes(url.pathname)) {
event.respondWith(offlineFile(request));
}
});
async function navigate(request) {
try {
return await fetch(request);
} catch {
const cached = await caches.match(offlineUrl, { cacheName: offlineCache });
return cached ?? new Response('You are offline.', {
status: 503,
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
});
}
}
async function offlineFile(request) {
try {
return await fetch(request);
} catch (error) {
const cached = await caches.match(request, { cacheName: offlineCache, ignoreSearch: true });
if (cached) {
return cached;
}
throw error;
}
}
async function asset(request) {
const cache = await caches.open(assetCache);
const cached = await cache.match(request);
if (cached) {
return cached;
}
const response = await fetch(request);
if (response.ok && response.type === 'basic') {
await cache.put(request, response.clone());
}
return response;
}