aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/PwaRegister.astro
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/PwaRegister.astro')
-rw-r--r--src/components/PwaRegister.astro19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/components/PwaRegister.astro b/src/components/PwaRegister.astro
new file mode 100644
index 0000000..82359a4
--- /dev/null
+++ b/src/components/PwaRegister.astro
@@ -0,0 +1,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>