/* sw.js — service worker (auto-update) สำคัญ: bump VERSION ทุกครั้งที่แก้ไฟล์นี้ และต้องตรงกับ app/version.js + app/version.json กลไก: skipWaiting (install) → clients.claim (activate) → หน้าเว็บ reload เองที่ controllerchange ทุก fetch ใช้ cache:"no-store" เพราะ server ที่ไม่ส่ง Cache-Control (python http.server) จะทำให้ HTTP heuristic cache คืนของเก่าให้ SW */ const VERSION = "2026.11.16.7"; const CACHE = `farm-planning-${VERSION}`; const PRECACHE = [ "./index.html", "./manifest.json", "./assets/css/app.css", "./css/creative.css", "./css/legacy.css", "./coding/css/custom.css", "./coding/css/gototop.css", "./coding/vendor/bootstrap/css/bootstrap.min.css", "./coding/vendor/bootstrap/js/bootstrap.bundle.min.js", "./coding/vendor/jquery/jquery.min.js", "./app/version.js", "./app/version.json", "./app/router.js", "./app/pwa.js", "./core/crop_water.js", "./core/pond.js", "./data/climate/eto_by_region.json", "./partials/home.html", "./partials/crop-water.html", "./partials/pond.html", "./partials/irrigation.html", "./partials/glossary.html", "./partials/contact.html", "./partials/credits.html", "./partials/offline.html", "./assets/icons/icon-192.png", "./assets/icons/icon-512.png", "./assets/icons/icon-maskable-512.png", "./assets/icons/apple-touch-icon-180.png", "./logo/logo-farm-planning.ico", "./logo/logo%20%E0%B9%83%E0%B8%AB%E0%B8%A1%E0%B9%88/2566-09-08%2011-51-40.png", "./flat%20icon/smart-farm%20v2.svg", "./flat%20icon/sprinkler.svg", "./flat%20icon/groundwater.svg", "./flat%20icon/water-cycle.svg", "./flat%20icon/river.svg", "./flat%20icon/canal.svg", "./flat%20icon/retention%20pond.svg", "./flat%20icon/calendar.svg", "./flat%20icon/water%20cost.svg", "./flat%20icon/newspaper.svg", "./flat%20icon/watershaed.svg", "./flat%20icon/accounting.svg", "./flat%20icon/pool.svg", "./flat%20icon/book.svg" ]; self.addEventListener("install", (event) => { self.skipWaiting(); event.waitUntil(precacheAll(PRECACHE)); }); async function precacheAll(urls) { const cache = await caches.open(CACHE); await Promise.all( urls.map(async (url) => { try { const response = await fetch(url, { cache: "no-store" }); if (response.ok) await cache.put(url, response.clone()); } catch (error) { /* ไฟล์ไหนโหลดไม่ได้ = ข้ามไป ไม่ทำให้ install ล้มทั้งชุด */ } }), ); } 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("message", (event) => { if (event.data === "SKIP_WAITING") self.skipWaiting(); }); self.addEventListener("fetch", (event) => { const { request } = event; if (request.method !== "GET") return; const url = new URL(request.url); if (url.origin !== self.location.origin) { /* ฟอนต์ Google ปล่อยให้ browser จัดการ — ไม่อยากให้ cache โตโดยไม่จำเป็น */ return; } const isDocument = request.destination === "document"; const isText = url.pathname.endsWith(".html") || url.pathname.endsWith(".css") || url.pathname.endsWith(".js") || url.pathname.endsWith(".json") || url.pathname.startsWith("/partials/") || url.pathname.startsWith("/app/") || url.pathname.startsWith("/core/") || url.pathname.startsWith("/data/"); if (isDocument || isText) { event.respondWith(networkFirst(request)); return; } event.respondWith(cacheFirst(request)); }); /* ---------- helpers (ทุกตัวรองรับ ?v= ของ shell assets) ---------- */ async function matchLoose(cache, request) { const hit = await cache.match(request); if (hit) return hit; return cache.match(request, { ignoreSearch: true }); } async function networkFirst(request) { const cache = await caches.open(CACHE); try { const response = await fetch(request, { cache: "no-store" }); if (response.ok) cache.put(request, response.clone()); return response; } catch (error) { const hit = await matchLoose(cache, request); if (hit) return hit; if (request.destination === "document") { const shell = await cache.match("./index.html", { ignoreSearch: true }); if (shell) return shell; } return Response.error(); } } async function cacheFirst(request) { const cache = await caches.open(CACHE); const hit = await matchLoose(cache, request); if (hit) return hit; try { const response = await fetch(request, { cache: "no-store" }); if (response.ok) cache.put(request, response.clone()); return response; } catch (error) { return Response.error(); } }