summaryrefslogtreecommitdiffstats
path: root/web_src/js/features/serviceworker.js
blob: fa415866cd2997db05d35236d8539bd4f0ba04c4 (plain)
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
const {UseServiceWorker, AppSubUrl, AppVer} = window.config;
const cachePrefix = 'static-cache-v'; // actual version is set in the service worker script

async function unregister() {
  const registrations = await navigator.serviceWorker.getRegistrations();
  await Promise.all(registrations.map((registration) => {
    return registration.active && registration.unregister();
  }));
}

async function invalidateCache() {
  const cacheKeys = await caches.keys();
  await Promise.all(cacheKeys.map((key) => {
    return key.startsWith(cachePrefix) && caches.delete(key);
  }));
}

async function checkCacheValidity() {
  const cacheKey = AppVer;
  const storedCacheKey = localStorage.getItem('staticCacheKey');

  // invalidate cache if it belongs to a different gitea version
  if (cacheKey && storedCacheKey !== cacheKey) {
    await invalidateCache();
    localStorage.setItem('staticCacheKey', cacheKey);
  }
}

export default async function initServiceWorker() {
  if (!('serviceWorker' in navigator)) return;

  if (UseServiceWorker) {
    try {
      // normally we'd serve the service worker as a static asset from StaticUrlPrefix but
      // the spec strictly requires it to be same-origin so it has to be AppSubUrl to work
      await Promise.all([
        checkCacheValidity(),
        navigator.serviceWorker.register(`${AppSubUrl}/serviceworker.js`),
      ]);
    } catch (err) {
      console.error(err);
      await Promise.all([
        invalidateCache(),
        unregister(),
      ]);
    }
  } else {
    await Promise.all([
      invalidateCache(),
      unregister(),
    ]);
  }
}