Rebuild mock as hash-routed SPA with i18n and PWA refresh
- Single-page shell (ui/index.html) + vanilla hash router; each screen is a partial - New #/check schedule view (all stop times open, no collapse) wired to the 'check schedules' CTA; contact-operator box, route map and gallery trimmed from it - Mobile-first responsive.css (375px iPhone 8 up to 27-inch/1440p) - TH/EN i18n system (data-i18n, browser-language detect, topbar switcher) - Shared form controls moved to _shared.css (mu-btn, custom select chevron, focus-visible ring); footer contact column now mailto:[email protected] - SW v9: JS network-first, precache i18n.js + all partials - Legacy pages become redirect stubs to index.html#/...
This commit is contained in:
1 parent
8a6133fd77
commit
c44f79b7d9
41 files changed
+4214
-2359
No files matched your search
@@ -0,0 +1,147 @@
|
||||
/* ============================================================
|
||||
AT Transit — Service Worker
|
||||
กลไกอัปเดตอัตโนมัติ: ผู้ใช้ไม่ต้องล้างแคชเอง
|
||||
------------------------------------------------------------
|
||||
หลักการ:
|
||||
- HTML/CSS/partials/ข้อมูล → network-first (ออนไลน์ = ได้ของใหม่
|
||||
ทุกครั้ง, ออฟไลน์ = ใช้ cache) เนื้อหาเปลี่ยน = เห็นผลทันที
|
||||
- รูป/ฟอนต์/ไอคอน → cache-first (เร็ว + offline)
|
||||
- fetch ทุกครั้งใน SW ใช้ cache:"no-store" → ไม่เชื่อ HTTP cache
|
||||
ของ server (กัน stale จาก server ที่ไม่ส่ง Cache-Control)
|
||||
- cache ใช้ชื่อมีเวอร์ชัน → deploy รอบใหม่ = cache ใหม่
|
||||
และ activate ลบ cache เก่าทิ้งอัตโนมัติ
|
||||
- ขั้นตอนอัปเดต: ดู AGENTS.md หัวข้อ PWA
|
||||
============================================================ */
|
||||
|
||||
const VERSION = "2026.09.02.9"; // ← bump ทุกครั้งที่แก้ SW เอง
|
||||
const CACHE = `at-transit-${VERSION}`;
|
||||
|
||||
const PRECACHE = [
|
||||
"./index.html",
|
||||
"./busStop.html",
|
||||
"./busDetail.html",
|
||||
"./busDetailExpanded.html",
|
||||
"./stopDetail.html",
|
||||
"./credits.html",
|
||||
"./_shared.css",
|
||||
"./responsive.css",
|
||||
"./pwa.js",
|
||||
"./i18n.js",
|
||||
"./router.js",
|
||||
"./partials/home.html",
|
||||
"./partials/home.init.js",
|
||||
"./partials/busStop.html",
|
||||
"./partials/busDetail.html",
|
||||
"./partials/busDetailExpanded.html",
|
||||
"./partials/checkSchedule.html",
|
||||
"./partials/stopDetail.html",
|
||||
"./partials/credits.html",
|
||||
"./partials/icons-sprite.svg",
|
||||
"../resource/images/icons/minibus-512x512.png",
|
||||
];
|
||||
|
||||
/* ---------- install: เข้าควบคุมทันที + precache shell ---------- */
|
||||
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 res = await fetch(url, { cache: "no-store" });
|
||||
if (res.ok) await cache.put(url, res.clone());
|
||||
} catch {
|
||||
/* ไฟล์ไหนโหลดไม่ได้ = ข้ามไป ไม่พังทั้ง install */
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/* ---------- activate: ลบ cache รุ่นเก่าทิ้ง + ยึดหน้ากลับมา ---------- */
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) =>
|
||||
Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))),
|
||||
)
|
||||
.then(() => self.clients.claim()),
|
||||
);
|
||||
});
|
||||
|
||||
/* ---------- fetch: เลือกกลยุทธ์ตามประเภทไฟล์ ---------- */
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const { request } = event;
|
||||
if (request.method !== "GET") return;
|
||||
|
||||
const url = new URL(request.url);
|
||||
|
||||
// ข้าม cross-origin ที่ไม่ใช่ font (Google Fonts) — ปล่อย browser จัดการ
|
||||
if (url.origin !== self.location.origin) {
|
||||
if (url.hostname.includes("fonts.googleapis.com") || url.hostname.includes("fonts.gstatic.com")) {
|
||||
event.respondWith(staleWhileRevalidate(request));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// หน้า HTML + CSS + JS + partials + ข้อมูล JSON → network-first
|
||||
if (
|
||||
request.destination === "document" ||
|
||||
url.pathname.endsWith(".css") ||
|
||||
url.pathname.endsWith(".js") ||
|
||||
url.pathname.endsWith(".html") ||
|
||||
url.pathname.endsWith(".json") ||
|
||||
url.pathname.includes("/partials/")
|
||||
) {
|
||||
event.respondWith(networkFirst(request));
|
||||
return;
|
||||
}
|
||||
|
||||
// รูป ฟอนต์ ไอคอน SVG sprite → cache-first
|
||||
event.respondWith(cacheFirst(request));
|
||||
});
|
||||
|
||||
/* ================= helpers ================= */
|
||||
/* ทุก fetch ใช้ cache:"no-store" เสมอ — ไม่เชื่อ HTTP cache
|
||||
(server บางตัวไม่ส่ง Cache-Control → browser heuristic cache
|
||||
อาจคืนของเก่าโดยไม่แตะ network — กันไว้ที่ SW เลย) */
|
||||
|
||||
async function networkFirst(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);
|
||||
return hit || Response.error();
|
||||
}
|
||||
}
|
||||
|
||||
async function cacheFirst(request) {
|
||||
const hit = await caches.match(request);
|
||||
if (hit) return hit;
|
||||
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 {
|
||||
return Response.error();
|
||||
}
|
||||
}
|
||||
|
||||
async function staleWhileRevalidate(request) {
|
||||
const cache = await caches.open(CACHE);
|
||||
const hit = await cache.match(request);
|
||||
const fetchPromise = fetch(request, { cache: "no-store" })
|
||||
.then((res) => {
|
||||
if (res.ok) cache.put(request, res.clone());
|
||||
return res;
|
||||
})
|
||||
.catch(() => hit);
|
||||
return hit || fetchPromise;
|
||||
}
|
||||
Reference in new issue
Block a user