aboutsummaryrefslogtreecommitdiffstats
path: root/src/service-worker.js
blob: 3c1ae0ac8dd2967996818983b8a8fd97589d11e0 (plain) (blame)
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
import { build, files, version } from "$service-worker";
import { Worker } from "@arkade-os/sdk";

const worker = new Worker();
worker.start(false).catch(console.error);

const CACHE_BUILD = `coinos-build-${version}`;
const CACHE_PAGES = `coinos-pages-${version}`;
const CACHE_PUBLIC = `coinos-public-${version}`;

const ESSENTIAL_STATIC = files.filter(
  (f) =>
    f.endsWith(".woff2") ||
    f.endsWith(".woff") ||
    f.endsWith(".png") ||
    f.endsWith(".webp") ||
    f.endsWith(".webmanifest") ||
    f === "/favicon.ico",
);

self.addEventListener("install", (event) => {
  event.waitUntil(
    caches
      .open(CACHE_BUILD)
      .then((cache) => cache.addAll([...build, ...ESSENTIAL_STATIC]))
      .then(() => self.skipWaiting()),
  );
});

self.addEventListener("activate", (event) => {
  event.waitUntil(
    caches
      .keys()
      .then((keys) =>
        Promise.all(
          keys
            .filter((k) => k.startsWith("coinos-") && k !== CACHE_BUILD && k !== CACHE_PAGES && k !== CACHE_PUBLIC)
            .map((k) => caches.delete(k)),
        ),
      )
      .then(() => self.clients.claim()),
  );
});

self.addEventListener("fetch", (event) => {
  const { request } = event;
  const url = new URL(request.url);

  if (request.method !== "GET") return;
  if (url.origin !== self.location.origin) return;

  // Build assets — cache-first (fingerprinted/immutable)
  if (build.includes(url.pathname)) {
    event.respondWith(caches.match(request).then((cached) => cached || fetch(request)));
    return;
  }

  // Navigation HTML — network-first
  if (request.mode === "navigate") {
    event.respondWith(
      fetch(request)
        .then((response) => {
          const clone = response.clone();
          caches.open(CACHE_PAGES).then((cache) => cache.put(request, clone));
          return response;
        })
        .catch(() => caches.match(request).then((cached) => cached || caches.match("/"))),
    );
    return;
  }

  // __data.json (SvelteKit client-side navigation) — network-first
  if (url.pathname.endsWith("__data.json")) {
    event.respondWith(
      fetch(request)
        .then((response) => {
          const clone = response.clone();
          caches.open(CACHE_PAGES).then((cache) => cache.put(request, clone));
          return response;
        })
        .catch(() => caches.match(request)),
    );
    return;
  }

  // Public API (avatars etc) — cache-first
  if (url.pathname.startsWith("/api/public/")) {
    event.respondWith(
      caches.match(request).then(
        (cached) =>
          cached ||
          fetch(request).then((response) => {
            const clone = response.clone();
            caches.open(CACHE_PUBLIC).then((cache) => cache.put(request, clone));
            return response;
          }),
      ),
    );
    return;
  }

  // Static files from ESSENTIAL_STATIC — cache-first
  if (ESSENTIAL_STATIC.includes(url.pathname)) {
    event.respondWith(caches.match(request).then((cached) => cached || fetch(request)));
    return;
  }
});

self.addEventListener("push", (event) => {
  const { title, body, url } = event.data.json();
  const icon = "/images/icon.png";
  const badge = "/images/badge.png";
  self.registration.showNotification(title, {
    body,
    icon,
    badge,
    data: { url },
  });
});

self.addEventListener("notificationclick", (event) => {
  event.notification.close();

  const url = event.notification.data.url || "/";

  event.waitUntil(
    clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => {
      for (const client of clientList) {
        if (client.url === url && "focus" in client) {
          return client.focus();
        }
      }
      if (clients.openWindow) {
        return clients.openWindow(url);
      }
    }),
  );
});