/* pwa.js — ลงทะเบียน SW + อัปเดตอัตโนมัติ (ผู้ใช้ไม่ต้องล้างแคช) + ปุ่มติดตั้ง + สถานะออนไลน์ กลไก: 1) register ด้วย updateViaCache:'none' → ไม่เชื่อ HTTP cache ของ sw.js 2) reg.update() ทุกครั้งที่โหลดหน้า + ทุก 30 นาที 3) controllerchange → reload เงียบ ๆ ครั้งเดียวต่อเวอร์ชัน (กัน reload วน) 4) เทียบ app/version.json กับ build ที่ฝังมา → reload เมื่อมี build ใหม่ */ (function () { 'use strict'; var BAKED_VERSION = window.FARM_VERSION || ''; var CHECK_INTERVAL_MS = 30 * 60 * 1000; var VERSION_POLL_MS = 5 * 60 * 1000; /* ---------- ป้าย build ใน footer ---------- */ var buildEl = document.getElementById('build-version'); if (buildEl) buildEl.textContent = BAKED_VERSION || 'dev'; /* ---------- สถานะออนไลน์/ออฟไลน์ ---------- */ var netBadge = document.getElementById('net-badge'); function paintNet() { if (!netBadge) return; var online = navigator.onLine; netBadge.textContent = online ? 'ออนไลน์' : 'ออฟไลน์ — ใช้ข้อมูลที่บันทึกไว้'; netBadge.classList.toggle('badge-net--off', !online); } window.addEventListener('online', paintNet); window.addEventListener('offline', paintNet); paintNet(); /* ---------- ปุ่มกลับขึ้นด้านบน (custom-style เดิมใช้ #myBtn) ---------- */ var topBtn = document.getElementById('myBtn'); if (topBtn) { topBtn.addEventListener('click', function () { window.scrollTo({ top: 0, behavior: 'smooth' }); }); window.addEventListener('scroll', function () { topBtn.style.display = window.scrollY > 20 ? 'block' : 'none'; }); topBtn.style.display = 'none'; } /* ---------- reload ครั้งเดียวต่อเวอร์ชัน ---------- */ function reloadFor(key, guardMs) { var now = Date.now(); var last = 0; try { last = Number(sessionStorage.getItem(key) || 0); } catch (error) { last = 0; } /* กัน reload วน: ถ้าเพิ่ง reload ไปไม่นาน ให้ข้าม */ if (guardMs && now - last < guardMs) return; try { sessionStorage.setItem(key, String(now)); } catch (error) { /* sessionStorage ปิดอยู่ → ยอมให้ reload รอบเดียว */ } window.location.reload(); } /* ---------- service worker ---------- */ if ('serviceWorker' in navigator) { window.addEventListener('load', function () { navigator.serviceWorker .register('./sw.js', { updateViaCache: 'none' }) .then(function (registration) { registration.update(); setInterval(function () { registration.update(); }, CHECK_INTERVAL_MS); /* SW ใหม่อาจปรากฏหลัง register() resolve — ดักที่ updatefound ด้วย ไม่งั้นจะค้างสถานะ waiting จนกว่าจะปิดแท็บทั้งหมด */ registration.addEventListener('updatefound', function () { var incoming = registration.installing; if (!incoming) return; incoming.addEventListener('statechange', function () { if (incoming.state === 'installed' && navigator.serviceWorker.controller) { incoming.postMessage('SKIP_WAITING'); } }); }); navigator.serviceWorker.addEventListener('controllerchange', function () { /* SW ใหม่เข้าควบคุม → โหลดเอกสารใหม่ทันที (กัน reload วนด้วย guard 8 วินาที) */ reloadFor('fp_reload_at', 8000); }); if (registration.waiting) { registration.waiting.postMessage('SKIP_WAITING'); } }) .catch(function () { /* ไม่มี SW = ไม่มีโหมดออฟไลน์ — ตัวเว็บยังทำงานปกติ */ }); }); } /* ---------- ตรวจ version.json (จับกรณีเอกสารเก่าค้างใน cache) ---------- */ function checkVersion() { fetch('app/version.json', { cache: 'no-store' }) .then(function (response) { return response.ok ? response.json() : null; }) .then(function (data) { if (!data || !data.version || !BAKED_VERSION) return; if (data.version !== BAKED_VERSION) reloadFor('fp_reload_ver_' + data.version, 0); }) .catch(function () { /* ออฟไลน์ — เทียบไม่ได้ ไม่เป็นไร */ }); } window.addEventListener('load', checkVersion); document.addEventListener('visibilitychange', function () { if (document.visibilityState === 'visible') checkVersion(); }); setInterval(checkVersion, VERSION_POLL_MS); /* ---------- ปุ่มติดตั้งแอป ---------- */ var installPrompt = null; var installWrap = document.getElementById('install-wrap'); var installBtn = document.getElementById('install-btn'); window.addEventListener('beforeinstallprompt', function (event) { event.preventDefault(); installPrompt = event; if (installWrap) installWrap.hidden = false; }); if (installBtn) { installBtn.addEventListener('click', function () { if (!installPrompt) return; installPrompt.prompt(); installPrompt.userChoice.then(function () { installPrompt = null; if (installWrap) installWrap.hidden = true; }); }); } window.addEventListener('appinstalled', function () { installPrompt = null; if (installWrap) installWrap.hidden = true; }); /* บอกสาเหตุตรง ๆ ถ้าเปิดผ่าน http:// ซึ่งไม่ใช่ secure context */ if (!window.isSecureContext && installWrap) { installWrap.hidden = false; if (installBtn) { installBtn.disabled = true; installBtn.textContent = 'ติดตั้งได้เฉพาะ https หรือ localhost'; } } })();