summaryrefslogtreecommitdiff
path: root/apps/web/app/sw.ts
blob: 046937767ebe0e3a69e175fd071853972e69f1a7 (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
/// <reference lib="webworker" />
import { defaultCache } from "@serwist/next/worker"
import type { PrecacheEntry, SerwistGlobalConfig } from "serwist"
import { Serwist } from "serwist"

declare global {
  interface WorkerGlobalScope extends SerwistGlobalConfig {
    __SW_MANIFEST: (PrecacheEntry | string)[] | undefined
  }
}

declare const self: ServiceWorkerGlobalScope

const sameOriginCache = defaultCache.map((entry) => ({
  ...entry,
  matcher: (parameters: Parameters<Exclude<(typeof defaultCache)[number]["matcher"], RegExp | string | undefined>>[0]) => {
    if (!parameters.sameOrigin) return false
    const original = entry.matcher
    if (typeof original === "function") return original(parameters)
    if (original instanceof RegExp) return original.test(parameters.url.href)
    return false
  },
}))

const serwist = new Serwist({
  precacheEntries: self.__SW_MANIFEST,
  skipWaiting: true,
  clientsClaim: true,
  navigationPreload: true,
  runtimeCaching: sameOriginCache,
})

serwist.addEventListeners()