From fcdab3ac805005d5e5fd1064430794fb71719490 Mon Sep 17 00:00:00 2001 From: Bkk1234 Date: Tue, 3 Nov 2026 00:32:41 +0700 Subject: [PATCH] pwa: automatic update UX - no more hard refresh - ui/version.json: server-side version marker, polled by pwa.js (load, 5 min, tab focus, network online); mismatch -> toast + auto reload once per version - pwa.js: silent reload when tab hidden, guarded reload (sessionStorage) so a partial deploy cannot loop, ATUpdate.check() exposed for testing - _shared.css: .mu-update-toast pill (bottom center, reduced-motion aware) - shell js/css get ?v= automatically (bump-version.py) so HTTP cache cannot serve stale files before the service worker takes control - sw: cache fallback now ignores query string (precache still matches ?v= URLs) - bump-version.py: writes version.json + verifies it in --check - VERSION 2026.09.27.108 --- resource/tools/bump-version.py | 30 +++++- ui/_shared.css | 43 +++++++++ ui/index.html | 42 ++++---- ui/locales/en.json | 3 +- ui/locales/th.json | 3 +- ui/pwa.js | 169 +++++++++++++++++++++++++++++---- ui/sw.js | 7 +- ui/version.json | 1 + 8 files changed, 248 insertions(+), 50 deletions(-) create mode 100644 ui/version.json diff --git a/resource/tools/bump-version.py b/resource/tools/bump-version.py index 6e424d9..e73c68e 100644 --- a/resource/tools/bump-version.py +++ b/resource/tools/bump-version.py @@ -16,6 +16,7 @@ หลังอัปรูป: รันคำสั่งนี้ แล้วให้ผู้ใช้กด Ctrl+Shift+R ครั้งเดียว = เห็นรูปใหม่ทั้งหมด """ import argparse +import json import re import sys from pathlib import Path @@ -24,9 +25,13 @@ ROOT = Path(__file__).resolve().parents[2] UI = ROOT / "ui" SW = UI / "sw.js" INDEX = UI / "index.html" +VERSION_JSON = UI / "version.json" VER_RE = re.compile(r'const VERSION = "([^"]+)";') +# src="./x.js" | href="./x.css" (ไม่แตะ URL ภายนอก/ไฟล์อื่น) +SHELL_ASSET_RE = re.compile(r'((?:src|href)=")(\./[^"?]+\.(?:js|css))(?:\?v=[^"]*)?(")') + def read_version(): m = VER_RE.search(SW.read_text(encoding="utf-8")) @@ -60,15 +65,25 @@ def apply(ver, dry=False): SW.write_text(sw2, encoding="utf-8") changed.append("ui/sw.js") - # 2) index.html — meta + โลโก้ static (?v=) + # 2) index.html — meta + โลโก้ static (?v=) + shell asset (?v=) idx = INDEX.read_text(encoding="utf-8") idx2 = re.sub(r'()', rf"\g<1>{ver}\g<2>", idx) idx2 = re.sub(r'(\.image/icons/[^"?]+\.(?:png|jpg|jpeg|webp|svg))\?v=[^"&]*', rf"\g<1>?v={ver}", idx2) + # JS/CSS ของ shell (./xxx.js, ./xxx.css) → ?v= กัน HTTP cache คืนของเก่าก่อน SW ทำงาน + idx2 = SHELL_ASSET_RE.sub(rf"\g<1>\g<2>?v={ver}\g<3>", idx2) if idx2 != idx: if not dry: INDEX.write_text(idx2, encoding="utf-8") changed.append("ui/index.html") + # 3) version.json — ให้ pwa.js ตรวจเวอร์ชันใหม่ได้เอง (ทุกเครื่อง) + want = '{"version": "%s"}\n' % ver + old = VERSION_JSON.read_text(encoding="utf-8") if VERSION_JSON.exists() else "" + if old != want: + if not dry: + VERSION_JSON.write_text(want, encoding="utf-8") + changed.append("ui/version.json") + return changed @@ -99,16 +114,25 @@ def main(): m = re.search(r' หน้าแรก | Atroute (mockup) - + - - - - - + + + + +