diff --git a/AGENTS.md b/AGENTS.md
index 746dffa..2975893 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -10,7 +10,8 @@ Repository: `hobbypj-contents` (Gitea org `hobbyproject-list`) · deploy เป
```bash
node server/publish.mjs # เซิร์ฟเวอร์ + หลังบ้าน → http://localhost:8090
-npm run check # ตรวจ syntax ทุกไฟล์ JS/MJS
+npm run check # ตรวจ syntax ทุกไฟล์ JS/MJS + เวอร์ชัน sw.js/index.html ตรงกัน
+npm run version # bump VERSION (sw.js) + (index.html)
PORT=8090 ADMIN_TOKEN=xxx node server/publish.mjs # โหมดมีรหัสเผยแพร่
PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไม่ push
```
@@ -23,7 +24,7 @@ PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไ
4. **สีต้องมาจาก `assets/css/theme.css`** — ห้ามใส่ hex ใหม่ในไฟล์ view; ถ้าต้องเพิ่มสี ให้เพิ่มเป็นโทเคน + อัปเดต `DESIGN.md`
5. **ตัวอักษรต้อง ≥ 4.5:1** บนพื้นของตัวเอง — เพิ่มสีใหม่ต้องคำนวณคอนทราสต์ ไม่เดาจากสายตา
6. **ปุ่ม/ลิงก์ต้องมีพื้นที่กด ≥ 44×44px** และมี `aria-label` เมื่อเป็นไอคอนเดี่ยว
-7. **แก้ `sw.js` หรือรายการ `PRECACHE` → bump `VERSION`** ใน `sw.js` และ `` ใน `index.html` ให้ตรงกัน (แก้ HTML/CSS/JSON อย่างเดียวไม่ต้อง bump)
+7. **แก้ `sw.js` หรือรายการ `PRECACHE` → bump เวอร์ชันด้วย `npm run version`** (แก้ `VERSION` ใน `sw.js` + `` ใน `index.html` พร้อมกัน) · `npm run check` มี `bump-version.mjs --check` ตรวจว่าไม่หลุดกัน · แก้ HTML/CSS/JS/JSON เนื้อหาอย่างเดียวไม่ต้อง bump (network-first จัดการ)
8. **PWA boot บน plain HTTP ไม่ได้** — ต้อง HTTPS หรือ localhost
9. การเปลี่ยนคำ/ค่าออกแบบ → อัปเดต `DESIGN.md` และ `README.md` (เครดิต) ด้วยในคอมมิตเดียวกัน
@@ -43,6 +44,7 @@ PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไ
| `ui/views/admin.js` | หลังบ้าน: ล็อกอิน → ฟอร์มแก้ไข, เรียงลำดับ, ลบ, เผยแพร่, ดาวน์โหลด JSON |
| `server/publish.mjs` | static server + `GET/PUT api/projects` + git commit/push |
| `server/db.mjs` | ชั้น SQLite (`node:sqlite`): schema, `readData`/`saveData` (ทรานแซกชัน), `backupDatabase` (VACUUM INTO), `exportJson`, `seedFromJson` |
+| `scripts/bump-version.mjs` | bump เวอร์ชันคู่ (`sw.js` VERSION + ``) · `--check` ใช้ใน `npm run check` |
| `data/hobbypj.sqlite` | ฐานข้อมูลจริง (ตาราง `site` 1 แถว + `projects`) — **ไม่ขึ้น git** |
| `data/projects.json` | สำเนา export ของฐานข้อมูล (ขึ้น git, service worker precache, ใช้อ่านตอนไม่มีเซิร์ฟเวอร์) |
diff --git a/README.md b/README.md
index 19bef63..70aca65 100644
--- a/README.md
+++ b/README.md
@@ -63,6 +63,19 @@ repo นี้ deploy เป็น **Compose application** บน Dokploy (serve
ถ้าได้ 404 = ยังไม่มี domain ในแท็บ Domains · 502 = container ไม่ขึ้นหรือ port ไม่ตรง (`docker ps` ใน server: `hobbypj-contents-hobbypj-contents-1`)
+## PWA: ติดตั้งได้ + อัปเดตแคชเอง (ไม่มี reload ตอนเปลี่ยนหน้า)
+
+- **หน้าเดียว**: hash router (`#/`, `#/admin`) — เปลี่ยนหน้าไม่โหลดใหม่ (`performance.navigation` ไม่เพิ่ม, `timeOrigin` เดิม)
+- **ติดตั้งได้**: `manifest.json` (`display: standalone`, ไอคอน 192/512 + maskable) ลิงก์ใน `index.html`
+- **ออฟไลน์ได้**: `sw.js` precache shell ไว้ครบ เปิดแบบไม่มีเน็ตยังเห็นรายการ (ข้อมูลจากสำเนา JSON/localStorage)
+- **อัปเดตเอง**: `ui/pwa.js` ลงทะเบียนด้วย `updateViaCache: "none"` แล้ว `update()` ตอนเปิดหน้า / ทุก 30 นาที / กลับมาโฟกัสแท็บ / ต่อเน็ตใหม่ · `sw.js` ใช้ `skipWaiting()` + `clients.claim()` → `controllerchange` สั่ง reload หน้าเอง (ชะงักเฉพาะตอนกำลังพิมพ์ในฟอร์ม แล้วทำต่อเมื่อเลิกพิมพ์)
+- cache เวอร์ชันเก่าถูกลบใน `activate` → ผู้ใช้ไม่ต้องล้างแคชเอง
+
+```bash
+npm run version # bump VERSION (sw.js) + (index.html) พร้อมกัน
+npm run check # มี --check ตรวจสองค่านี้ไม่ให้หลุดกัน
+```
+
## ระบบหลังบ้านทำงานอย่างไร
```
diff --git a/index.html b/index.html
index c9f070f..e6d47bb 100644
--- a/index.html
+++ b/index.html
@@ -22,7 +22,7 @@
content="รายชื่อโปรเจคอดิเรกของเรา เลือกโปรเจคที่อยากดูได้เลย"
/>
-
+
diff --git a/package.json b/package.json
index d47b7e8..6a024f0 100644
--- a/package.json
+++ b/package.json
@@ -6,6 +6,7 @@
"description": "หน้ารายชื่อโปรเจคอดิเรก (PWA + SPA) พร้อมหลังบ้านแก้ไขรายการ",
"scripts": {
"start": "node --disable-warning=ExperimentalWarning server/publish.mjs",
- "check": "node --check ui/app.js && node --check ui/data.js && node --check ui/router.js && node --check ui/ui.js && node --check ui/pwa.js && node --check ui/views/home.js && node --check ui/views/admin.js && node --check sw.js && node --check server/publish.mjs && node --check server/db.mjs"
+ "check": "node --check ui/app.js && node --check ui/data.js && node --check ui/router.js && node --check ui/ui.js && node --check ui/pwa.js && node --check ui/views/home.js && node --check ui/views/admin.js && node --check sw.js && node --check server/publish.mjs && node --check server/db.mjs && node --check scripts/bump-version.mjs && node scripts/bump-version.mjs --check",
+ "version": "node scripts/bump-version.mjs"
}
}
diff --git a/scripts/bump-version.mjs b/scripts/bump-version.mjs
new file mode 100644
index 0000000..0722c40
--- /dev/null
+++ b/scripts/bump-version.mjs
@@ -0,0 +1,100 @@
+#!/usr/bin/env node
+/**
+ * scripts/bump-version.mjs — bump เวอร์ชันแอปให้ตรงกันสองที่
+ *
+ * node scripts/bump-version.mjs # bump เป็นวันนี้ +1 (เช่น 2026.11.23.2)
+ * node scripts/bump-version.mjs --set 2026.12.01.1
+ * node scripts/bump-version.mjs --check # ตรวจว่า sw.js VERSION === (exit 1 ถ้าไม่ตรง)
+ *
+ * สองที่ที่ต้องตรงกัน: `VERSION` ใน sw.js (ชื่อ cache) และ `` ใน index.html
+ * bump เฉพาะเมื่อแก้ logic/PRECACHE ของ sw.js — แก้เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first ไม่ต้อง bump
+ */
+
+import { readFile, writeFile } from "node:fs/promises";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
+const SW_FILE = path.join(ROOT, "sw.js");
+const HTML_FILE = path.join(ROOT, "index.html");
+
+const SW_RE = /const VERSION = "([^"]+)";/;
+const META_RE = //;
+const SEMVER_RE = /^\d{4}\.\d{2}\.\d{2}\.\d+$/;
+
+function today() {
+ const now = new Date();
+ const pad = (n) => String(n).padStart(2, "0");
+ return `${now.getFullYear()}.${pad(now.getMonth() + 1)}.${pad(now.getDate())}`;
+}
+
+function nextVersion(current) {
+ const base = today();
+ const match = String(current || "").match(/^(\d{4}\.\d{2}\.\d{2})\.(\d+)$/);
+ if (match && match[1] === base) return `${base}.${Number(match[2]) + 1}`;
+ return `${base}.1`;
+}
+
+async function readPair() {
+ const [sw, html] = await Promise.all([readFile(SW_FILE, "utf8"), readFile(HTML_FILE, "utf8")]);
+ const swMatch = sw.match(SW_RE);
+ const metaMatch = html.match(META_RE);
+ return {
+ sw,
+ html,
+ swVersion: swMatch ? swMatch[1] : null,
+ metaVersion: metaMatch ? metaMatch[1] : null,
+ };
+}
+
+async function check() {
+ const { swVersion, metaVersion } = await readPair();
+ if (!swVersion) {
+ console.error("[version] หา `const VERSION = \"…\"` ใน sw.js ไม่เจอ");
+ process.exit(1);
+ }
+ if (!metaVersion) {
+ console.error('[version] หา ใน index.html ไม่เจอ');
+ process.exit(1);
+ }
+ if (swVersion !== metaVersion) {
+ console.error(`[version] ไม่ตรงกัน: sw.js = ${swVersion} · index.html = ${metaVersion}`);
+ console.error("[version] แก้ด้วย `node scripts/bump-version.mjs` หรือ `--set <เวอร์ชัน>`");
+ process.exit(1);
+ }
+ console.log(`[version] ตรงกันที่ ${swVersion}`);
+}
+
+async function setVersion(target) {
+ if (!SEMVER_RE.test(target)) {
+ console.error(`[version] รูปแบบไม่ถูกต้อง: ${target} (ต้องเป็น YYYY.MM.DD.N)`);
+ process.exit(1);
+ }
+ const { sw, html } = await readPair();
+ const nextSw = sw.replace(SW_RE, `const VERSION = "${target}";`);
+ const nextHtml = html.replace(META_RE, ``);
+ if (nextSw === sw && nextHtml === html) {
+ console.log(`[version] เป็น ${target} อยู่แล้ว ไม่ต้องแก้`);
+ return;
+ }
+ await Promise.all([
+ writeFile(SW_FILE, nextSw, "utf8"),
+ writeFile(HTML_FILE, nextHtml, "utf8"),
+ ]);
+ console.log(`[version] ตั้งเป็น ${target} แล้ว (sw.js + index.html)`);
+}
+
+const args = process.argv.slice(2);
+if (args.includes("--check")) {
+ await check();
+} else if (args.includes("--set")) {
+ const value = args[args.indexOf("--set") + 1];
+ if (!value) {
+ console.error("[version] ต้องระบุเวอร์ชันหลัง --set");
+ process.exit(1);
+ }
+ await setVersion(value);
+} else {
+ const { swVersion } = await readPair();
+ await setVersion(nextVersion(swVersion));
+}
diff --git a/sw.js b/sw.js
index ee76b37..dcc5c20 100644
--- a/sw.js
+++ b/sw.js
@@ -2,7 +2,7 @@
กติกา: bump VERSION เฉพาะเมื่อแก้ไฟล์นี้หรือรายการ PRECACHE
เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first จึงไม่ต้อง bump */
-const VERSION = "2026.11.19.4";
+const VERSION = "2026.11.24.2";
const CACHE = `hobbypj-${VERSION}`;
const PRECACHE = [
@@ -26,6 +26,7 @@ const PRECACHE = [
"./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",
];
diff --git a/ui/pwa.js b/ui/pwa.js
index c96b681..7290d25 100644
--- a/ui/pwa.js
+++ b/ui/pwa.js
@@ -1,7 +1,36 @@
-/* pwa.js — ลงทะเบียน service worker + เช็คอัปเดตอัตโนมัติ (ผู้ใช้ไม่ต้องล้างแคช) */
+/* pwa.js — ลงทะเบียน service worker + เช็ค/รับอัปเดตอัตโนมัติ (ผู้ใช้ไม่ต้องล้างแคช)
+ *
+ * ห่วงโซ่อัปเดตที่ต้องครบ (ขาดข้อใดข้อหนึ่ง = ค้างเวอร์ชันเก่า):
+ * 1. register ด้วย updateViaCache:"none" → เบราว์เซอร์ไม่แคชตัว sw.js เอง
+ * 2. reg.update() ตอนเปิดหน้า + ทุก 30 นาที + ตอนกลับมาโฟกัสแท็บ + ตอนต่อเน็ตใหม่
+ * 3. sw.js เรียก skipWaiting() ตอน install
+ * 4. sw.js เรียก clients.claim() ตอน activate (เข้าคุมแท็บที่เปิดอยู่ทันที)
+ * 5. controllerchange → โหลดหน้าใหม่เอง (เลื่อนออกไปถ้าผู้ใช้กำลังพิมพ์ในฟอร์ม)
+ */
const SW_URL = "sw.js";
+const UPDATE_INTERVAL_MS = 30 * 60 * 1000;
+
let reloading = false;
+let pendingReload = false;
+
+/** กำลังพิมพ์/แก้ค่าอยู่ไหม — ถ้าใช่ เลื่อน reload ออกไปก่อน จะได้ไม่หายข้อความ */
+function isEditing() {
+ const el = document.activeElement;
+ if (!el || el.disabled) return false;
+ const tag = el.tagName;
+ return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || el.isContentEditable;
+}
+
+function applyUpdate() {
+ if (reloading) return;
+ if (isEditing()) {
+ pendingReload = true;
+ return;
+ }
+ reloading = true;
+ location.reload();
+}
export async function registerServiceWorker() {
if (!("serviceWorker" in navigator)) return null;
@@ -9,6 +38,18 @@ export async function registerServiceWorker() {
// PWA ต้องใช้ HTTPS หรือ localhost เท่านั้น
return null;
}
+
+ // อัปเดตที่เลื่อนไว้ (ตอนผู้ใช้กำลังพิมพ์) → ทำต่อเมื่อเลิกพิมพ์หรือสลับแท็บ
+ document.addEventListener("focusout", () => {
+ if (!pendingReload) return;
+ setTimeout(() => {
+ if (!isEditing()) applyUpdate();
+ }, 400);
+ });
+ document.addEventListener("visibilitychange", () => {
+ if (pendingReload && document.visibilityState === "hidden") applyUpdate();
+ });
+
try {
const registration = await navigator.serviceWorker.register(SW_URL, {
scope: "./",
@@ -17,17 +58,19 @@ export async function registerServiceWorker() {
// เช็คอัปเดตทันทีที่เปิดหน้า และซ้ำทุก 30 นาที (เบราว์เซอร์เช็คเองทุก 24 ชม. ซึ่งช้าเกินไป)
registration.update().catch(() => {});
- setInterval(() => registration.update().catch(() => {}), 30 * 60 * 1000);
-
- navigator.serviceWorker.addEventListener("controllerchange", () => {
- if (reloading) return;
- reloading = true;
- location.reload();
- });
+ setInterval(() => registration.update().catch(() => {}), UPDATE_INTERVAL_MS);
+ // กลับมาโฟกัสแท็บ (แท็บที่เปิดค้างข้ามวัน) และตอนต่อเน็ตใหม่
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") registration.update().catch(() => {});
});
+ window.addEventListener("online", () => registration.update().catch(() => {}));
+
+ // service worker ใหม่เข้าคุมแล้ว → โหลดหน้าใหม่เอง (กันลูปด้วย flag ใน applyUpdate)
+ navigator.serviceWorker.addEventListener("controllerchange", () => {
+ if (!navigator.serviceWorker.controller) return;
+ applyUpdate();
+ });
return registration;
} catch (error) {