- ui/pwa.js: register with updateViaCache:'none', check for updates on load, every 30 min, on tab focus and on 'online'; controllerchange reloads the page itself but waits while the user is typing in a form (admin edits survive), retrying on blur/hide - sw.js: precache the maskable icon; bump VERSION -> 2026.11.24.2 - scripts/bump-version.mjs: bump sw.js VERSION + <meta app-version> together, --check fails on drift; wired into > [email protected] check > node --check ui/app.js && node --check ui/data.js && node --check ui/router.js && node --check ui/ui.js && node --check ui/pwa.js && node --check ui/views/home.js && node --check ui/views/admin.js && node --check sw.js && node --check server/publish.mjs && node --check server/db.mjs && node --check scripts/bump-version.mjs && node scripts/bump-version.mjs --check [version] ตรงกันที่ 2026.11.24.2, exposed as > [email protected] version > node scripts/bump-version.mjs [version] ตั้งเป็น 2026.11.24.3 แล้ว (sw.js + index.html) - README/AGENTS: document the PWA behaviour and the bump workflow Verified in Chrome: SPA route changes keep timeOrigin and navigation count at 1 (no reload), a simulated deploy (version bump -> reg.update) replaced the cache from hobbypj-2026.11.24.1 to .2 with the old cache deleted and the page reloading by itself, Page.getAppManifest errors: [], and with the network emulated offline the shell + 6 cards still render while an uncached file fails.
83 lines
4.0 KiB
JavaScript
83 lines
4.0 KiB
JavaScript
/* pwa.js — ลงทะเบียน service worker + เช็ค/รับอัปเดตอัตโนมัติ (ผู้ใช้ไม่ต้องล้างแคช)
|
|
*
|
|
* ห่วงโซ่อัปเดตที่ต้องครบ (ขาดข้อใดข้อหนึ่ง = ค้างเวอร์ชันเก่า):
|
|
* 1. register ด้วย updateViaCache:"none" → เบราว์เซอร์ไม่แคชตัว sw.js เอง
|
|
* 2. reg.update() ตอนเปิดหน้า + ทุก 30 นาที + ตอนกลับมาโฟกัสแท็บ + ตอนต่อเน็ตใหม่
|
|
* 3. sw.js เรียก skipWaiting() ตอน install
|
|
* 4. sw.js เรียก clients.claim() ตอน activate (เข้าคุมแท็บที่เปิดอยู่ทันที)
|
|
* 5. controllerchange → โหลดหน้าใหม่เอง (เลื่อนออกไปถ้าผู้ใช้กำลังพิมพ์ในฟอร์ม)
|
|
*/
|
|
|
|
const SW_URL = "sw.js";
|
|
const UPDATE_INTERVAL_MS = 30 * 60 * 1000;
|
|
|
|
let reloading = false;
|
|
let pendingReload = false;
|
|
|
|
/** กำลังพิมพ์/แก้ค่าอยู่ไหม — ถ้าใช่ เลื่อน reload ออกไปก่อน จะได้ไม่หายข้อความ */
|
|
function isEditing() {
|
|
const el = document.activeElement;
|
|
if (!el || el.disabled) return false;
|
|
const tag = el.tagName;
|
|
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || el.isContentEditable;
|
|
}
|
|
|
|
function applyUpdate() {
|
|
if (reloading) return;
|
|
if (isEditing()) {
|
|
pendingReload = true;
|
|
return;
|
|
}
|
|
reloading = true;
|
|
location.reload();
|
|
}
|
|
|
|
export async function registerServiceWorker() {
|
|
if (!("serviceWorker" in navigator)) return null;
|
|
if (location.protocol !== "https:" && location.hostname !== "localhost" && location.hostname !== "127.0.0.1") {
|
|
// PWA ต้องใช้ HTTPS หรือ localhost เท่านั้น
|
|
return null;
|
|
}
|
|
|
|
// อัปเดตที่เลื่อนไว้ (ตอนผู้ใช้กำลังพิมพ์) → ทำต่อเมื่อเลิกพิมพ์หรือสลับแท็บ
|
|
document.addEventListener("focusout", () => {
|
|
if (!pendingReload) return;
|
|
setTimeout(() => {
|
|
if (!isEditing()) applyUpdate();
|
|
}, 400);
|
|
});
|
|
document.addEventListener("visibilitychange", () => {
|
|
if (pendingReload && document.visibilityState === "hidden") applyUpdate();
|
|
});
|
|
|
|
try {
|
|
const registration = await navigator.serviceWorker.register(SW_URL, {
|
|
scope: "./",
|
|
updateViaCache: "none",
|
|
});
|
|
|
|
// เช็คอัปเดตทันทีที่เปิดหน้า และซ้ำทุก 30 นาที (เบราว์เซอร์เช็คเองทุก 24 ชม. ซึ่งช้าเกินไป)
|
|
registration.update().catch(() => {});
|
|
setInterval(() => registration.update().catch(() => {}), UPDATE_INTERVAL_MS);
|
|
|
|
// กลับมาโฟกัสแท็บ (แท็บที่เปิดค้างข้ามวัน) และตอนต่อเน็ตใหม่
|
|
document.addEventListener("visibilitychange", () => {
|
|
if (document.visibilityState === "visible") registration.update().catch(() => {});
|
|
});
|
|
window.addEventListener("online", () => registration.update().catch(() => {}));
|
|
|
|
// service worker ใหม่เข้าคุมแล้ว → โหลดหน้าใหม่เอง (กันลูปด้วย flag ใน applyUpdate)
|
|
navigator.serviceWorker.addEventListener("controllerchange", () => {
|
|
if (!navigator.serviceWorker.controller) return;
|
|
applyUpdate();
|
|
});
|
|
|
|
return registration;
|
|
} catch (error) {
|
|
console.warn("ลงทะเบียน service worker ไม่สำเร็จ", error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
registerServiceWorker();
|