feat: pwa and responsive

This commit is contained in:
phattaranun.a committed 2026-09-12 17:21:19 +07:00
1 parent 71dacc0da2
commit 6b612e2758
7 files changed
+269 -18

No files matched your search

+24 -3
View File
@@ -13,11 +13,12 @@
- ขั้นตอนอัปเดต: ดู AGENTS.md หัวข้อ PWA
============================================================ */
const VERSION = "2026.09.02.9"; // ← bump ทุกครั้งที่แก้ SW เอง
const VERSION = "2026.09.12.1"; // ← bump ทุกครั้งที่แก้ SW เอง
const CACHE = `at-transit-${VERSION}`;
const PRECACHE = [
"./index.html",
"./offline.html",
"./busStop.html",
"./busDetail.html",
"./busDetailExpanded.html",
@@ -87,9 +88,14 @@ self.addEventListener("fetch", (event) => {
return;
}
// หน้า HTML + CSS + JS + partials + ข้อมูล JSON → network-first
// หน้า HTML (navigation) → network-first แต่ถ้าออฟไลน์ไม่มี cache → offline.html
if (request.destination === "document") {
event.respondWith(networkFirstDocument(request));
return;
}
// CSS + JS + partials + ข้อมูล JSON → network-first
if (
request.destination === "document" ||
url.pathname.endsWith(".css") ||
url.pathname.endsWith(".js") ||
url.pathname.endsWith(".html") ||
@@ -121,6 +127,21 @@ async function networkFirst(request) {
}
}
/* สำหรับ navigation: ถ้าออฟไลน์และไม่มี cache ให้หน้า offline.html */
async function networkFirstDocument(request) {
const cache = await caches.open(CACHE);
try {
const res = await fetch(request, { cache: "no-store" });
if (res.ok) cache.put(request, res.clone());
return res;
} catch {
const hit = await cache.match(request);
if (hit) return hit;
const offline = await cache.match("./offline.html");
return offline || Response.error();
}
}
async function cacheFirst(request) {
const hit = await caches.match(request);
if (hit) return hit;