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=<version> 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
This commit is contained in:
1 parent
449afb923a
commit
fcdab3ac80
8 files changed
+248
-50
No files matched your search
@@ -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'(<meta name="app-version" content=")[^"]*(">)', 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'<meta name="app-version" content="([^"]*)"', idx)
|
||||
meta = m.group(1) if m else None
|
||||
logos = set(re.findall(r"\.image/icons/[^\"]+?\?v=([^\"&]+)", idx))
|
||||
ok = (meta == cur) and (logos == {cur} or not logos)
|
||||
shell = set(re.findall(r'"\./[^"?]+\.(?:js|css)\?v=([^"]*)"', idx))
|
||||
vjson = None
|
||||
if VERSION_JSON.exists():
|
||||
try:
|
||||
vjson = json.loads(VERSION_JSON.read_text(encoding="utf-8")).get("version")
|
||||
except Exception:
|
||||
vjson = "parse-error"
|
||||
ok = (meta == cur) and (logos == {cur} or not logos) and (shell == {cur} or not shell) and vjson == cur
|
||||
print(f"sw.js VERSION = {cur}")
|
||||
print(f"meta app-version= {meta} {'✓' if meta == cur else '✗ ไม่ตรง'}")
|
||||
print(f"โลโก้ ?v= = {sorted(logos) or '-'} {'✓' if (logos == {cur} or not logos) else '✗ ไม่ตรง'}")
|
||||
print(f"shell js/css ?v== {sorted(shell) or '-'} {'✓' if (shell == {cur} or not shell) else '✗ ไม่ตรง'}")
|
||||
print(f"version.json = {vjson} {'✓' if vjson == cur else '✗ ไม่ตรง'}")
|
||||
return 0 if ok else 1
|
||||
|
||||
ver = cur if args.sync else next_version(cur, args.base)
|
||||
changed = apply(ver, dry=False)
|
||||
report(ver, changed)
|
||||
print("\nขั้นตอนถัดไป: ให้ผู้ใช้กด Ctrl+Shift+R ครั้งเดียว (รูปทั้งหมดจะโหลดใหม่)")
|
||||
print("\nผู้ใช้ไม่ต้องกด Ctrl+Shift+R — pwa.js ตรวจ version.json เองแล้วอัปเดตให้")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user