aboutsummaryrefslogtreecommitdiffhomepage
path: root/public/sw.js
diff options
context:
space:
mode:
authorYasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com>2026-10-24 12:29:24 +0900
committerYasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com>2026-10-24 12:29:24 +0900
commita71727538f65ab4f10f5239550be82590f9d47d1 (patch)
tree4608d37faead4f253c3311982a400a8fb88fba9e /public/sw.js
parentc7efe674b34a2949b76d62bb71a972427965af92 (diff)
parks: 公園一覧と地図、公共施設の情報を更新
- 近隣公園のクリックを市内公園と同じ挙動にし、Googleマップを公園名で開く - 公共施設のトイレを○にし、ルネこだいらを追加 - 未取得だった施設の座標を追加し、再ジオコーディング時の上書きを防止 - 手入力ポリゴン、近隣公園、PWA、ライセンス表示を追加
Diffstat (limited to 'public/sw.js')
-rw-r--r--public/sw.js70
1 files changed, 70 insertions, 0 deletions
diff --git a/public/sw.js b/public/sw.js
new file mode 100644
index 0000000..99af941
--- /dev/null
+++ b/public/sw.js
@@ -0,0 +1,70 @@
+const CACHE = "kodaira-parks-v1";
+const APP_SHELL = [
+ "/kodaira-data/parks/",
+ "/manifest.webmanifest",
+ "/icons/icon-192.png",
+ "/icons/icon-512.png",
+];
+
+self.addEventListener("install", (event) => {
+ event.waitUntil(
+ caches
+ .open(CACHE)
+ .then((cache) => cache.addAll(APP_SHELL))
+ .then(() => self.skipWaiting()),
+ );
+});
+
+self.addEventListener("activate", (event) => {
+ event.waitUntil(
+ caches
+ .keys()
+ .then((keys) =>
+ Promise.all(keys.filter((key) => key !== CACHE).map((key) => caches.delete(key))),
+ )
+ .then(() => self.clients.claim()),
+ );
+});
+
+self.addEventListener("fetch", (event) => {
+ const request = event.request;
+ if (request.method !== "GET") return;
+
+ const url = new URL(request.url);
+ // OSMのタイルなど外部オリジンのものはキャッシュしない。
+ if (url.origin !== self.location.origin) return;
+
+ // ページ遷移はネットワーク優先。オフライン時はキャッシュ→公園ページ。
+ if (request.mode === "navigate") {
+ event.respondWith(
+ fetch(request)
+ .then((response) => {
+ const copy = response.clone();
+ caches.open(CACHE).then((cache) => cache.put(request, copy));
+ return response;
+ })
+ .catch(() =>
+ caches
+ .match(request)
+ .then((cached) => cached || caches.match("/kodaira-data/parks/")),
+ ),
+ );
+ return;
+ }
+
+ // それ以外はキャッシュ優先。取得できたらキャッシュを更新する。
+ event.respondWith(
+ caches.match(request).then((cached) => {
+ const network = fetch(request)
+ .then((response) => {
+ if (response.ok) {
+ const copy = response.clone();
+ caches.open(CACHE).then((cache) => cache.put(request, copy));
+ }
+ return response;
+ })
+ .catch(() => cached);
+ return cached || network;
+ }),
+ );
+});