- landing page: welcome hero, live search, project card grid (10 projects from owner list) - warm brown/white theme (fastwork-inspired layout), embedded K2D Thai font - PWA: manifest, PIL-generated icons, versioned service worker with auto-update - backoffice at #/admin: edit name/description/url, reorder, delete, publish - server/publish.mjs: zero-dependency static server + PUT api/projects with validation, JSON write, git commit and push; loopback-only unless ADMIN_TOKEN - docs: README, DESIGN.md (color/type contract), AGENTS.md
40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
/* pwa.js — ลงทะเบียน service worker + เช็คอัปเดตอัตโนมัติ (ผู้ใช้ไม่ต้องล้างแคช) */
|
|
|
|
const SW_URL = "sw.js";
|
|
let reloading = false;
|
|
|
|
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;
|
|
}
|
|
try {
|
|
const registration = await navigator.serviceWorker.register(SW_URL, {
|
|
scope: "./",
|
|
updateViaCache: "none",
|
|
});
|
|
|
|
// เช็คอัปเดตทันทีที่เปิดหน้า และซ้ำทุก 30 นาที (เบราว์เซอร์เช็คเองทุก 24 ชม. ซึ่งช้าเกินไป)
|
|
registration.update().catch(() => {});
|
|
setInterval(() => registration.update().catch(() => {}), 30 * 60 * 1000);
|
|
|
|
navigator.serviceWorker.addEventListener("controllerchange", () => {
|
|
if (reloading) return;
|
|
reloading = true;
|
|
location.reload();
|
|
});
|
|
|
|
document.addEventListener("visibilitychange", () => {
|
|
if (document.visibilityState === "visible") registration.update().catch(() => {});
|
|
});
|
|
|
|
return registration;
|
|
} catch (error) {
|
|
console.warn("ลงทะเบียน service worker ไม่สำเร็จ", error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
registerServiceWorker();
|