Files
web-at-transit-mock/ui/sw.js
T
Bkk1234 74d605f1c4 ระบบ affiliate พาร์ทเนอร์ + นับผู้เข้าชม + สายที่เปิดดูบ่อย + ความปลอดภัยรูป
== ระบบพาร์ทเนอร์ (affiliate) ==
- resource/data/affiliates.json — ข้อมูล 11 โปรแกรม (8 ใน footer) + ที่วางลิงก์แนะนำ
- home.init.js: initSponsors() สร้างไทล์ marquee จาก JSON (ไม่ฮาร์ดโค้ดใน HTML)
    · มี affiliate_link → <a target=_blank rel="noopener sponsored">
    · ยังไม่มีลิงก์   → <span> แสดงรูปเฉย ๆ (กดไม่ได้ ไม่พาไปหน้าเปล่า)
    · โหลด JSON ไม่ได้ → คงไทล์ fallback ที่ฝังใน HTML (8 ไทล์)
- เติมลิงก์ affiliate แล้ว 6 แบรนด์: 7-Eleven · Shopee · Lazada · airasia MOVE ·
  busonlineticket · Trip.com

== ระบบสถิติ (ฝั่งเบราว์เซอร์ ไม่มี backend) ==
- ui/popular.js — "สายที่เปิดดูบ่อย" ใน footer
    · score = hits × 0.5^(อายุวัน/30) — กันคะแนนเฟ้อ (ครึ่งชีวิต 30 วัน)
    · ทิ้งข้อมูลที่ตายเกิน 180 วันอัตโนมัติ · เก็บ 2 ตัวเลข/สาย (~24 ไบต์)
    · ไม่มีข้อมูล → ใช้ค่าเริ่มต้น 101/902/15/464
- ui/visitors.js — นับผู้เข้าชมจริง (ฐาน 12,834)
    · กันเฟ้อ 3 ชั้น: sessionStorage (F5 ไม่นับ) → 1 ครั้ง/วัน → dayKey กันล้าง session
    · กดตัวเลข 5 ครั้ง → ช่องกรอกรหัส (SHA-256 ไม่เก็บรหัสจริงในโค้ด) → รีเซ็ตเป็น 0
- หัวข้อ footer เปลี่ยนเป็น "สายที่เปิดดูบ่อย" / "Most Viewed Routes"

== ความปลอดภัยของรูป ==
- images.zip: ลบ EXIF/GPS ทั้งหมด 12 ไฟล์ (piexif — ไม่แตะ pixel data, คง ICC profile)
    · ตัด .image/partners/_original/ ออก (ไฟล์สำรองที่ไม่ควรแจก)
    · ตรวจแล้ว: GPS=0 · EXIF=0 · รูปเสีย=0 · 39.4 MB → 37 MB
- tcl999-angthong.jpg ย่อ 4032×3024 (2.5 MB) → 700×525 (86 KB) สำหรับใช้บนเว็บ

== UI ==
- nav (mu-topbar) z-index 50 → 1100 + isolation:isolate ที่กรอบแผนที่
  (Leaflet ใช้ z-index ถึง 1000 — เดิมปุ่มซูม/ป๊อปอัปทะลุขึ้นทับ nav)
- รูปสำรองสถานี: ไม่มีรูป → แสดง minibus-512x512 (.is-fallback ใช้ object-fit:contain)
- affiliate.json เป็น CRLF ตาม pattern เดิมของโปรเจกต์
2026-10-07 23:29:36 +07:00

210 lines
7.5 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.94"; // ← bump ทุกครั้งที่แก้ SW เอง
const CACHE = `at-transit-${VERSION}`;
const PRECACHE = [
"./index.html",
"./offline.html",
"./busStop.html",
"./busDetail.html",
"./stopDetail.html",
"./credits.html",
"./_shared.css",
"./responsive.css",
"./imageurl.js",
"./locales/th.json",
"./locales/en.json",
"./pwa.js",
"./i18n.js",
"./popular.js",
"./visitors.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/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",
"../resource/data/8 other_stop.json",
"./partials/otherStop.js",
"./vendor/fonts/k2d.css",
"./vendor/fonts/k2d-300-latin-ext.woff2",
"./vendor/fonts/k2d-300-latin.woff2",
"./vendor/fonts/k2d-300-thai.woff2",
"./vendor/fonts/k2d-400-latin-ext.woff2",
"./vendor/fonts/k2d-400-latin.woff2",
"./vendor/fonts/k2d-400-thai.woff2",
"./vendor/fonts/k2d-500-latin-ext.woff2",
"./vendor/fonts/k2d-500-latin.woff2",
"./vendor/fonts/k2d-500-thai.woff2",
"./vendor/fonts/k2d-600-latin-ext.woff2",
"./vendor/fonts/k2d-600-latin.woff2",
"./vendor/fonts/k2d-600-thai.woff2",
"./vendor/fonts/k2d-700-latin-ext.woff2",
"./vendor/fonts/k2d-700-latin.woff2",
"./vendor/fonts/k2d-700-thai.woff2",
"./vendor/fonts/k2d-800-latin-ext.woff2",
"./vendor/fonts/k2d-800-latin.woff2",
"./vendor/fonts/k2d-800-thai.woff2",
"./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;
}