From 5216789b90514001fcf5e5f64c644374242ed196 Mon Sep 17 00:00:00 2001 From: Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:37:08 +0900 Subject: sw.js: アプリ画面とイベントデータをキャッシュ対象に追加 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit イベントページにイベント用のmanifestとapple-touch-iconを指定し、今日の予定の画面とdata.jsonをキャッシュするようにした。noindexのアプリ画面はサイトマップから除外した。 --- public/sw.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'public') diff --git a/public/sw.js b/public/sw.js index 571f752..64e8255 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,10 +1,14 @@ // 公園ページはドラフト公開停止中のため、公園ページをAPP_SHELLから外し、 // キャッシュを v2 に更新して旧キャッシュを削除する。公開時に元に戻す。 -const CACHE = "kodaira-parks-v2"; +const CACHE = "kodaira-events-v3"; const APP_SHELL = [ "/manifest.webmanifest", + "/manifest-events.webmanifest", "/icons/icon-192.png", "/icons/icon-512.png", + "/icons/event-192.png", + "/icons/event-512.png", + "/kodaira/events/today/", ]; self.addEventListener("install", (event) => { @@ -49,6 +53,22 @@ self.addEventListener("fetch", (event) => { return; } + // アプリ用データ(data.json)は新しい内容を優先し、オフライン時はキャッシュを使う。 + if (url.pathname === "/kodaira/events/data.json") { + event.respondWith( + fetch(request) + .then((response) => { + if (response.ok) { + const copy = response.clone(); + caches.open(CACHE).then((cache) => cache.put(request, copy)); + } + return response; + }) + .catch(() => caches.match(request)), + ); + return; + } + // それ以外はキャッシュ優先。取得できたらキャッシュを更新する。 event.respondWith( caches.match(request).then((cached) => { -- cgit v1.3.1