summaryrefslogtreecommitdiff
path: root/crates/wipfel/www/sw.js
blob: 2a5cfdc3c981eba6f3e23e426b86c95c300e4a9e (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
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
/* wipfel service worker: full offline + Web Share Target.
 *
 * Strategy: network-first with cache fallback, so an online app is
 * always fresh and the app still starts offline after the first visit.
 *
 * Share target: the browser POSTs multipart/form-data to __share; we
 * stash the file in a cache under __shared and redirect to the app
 * with ?shared=1, which the app picks up on startup.
 */
const VERSION = "wipfel-v1";
const SHARE_CACHE = "wipfel-share";
const base = self.registration.scope;
const url = (p) => new URL(p, base).href;
const sharePath = new URL("__share", base).pathname;
const sharedPath = new URL("__shared", base).pathname;

const SHELL = [
  "",
  "index.html",
  "wipfel.js",
  "wipfel_bg.wasm",
  "style.css",
  "gipfel.dat.gz",
  "manifest.webmanifest",
  "icons/icon-192.png",
  "icons/icon-512.png",
  "icons/icon-maskable-512.png",
].map(url);

self.addEventListener("install", (e) => {
  e.waitUntil(
    caches
      .open(VERSION)
      .then((c) => c.addAll(SHELL))
      .then(() => self.skipWaiting())
  );
});

self.addEventListener("activate", (e) => {
  e.waitUntil(
    caches
      .keys()
      .then((keys) =>
        Promise.all(
          keys.filter((k) => k !== VERSION && k !== SHARE_CACHE).map((k) => caches.delete(k))
        )
      )
      .then(() => self.clients.claim())
  );
});

self.addEventListener("fetch", (e) => {
  const req = e.request;
  const u = new URL(req.url);
  if (u.origin !== self.location.origin) return;

  // Web Share Target handoff
  if (req.method === "POST" && u.pathname === sharePath) {
    e.respondWith(
      (async () => {
        const form = await req.formData();
        const file = form.getAll("files")[0] || form.get("files");
        if (file && file.size) {
          const buf = await file.arrayBuffer();
          const cache = await caches.open(SHARE_CACHE);
          await cache.put(
            sharedPath,
            new Response(buf, {
              headers: {
                "Content-Type": file.type || "image/jpeg",
                "X-File-Name": encodeURIComponent(file.name || "shared.jpg"),
              },
            })
          );
        }
        return Response.redirect(url("?shared=1"), 303);
      })()
    );
    return;
  }

  // the stashed shared image
  if (req.method === "GET" && u.pathname === sharedPath) {
    e.respondWith(
      caches.open(SHARE_CACHE).then((c) =>
        c.match(sharedPath).then(
          (r) => r || new Response("no shared image", { status: 410 })
        )
      )
    );
    return;
  }

  // app assets: network first, cache fallback
  e.respondWith(
    (async () => {
      try {
        const res = await fetch(req);
        if (res.ok) {
          const cache = await caches.open(VERSION);
          cache.put(req, res.clone());
        }
        return res;
      } catch (err) {
        const cached = await caches.match(req, { ignoreSearch: true });
        if (cached) return cached;
        if (req.mode === "navigate") {
          const idx = await caches.match(url("index.html"));
          if (idx) return idx;
        }
        throw err;
      }
    })()
  );
});