aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/PwaRegister.astro
blob: 82359a4ed1ce2ba267edcbcf2a5f1c77491c97fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script>
  if ("serviceWorker" in navigator) {
    if (import.meta.env.PROD) {
      window.addEventListener("load", () => {
        navigator.serviceWorker.register("/sw.js").catch(() => {
          // PWA登録に失敗しても、通常のWebページとして動作させる。
        });
      });
    } else {
      // 開発時はService WorkerのキャッシュがHMRを妨げるため、既存の登録を解除する。
      navigator.serviceWorker.getRegistrations().then((registrations) => {
        registrations.forEach((registration) => registration.unregister());
      });
      if ("caches" in window) {
        caches.keys().then((keys) => keys.forEach((key) => caches.delete(key)));
      }
    }
  }
</script>