feat(pwa): harden the auto-update chain and add a version tool
- ui/pwa.js: register with updateViaCache:'none', check for updates on load, every 30 min, on tab focus and on 'online'; controllerchange reloads the page itself but waits while the user is typing in a form (admin edits survive), retrying on blur/hide - sw.js: precache the maskable icon; bump VERSION -> 2026.11.24.2 - scripts/bump-version.mjs: bump sw.js VERSION + <meta app-version> together, --check fails on drift; wired into > [email protected] 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] ตรงกันที่ 2026.11.24.2, exposed as > [email protected] version > node scripts/bump-version.mjs [version] ตั้งเป็น 2026.11.24.3 แล้ว (sw.js + index.html) - README/AGENTS: document the PWA behaviour and the bump workflow Verified in Chrome: SPA route changes keep timeOrigin and navigation count at 1 (no reload), a simulated deploy (version bump -> reg.update) replaced the cache from hobbypj-2026.11.24.1 to .2 with the old cache deleted and the page reloading by itself, Page.getAppManifest errors: [], and with the network emulated offline the shell + 6 cards still render while an uncached file fails.
This commit is contained in:
1 parent
9258cdd09e
commit
634dc3ae3c
7 files changed
+173
-13
No files matched your search
@@ -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 === <meta app-version> (exit 1 ถ้าไม่ตรง)
|
||||
*
|
||||
* สองที่ที่ต้องตรงกัน: `VERSION` ใน sw.js (ชื่อ cache) และ `<meta name="app-version">` ใน 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 = /<meta name="app-version" content="([^"]*)" \/>/;
|
||||
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] หา <meta name="app-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, `<meta name="app-version" content="${target}" />`);
|
||||
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));
|
||||
}
|
||||
Reference in new issue
Block a user