Files
hobbypj-contents/sw.js
T
Bkk1234 2edbc39805 feat(privacy): privacy page, security headers, Chrome fallback for Samsung installs
The 'privacy' refusal on the S23 is Google Play Protect blocking the WebAPK that
Samsung Internet mints (old targetSdkVersion) — a browser-side bug that also hits
other PWAs; Chrome on the same device installs fine. So:

- ui/install-tip.js: detect Samsung Internet on Android and offer the same URL in
  Chrome (intent:// link, copy-link fallback, dismissable, remembered)
- ui/views/privacy.js + #/privacy route + footer link: Thai privacy policy (what is
  stored locally, no cookies/analytics/third-party scripts, how to delete)
- server/publish.mjs: CSP (script-src 'self', style-src 'self' 'unsafe-inline'),
  Referrer-Policy, Permissions-Policy, X-Content-Type-Options on every response
- ui/boot-motion.js: the head inline script moved out so CSP can forbid inline JS
- ui/data.js: admin publish token now lives in sessionStorage (was localStorage)
- theme.css: .notice / .legal components (44px targets), DESIGN.md + README + AGENTS

Verified: installabilityErrors [], fonts load under CSP, all requests same-origin,
notice renders under the header at 360px with 44px buttons and stays dismissed,
privacy page renders 5 sections. VERSION 2026.11.24.6.
2026-11-24 15:10:32 +07:00

119 lines
3.6 KiB
JavaScript

/* sw.js — service worker: ออฟไลน์ได้ + อัปเดตอัตโนมัติ
กติกา: bump VERSION เฉพาะเมื่อแก้ไฟล์นี้หรือรายการ PRECACHE
เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first จึงไม่ต้อง bump */
const VERSION = "2026.11.24.6";
const CACHE = `hobbypj-${VERSION}`;
const PRECACHE = [
"./",
"./index.html",
"./manifest.json",
"./assets/css/theme.css",
"./assets/css/home.css",
"./assets/css/admin.css",
"./assets/css/motion.css",
"./ui/app.js",
"./ui/router.js",
"./ui/ui.js",
"./ui/data.js",
"./ui/auth.js",
"./ui/pwa.js",
"./ui/boot-motion.js",
"./ui/install-tip.js",
"./ui/motion.js",
"./ui/views/home.js",
"./ui/views/admin.js",
"./ui/views/privacy.js",
"./data/projects.json",
"./config/admin-credentials.json",
"./icons/icon-192.png",
"./icons/icon-512.png",
"./icons/icon-512-maskable.png",
"./fonts/k2d-400-thai.woff2",
"./fonts/k2d-700-thai.woff2",
];
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 {
// fetch แบบ no-store เพื่อไม่ให้ HTTP cache ของเบราว์เซอร์ใส่ของเก่าลง precache
const res = await fetch(url, { cache: "no-store" });
if (res.ok) await cache.put(url, res.clone());
} catch {
/* ไฟล์ใดไฟล์หนึ่งพลาดต้องไม่ทำให้ install ทั้งหมดล้ม */
}
}),
);
}
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) => Promise.all(keys.filter((key) => key !== CACHE).map((key) => caches.delete(key))))
.then(() => self.clients.claim()),
);
});
self.addEventListener("fetch", (event) => {
const { request } = event;
if (request.method !== "GET") return;
const url = new URL(request.url);
if (url.origin !== self.location.origin) return;
// อย่าแตะ API: คำขอเผยแพร่/อ่านข้อมูลสดต้องถึงเซิร์ฟเวอร์เสมอ
if (url.pathname.includes("/api/")) return;
const isContent =
request.destination === "document" ||
url.pathname.endsWith(".html") ||
url.pathname.endsWith(".css") ||
url.pathname.endsWith(".js") ||
url.pathname.endsWith(".json") ||
url.pathname === "/" ||
url.pathname.endsWith("/");
if (isContent) {
event.respondWith(networkFirst(request));
return;
}
event.respondWith(cacheFirst(request));
});
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)) || (await caches.match(request, { ignoreSearch: true }));
if (hit) return hit;
if (request.destination === "document") return (await cache.match("./index.html")) || Response.error();
return 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();
}
}