Files
web-at-transit-mock/ui/sw.js
T
Bkk1234 1cfaeb1526 ตารางเวลาคำนวณได้ + หน้ารายละเอียดป้ายขับเคลื่อนด้วยข้อมูล + แผนที่ Leaflet
Schedules — กติกา "จุดนี้ = เวลาจุดอ้างอิง ± n นาที"
- ฟิลด์ derive { from_station_id, offset_min } คำนวณที่ ui/partials/schedules.js (ท่อเดียว) รองรับอ้างจุดข้ามทิศ
- จุดย่อยของ "8 other_stop" ใช้ offsets_min จับคู่ตามลำดับ (เช่น ตลาดสามโก้ = ตลาดท่าช้าง +45 นาที)
- เครื่องมือ resource/tools/derive-times.py + ตาราง resource/data/derive-times.csv (--check / --audit ชี้จุดที่ควรตั้งกฎ)
- แยกตารางเป็นรายสาย: scheduleslist/schedules<route_id>.json และ schedules.json เป็นสารบัญ (โหลดแบบ lazy + cache)
- ตรวจจับวันทำงาน/วันหยุด + schema 3 (weekday/holiday → directions) · ไฟล์ข้อมูลเป็น CRLF ทั้งชุด
- ล้างค่าที่ไม่ใช่ HH:MM ที่ฝังใน times (ทำให้ KPI เพี้ยน) + กันค่าหลอกในตัวสรุปเวลา

UI
- หน้ารายละเอียดป้าย/ตารางเวลา ขับเคลื่อนด้วย JSON (stopDetail.init.js) · จุดขึ้นรถประจำทางอื่นๆ รองรับหลายจุดต่อสาย
- #/stops: แสดง 6 ป้ายแรก + ปุ่ม "แสดงป้ายทั้งหมด" (โหลดรูปเฉพาะที่แสดง) · ชิปสาย → #/routes/<route_id>
- หน้าแรก: ตัดตัวเลือก "ทุกสถานีในจังหวัดนี้" · ตัวกรองจังหวัด/สถานี + ปุ่มแชร์ (share URL)
- การ์ดติดต่อ: ไอคอน/ชื่อ/สีเปลี่ยนตามชนิด URL (Facebook / LINE / เว็บไซต์) + ปุ่มเปิดลิงก์จาก web_url
- รูปรองรับหลายนามสกุล (.jpg → .jpeg → .webp → .png) · ป้าย "ประมาณ" + หมายเหตุสำหรับตารางที่คำนวณ
- หน้าเครดิตเหลือ 3 หมวด · แก้ข้อความหมวดแผนที่ให้ตรงกับข้อมูลจริง
- แผนที่ Leaflet (vendor ในโปรเจกต์ ไม่ใช้ CDN): เส้นทาง + หมุด + ป๊อปอัป · แก้ CSS ที่ทำให้ SVG ยุบ 0x0

เครื่องมือและเอกสาร
- qgis-to-web.py อ่าน .shp ตรง → geojson-web (สร้างใหม่ 10 ไฟล์) + audit-geojson.py ตรวจ 3 ส่วน
- stop-km.py วัดระยะ กม. ตามเส้นจริง → stop-km.json · schedules-editor.html (แก้ทีละไฟล์ · มุมมองตารางแบบ Excel)
- AGENTS.md: กติกา derive · offsets_min · นามสกุลรูป · ชิปสาย · หน้ารายละเอียดป้ายมาจากไหน
2026-09-30 03:25:18 +07:00

187 lines
6.7 KiB
JavaScript

/* ============================================================
Atroute — 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.27.15"; // ← bump ทุกครั้งที่แก้ SW เอง
const CACHE = `at-transit-${VERSION}`;
const PRECACHE = [
"./index.html",
"./offline.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/busStop.init.js",
"./partials/busStop.search.js",
"./partials/busDetail.html",
"./partials/busDetailExpanded.html",
"./partials/checkSchedule.html",
"./partials/stopDetail.html",
"./partials/stopDetail.init.js",
"./partials/credits.html",
"./partials/about.html",
"./partials/about.init.js",
"./partials/blog.html",
"./partials/blog.init.js",
"./partials/busDetail.init.js",
"./partials/checkSchedule.init.js",
"./partials/schedules.js",
"../resource/data/holidays.json",
"./partials/otherStop.js",
"./vendor/leaflet/leaflet.js",
"./vendor/leaflet/leaflet.css",
"./map.js",
"./map.css",
"./stops/chatuchak-minibus-terminal.html",
"./partials/icons-sprite.svg",
"../.image/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 (navigation) → network-first แต่ถ้าออฟไลน์ไม่มี cache → offline.html
if (request.destination === "document") {
event.respondWith(networkFirstDocument(request));
return;
}
// CSS + JS + partials + ข้อมูล JSON → network-first
if (
url.pathname.endsWith(".css") ||
url.pathname.endsWith(".js") ||
url.pathname.endsWith(".html") ||
url.pathname.endsWith(".json") ||
url.pathname.endsWith(".geojson") ||
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();
}
}
/* สำหรับ 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;
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;
}