@lethevimlet/sshift 1.8.0 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/webapp/sw.js CHANGED
@@ -1,5 +1,26 @@
1
- const CACHE_NAME = 'sshift-__VERSION__';
1
+ // SSHIFT service worker.
2
+ //
3
+ // Cache busting model (v1.8.2):
4
+ // - __VERSION__ is replaced with the package version by the server on every
5
+ // request, and /sw.js is served with no-store. A new release therefore
6
+ // produces a byte-different worker, which the browser installs as an
7
+ // update.
8
+ // - Every asset the page references carries ?v=<version>; the cache is
9
+ // named after the version too. Old caches (any name that is not this
10
+ // version's) are deleted on activate, so nothing from a previous version
11
+ // can ever be served again.
12
+ // - skipWaiting() + clients.claim(): the new worker replaces the old one as
13
+ // soon as it has installed, for every open page at once. There is never a
14
+ // "waiting" worker and never two versions alive side by side. The page's
15
+ // registration script reloads on controllerchange so HTML, JS and CSS are
16
+ // always from the same version.
17
+ // - Navigations (index.html) are network-first with a versioned offline
18
+ // fallback; everything else same-origin is cache-first because the cache
19
+ // is version-scoped. Socket.IO, the API and the worker script itself are
20
+ // never cached.
2
21
  const VERSION = '__VERSION__';
22
+ const CACHE_NAME = 'sshift-' + VERSION;
23
+ const NAV_CACHE = CACHE_NAME + '-nav';
3
24
 
4
25
  const PRECACHE_URLS = [
5
26
  '/css/style.css?v=' + VERSION,
@@ -9,73 +30,68 @@ const PRECACHE_URLS = [
9
30
  '/manifest.json?v=' + VERSION
10
31
  ];
11
32
 
12
- const NAV_CACHE = CACHE_NAME + '-nav';
13
-
14
33
  self.addEventListener('install', (event) => {
15
34
  event.waitUntil(
16
- caches.open(CACHE_NAME).then((cache) => {
17
- return Promise.allSettled(
35
+ caches.open(CACHE_NAME)
36
+ .then((cache) => Promise.allSettled(
18
37
  PRECACHE_URLS.map((url) =>
19
38
  cache.add(url).catch((e) => {
20
- console.warn('[SW] Failed to precache:', url, e.message);
39
+ console.warn('[SW] Failed to precache:', url, e && e.message);
21
40
  })
22
41
  )
23
- );
24
- })
42
+ ))
43
+ .then(() => self.skipWaiting())
25
44
  );
26
- self.skipWaiting();
27
45
  });
28
46
 
29
47
  self.addEventListener('activate', (event) => {
30
48
  event.waitUntil(
31
- caches.keys().then((cacheNames) =>
32
- Promise.all(
49
+ caches.keys()
50
+ .then((cacheNames) => Promise.all(
33
51
  cacheNames
34
52
  .filter((name) => name !== CACHE_NAME && name !== NAV_CACHE)
35
53
  .map((name) => caches.delete(name))
36
- )
37
- )
54
+ ))
55
+ .then(() => self.clients.claim())
38
56
  );
39
- self.clients.claim();
40
57
  });
41
58
 
42
- self.addEventListener('fetch', (event) => {
43
- if (event.request.method !== 'GET') return;
59
+ function isCacheable(url, request) {
60
+ if (request.method !== 'GET') return false;
61
+ if (url.origin !== self.location.origin) return false;
62
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
63
+ if (url.pathname.startsWith('/socket.io/')) return false;
64
+ if (url.pathname.startsWith('/api/')) return false;
65
+ if (url.pathname === '/sw.js') return false;
66
+ return true;
67
+ }
44
68
 
69
+ self.addEventListener('fetch', (event) => {
45
70
  const url = new URL(event.request.url);
71
+ if (!isCacheable(url, event.request)) return;
46
72
 
47
- if (url.protocol !== 'http:' && url.protocol !== 'https:') return;
48
-
49
- if (url.pathname.startsWith('/socket.io/')) return;
50
- if (url.pathname.startsWith('/api/')) return;
51
- if (url.pathname === '/sw.js') return;
52
-
53
- const isNavigation = event.request.mode === 'navigate';
54
-
55
- if (isNavigation) {
73
+ if (event.request.mode === 'navigate') {
56
74
  event.respondWith(
57
75
  fetch(event.request)
58
76
  .then((response) => {
59
77
  if (response && response.status === 200) {
60
78
  const clone = response.clone();
61
- caches.open(NAV_CACHE).then((cache) => cache.put(event.request, clone));
79
+ caches.open(NAV_CACHE).then((cache) => cache.put(event.request, clone)).catch(() => {});
62
80
  }
63
81
  return response;
64
82
  })
65
- .catch(() => caches.match(event.request))
83
+ .catch(() => caches.match(event.request).then((cached) => cached || Response.error()))
66
84
  );
67
85
  return;
68
86
  }
69
87
 
70
88
  event.respondWith(
71
89
  caches.match(event.request).then((cached) => {
72
- if (cached) {
73
- return cached;
74
- }
90
+ if (cached) return cached;
75
91
  return fetch(event.request).then((response) => {
76
92
  if (response && response.status === 200 && response.type === 'basic') {
77
93
  const clone = response.clone();
78
- caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
94
+ caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone)).catch(() => {});
79
95
  }
80
96
  return response;
81
97
  });
@@ -84,11 +100,22 @@ self.addEventListener('fetch', (event) => {
84
100
  });
85
101
 
86
102
  self.addEventListener('message', (event) => {
87
- if (event.data && event.data.type === 'CLEAR_CACHES') {
88
- caches.keys().then((names) => {
89
- for (const name of names) {
90
- caches.delete(name);
91
- }
92
- });
103
+ const data = event.data || {};
104
+ const reply = (msg) => {
105
+ try {
106
+ if (event.ports && event.ports[0]) event.ports[0].postMessage(msg);
107
+ else if (event.source && event.source.postMessage) event.source.postMessage(msg);
108
+ } catch (_) {}
109
+ };
110
+ if (data.type === 'CLEAR_CACHES') {
111
+ event.waitUntil(
112
+ caches.keys()
113
+ .then((names) => Promise.all(names.map((name) => caches.delete(name))))
114
+ .then(() => reply({ type: 'CACHES_CLEARED' }))
115
+ );
116
+ } else if (data.type === 'GET_VERSION') {
117
+ reply({ type: 'VERSION', version: VERSION });
118
+ } else if (data.type === 'SKIP_WAITING') {
119
+ self.skipWaiting();
93
120
  }
94
- });
121
+ });