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
|
||||
|
||||
|
||||
|
||||
@@ -494,6 +494,49 @@ body:has(.mu-site-footer) .mu-footer { display: none; }
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
/* =========================================================
|
||||
Toast แจ้งอัปเดตเวอร์ชันใหม่ (inject จาก ui/pwa.js)
|
||||
========================================================= */
|
||||
.mu-update-toast {
|
||||
position: fixed;
|
||||
left: 50%;
|
||||
bottom: calc(20px + env(safe-area-inset-bottom, 0px));
|
||||
z-index: 1300;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
max-width: calc(100vw - 32px);
|
||||
padding: 10px 16px;
|
||||
border-radius: 999px;
|
||||
background: #1f2a44;
|
||||
color: #fff;
|
||||
font-size: .82rem;
|
||||
font-weight: 600;
|
||||
line-height: 1.35;
|
||||
box-shadow: 0 10px 24px rgba(15, 23, 42, .22);
|
||||
transform: translate(-50%, 12px);
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
transition: opacity .25s ease, transform .25s ease;
|
||||
}
|
||||
.mu-update-toast.is-in { opacity: 1; transform: translate(-50%, 0); }
|
||||
.mu-update-dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
background: #b694f6;
|
||||
flex: none;
|
||||
animation: muUpdatePulse 1.1s ease-in-out infinite;
|
||||
}
|
||||
@keyframes muUpdatePulse {
|
||||
0%, 100% { opacity: .35; transform: scale(.85); }
|
||||
50% { opacity: 1; transform: scale(1); }
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.mu-update-toast { transition: none; }
|
||||
.mu-update-dot { animation: none; }
|
||||
}
|
||||
|
||||
/* =========================================================
|
||||
Motion — อนิเมชันร่วมทุกหน้า (ไฟล์ JS: ui/motion.js)
|
||||
---------------------------------------------------------
|
||||
|
||||
+21
-21
@@ -5,15 +5,15 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<title>หน้าแรก | Atroute (mockup)</title>
|
||||
<!-- ฟอนต์หลัก K2D: ไฟล์ในโปรเจกต์ (ui/vendor/fonts/) — ทำงานออฟไลน์ได้ -->
|
||||
<link href="./vendor/fonts/k2d.css" rel="stylesheet">
|
||||
<link href="./vendor/fonts/k2d.css?v=2026.09.27.108" rel="stylesheet">
|
||||
<!-- ตัวสำรอง (โหลดจาก CDN เมื่อออนไลน์เท่านั้น · ออฟไลน์ถอยไปใช้ system-ui) -->
|
||||
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@400;500;600;700;800&family=Prompt:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||
<link href="../../frontend-web-transit/web-transit/css/main-theme.css" rel="stylesheet">
|
||||
<link href="./_shared.css" rel="stylesheet">
|
||||
<link href="./vendor/leaflet/leaflet.css" rel="stylesheet">
|
||||
<link href="./map.css" rel="stylesheet">
|
||||
<meta name="app-version" content="2026.09.27.107">
|
||||
<link rel="icon" type="image/png" href="../.image/icons/minibus-512x512.png?v=2026.09.27.107">
|
||||
<link href="./_shared.css?v=2026.09.27.108" rel="stylesheet">
|
||||
<link href="./vendor/leaflet/leaflet.css?v=2026.09.27.108" rel="stylesheet">
|
||||
<link href="./map.css?v=2026.09.27.108" rel="stylesheet">
|
||||
<meta name="app-version" content="2026.09.27.108">
|
||||
<link rel="icon" type="image/png" href="../.image/icons/minibus-512x512.png?v=2026.09.27.108">
|
||||
<style>
|
||||
.mu-card.empty { width: 100%; padding: 16px; text-align: center; color: #6f7b95; background: #fff7fb; border: 1px dashed #f4c7df; }
|
||||
a.mu-card { text-decoration: none; color: inherit; display: block; }
|
||||
@@ -305,19 +305,19 @@
|
||||
</script>
|
||||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="default">
|
||||
<link rel="apple-touch-icon" href="../.image/icons/minibus-192x192.png?v=2026.09.27.107">
|
||||
<script src="./imageurl.js" defer></script>
|
||||
<script src="./pwa.js" defer></script>
|
||||
<script src="./i18n.js" defer></script>
|
||||
<script src="./analytics.js" defer></script>
|
||||
<script src="./popular.js" defer></script>
|
||||
<script src="./vendor/leaflet/leaflet.js" defer></script>
|
||||
<script src="./map.js" defer></script>
|
||||
<script src="./partials/schedules.js" defer></script>
|
||||
<script src="./partials/otherStop.js" defer></script>
|
||||
<script src="./motion.js" defer></script>
|
||||
<script src="./router.js" defer></script>
|
||||
<link rel="stylesheet" href="./responsive.css">
|
||||
<link rel="apple-touch-icon" href="../.image/icons/minibus-192x192.png?v=2026.09.27.108">
|
||||
<script src="./imageurl.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./pwa.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./i18n.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./analytics.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./popular.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./vendor/leaflet/leaflet.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./map.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./partials/schedules.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./partials/otherStop.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./motion.js?v=2026.09.27.108" defer></script>
|
||||
<script src="./router.js?v=2026.09.27.108" defer></script>
|
||||
<link rel="stylesheet" href="./responsive.css?v=2026.09.27.108">
|
||||
</head>
|
||||
<body class="main-theme-page">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" style="display:none" aria-hidden="true">
|
||||
@@ -411,7 +411,7 @@
|
||||
<header class="mu-topbar">
|
||||
<div class="mu-container mu-topbar-inner">
|
||||
<a class="mu-brand" href="#/" aria-label="Atroute — หน้าแรก" data-i18n-attr="aria-label:aria.brandHome">
|
||||
<img class="mu-logo-img" src="../.image/icons/minibus-512x512.png?v=2026.09.27.107" alt="Atroute logo" width="44" height="44" decoding="async">
|
||||
<img class="mu-logo-img" src="../.image/icons/minibus-512x512.png?v=2026.09.27.108" alt="Atroute logo" width="44" height="44" decoding="async">
|
||||
<span>
|
||||
<span class="mu-brand-title">Atroute</span>
|
||||
<span class="mu-brand-subtitle" data-i18n="brand.subtitle">สถานีขนส่งผู้โดยสารจังหวัดอ่างทอง</span>
|
||||
@@ -454,7 +454,7 @@
|
||||
<div class="mu-footer-grid">
|
||||
<div class="mu-footer-brand">
|
||||
<div class="mu-footer-brand-row">
|
||||
<img src="../.image/icons/minibus-512x512.png?v=2026.09.27.107" alt="Atroute">
|
||||
<img src="../.image/icons/minibus-512x512.png?v=2026.09.27.108" alt="Atroute">
|
||||
<div>
|
||||
<strong>Atroute</strong>
|
||||
<span data-i18n="brand.subtitle">สถานีขนส่งผู้โดยสารจังหวัดอ่างทอง</span>
|
||||
|
||||
+2
-1
@@ -383,5 +383,6 @@
|
||||
"credits.analyticsTitle": "Analytics — inspired by Umami",
|
||||
"credits.analyticsBody": "The analytics layer is written here in",
|
||||
"credits.analyticsBody2": "(no dependencies) — it borrows only the privacy-first approach and the /api/send payload shape from Umami; no Umami code is copied",
|
||||
"credits.analyticsLocal": "Stats live in the user's browser only (localStorage) — view or clear them on the Stats page"
|
||||
"credits.analyticsLocal": "Stats live in the user's browser only (localStorage) — view or clear them on the Stats page",
|
||||
"update.applying": "New version — updating…"
|
||||
}
|
||||
+2
-1
@@ -383,5 +383,6 @@
|
||||
"credits.analyticsTitle": "สถิติ — แนวคิดจาก Umami",
|
||||
"credits.analyticsBody": "ระบบสถิติของเว็บนี้เขียนเองใน",
|
||||
"credits.analyticsBody2": "(ไม่มี dependency) — ยืมเฉพาะแนวคิด privacy-first และรูปแบบ payload ของ Umami (/api/send) ไม่ได้คัดลอกโค้ดของ Umami มาใช้",
|
||||
"credits.analyticsLocal": "ข้อมูลสถิติเก็บในเบราว์เซอร์ของผู้ใช้เท่านั้น (localStorage) — ดู/ล้างได้ที่หน้า “สถิติ”"
|
||||
"credits.analyticsLocal": "ข้อมูลสถิติเก็บในเบราว์เซอร์ของผู้ใช้เท่านั้น (localStorage) — ดู/ล้างได้ที่หน้า “สถิติ”",
|
||||
"update.applying": "มีเวอร์ชันใหม่ — กำลังอัปเดต…"
|
||||
}
|
||||
@@ -1,39 +1,166 @@
|
||||
/* ============================================================
|
||||
Atroute — PWA registration + auto-update
|
||||
ผู้ใช้ไม่ต้องล้างแคชเอง:
|
||||
1. register แบบ updateViaCache:"none" → ไม่เชื่อ HTTP cache ของ sw.js
|
||||
2. เรียก reg.update() ทุกครั้งที่โหลดหน้า → ตรวจ SW ใหม่ทันที
|
||||
(ปกติ browser เช็คแค่ทุก 24 ชม.)
|
||||
3. ตรวจซ้ำทุก 30 นาที → เหมาะกับจอแสดงข้อมูลที่เปิดทิ้งไว้
|
||||
4. SW ใหม่ skipWaiting+claim → controllerchange → reload เงียบ ๆ
|
||||
Atroute — PWA registration + auto-update (ผู้ใช้ไม่ต้องกดอะไรเลย)
|
||||
------------------------------------------------------------
|
||||
ชั้นกันแคชที่ทำงานร่วมกัน — "ไม่มีใครต้องกด Ctrl+Shift+R"
|
||||
|
||||
1. shell assets (js/css) มี ?v=<version> → HTTP cache เห็นเป็นไฟล์ใหม่ทันที
|
||||
(bump-version.py เติมให้อัตโนมัติ)
|
||||
2. SW network-first + cache:"no-store" → ออนไลน์ได้ของใหม่เสมอ (ui/sw.js)
|
||||
3. reg.update() ทุกครั้งที่เปิดหน้า + ทุก 30 นาที
|
||||
4. version.json ← ตัวชี้ขาด: เวอร์ชันบนเซิร์ฟเวอร์ ≠ meta ในหน้า
|
||||
→ ขึ้น toast แล้วรีโหลดเอง (รีโหลดครั้งเดียวต่อเวอร์ชัน กันลูป)
|
||||
ตรวจตอนเปิดหน้า · ทุก 5 นาที · ตอนกลับมาที่แท็บ · ตอนเน็ตกลับมา
|
||||
|
||||
หมายเหตุ: ไม่มีปุ่ม "ติดตั้งแอป" ลอยมุมขวาล่าง (เอาออกตามคำขอ)
|
||||
============================================================ */
|
||||
(function () {
|
||||
if (!("serviceWorker" in navigator)) return;
|
||||
var CHECK_INTERVAL_MS = 5 * 60 * 1000; // ตรวจ version.json ทุก 5 นาที
|
||||
var SW_INTERVAL_MS = 30 * 60 * 1000; // ตรวจ sw.js ทุก 30 นาที
|
||||
var RELOAD_GUARD = 'at_reloaded_for'; // sessionStorage — กันรีโหลดวน
|
||||
|
||||
var CHECK_INTERVAL_MS = 30 * 60 * 1000; // ตรวจอัปเดตทุก 30 นาที
|
||||
var metaEl = document.querySelector('meta[name="app-version"]');
|
||||
var currentVersion = metaEl ? metaEl.content : '';
|
||||
|
||||
/* หมายเหตุ: ไม่มีปุ่ม "ติดตั้งแอป" ลอยมุมขวาล่างแล้ว
|
||||
(ตั้งใจเอาออก — Chrome จะโชว์ไอคอนติดตั้งของเบราว์เซอร์เองตามปกติ) */
|
||||
function t(key, fallback) {
|
||||
var v = (window.I18N && I18N.t) ? I18N.t(key) : key;
|
||||
return (!v || v === key) ? fallback : v;
|
||||
}
|
||||
|
||||
window.addEventListener("load", function () {
|
||||
/* ---------- toast เล็ก ๆ ล่างจอ (ไม่บล็อกการใช้งาน) ---------- */
|
||||
var toastEl = null, toastTimer = null;
|
||||
function showToast(msg) {
|
||||
if (document.querySelector('.mu-update-toast')) return;
|
||||
toastEl = document.createElement('div');
|
||||
toastEl.className = 'mu-update-toast';
|
||||
toastEl.setAttribute('role', 'status');
|
||||
toastEl.setAttribute('aria-live', 'polite');
|
||||
|
||||
var dot = document.createElement('span');
|
||||
dot.className = 'mu-update-dot';
|
||||
dot.setAttribute('aria-hidden', 'true');
|
||||
|
||||
var text = document.createElement('span');
|
||||
text.textContent = msg;
|
||||
|
||||
toastEl.appendChild(dot);
|
||||
toastEl.appendChild(text);
|
||||
document.body.appendChild(toastEl);
|
||||
toastTimer = setTimeout(function () { if (toastEl) toastEl.classList.add('is-in'); }, 20);
|
||||
}
|
||||
|
||||
function hideToast() {
|
||||
clearTimeout(toastTimer);
|
||||
if (!toastEl) return;
|
||||
toastEl.classList.remove('is-in');
|
||||
var el = toastEl;
|
||||
toastEl = null;
|
||||
setTimeout(function () { if (el) el.remove(); }, 300);
|
||||
}
|
||||
|
||||
/* ---------- รีโหลดครั้งเดียวต่อเวอร์ชัน (กันลูป) ---------- */
|
||||
function alreadyReloaded(ver) {
|
||||
try { return sessionStorage.getItem(RELOAD_GUARD) === ver; } catch (e) { return false; }
|
||||
}
|
||||
|
||||
function reloadOnce(ver) {
|
||||
try {
|
||||
if (alreadyReloaded(ver)) return;
|
||||
sessionStorage.setItem(RELOAD_GUARD, ver);
|
||||
} catch (e) { /* โหมดส่วนตัว: ยอมให้รีโหลดได้ */ }
|
||||
location.reload();
|
||||
}
|
||||
|
||||
/* ---------- พบเวอร์ชันใหม่ → แจ้ง + อัปเดตเอง ---------- */
|
||||
var applying = false;
|
||||
function applyUpdate(nextVersion) {
|
||||
if (applying) return;
|
||||
/* รีโหลดเพื่อเวอร์ชันนี้ไปแล้วแต่ยังไม่ตรง (เช่นเซิร์ฟเวอร์ยังไม่ลงไฟล์ครบ)
|
||||
→ หยุดเงียบ ๆ ไม่เด้ง toast ซ้ำให้รำคาญ */
|
||||
if (alreadyReloaded(nextVersion)) return;
|
||||
applying = true;
|
||||
showToast(t('update.applying', 'มีเวอร์ชันใหม่ — กำลังอัปเดต…'));
|
||||
|
||||
/* แท็บถูกซ่อน (ผู้ใช้ไม่ได้ดูอยู่) → รีโหลดเลย ไม่รบกวน */
|
||||
if (document.hidden) { reloadOnce(nextVersion); return; }
|
||||
|
||||
var done = false;
|
||||
function finish() {
|
||||
if (done) return;
|
||||
done = true;
|
||||
reloadOnce(nextVersion);
|
||||
}
|
||||
|
||||
/* ขอ SW ใหม่ก่อน — ถ้าได้ controllerchange จะรีโหลดจากตรงนั้น */
|
||||
if (navigator.serviceWorker) {
|
||||
navigator.serviceWorker.addEventListener('controllerchange', finish, { once: true });
|
||||
navigator.serviceWorker.getRegistration()
|
||||
.then(function (reg) { if (reg) return reg.update(); })
|
||||
.catch(function () {});
|
||||
}
|
||||
/* ไม่ว่า SW จะทันหรือไม่ ภายใน 2.5 วิ รีโหลดเพื่อรับ HTML/JS/CSS ใหม่ */
|
||||
setTimeout(finish, 2500);
|
||||
}
|
||||
|
||||
/* ---------- ตรวจ version.json (ตัวชี้ขาดว่ามีของใหม่ไหม) ---------- */
|
||||
function checkVersion() {
|
||||
if (!currentVersion) return Promise.resolve(null);
|
||||
return fetch('./version.json?t=' + Date.now(), { cache: 'no-store' })
|
||||
.then(function (r) { return r.ok ? r.json() : null; })
|
||||
.then(function (d) {
|
||||
if (!d || !d.version) return null;
|
||||
if (String(d.version) !== String(currentVersion)) {
|
||||
applyUpdate(String(d.version));
|
||||
return d.version;
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.catch(function () { return null; }); /* ออฟไลน์/ยังไม่มีไฟล์ = ไม่ทำอะไร */
|
||||
}
|
||||
|
||||
if (!('serviceWorker' in navigator)) {
|
||||
/* ไม่มี SW (เบราว์เซอร์เก่า): ยังตรวจ version.json ได้ */
|
||||
checkVersion();
|
||||
setInterval(checkVersion, CHECK_INTERVAL_MS);
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('load', function () {
|
||||
navigator.serviceWorker
|
||||
.register("./sw.js", { updateViaCache: "none" })
|
||||
.register('./sw.js', { updateViaCache: 'none' })
|
||||
.then(function (reg) {
|
||||
// ตรวจหา SW เวอร์ชันใหม่ทุกครั้งที่โหลดหน้า
|
||||
reg.update();
|
||||
|
||||
// ตรวจซ้ำเป็นระยะ (กันจอที่เปิดค้างไม่เคยโหลดหน้าใหม่)
|
||||
setInterval(function () {
|
||||
reg.update();
|
||||
}, CHECK_INTERVAL_MS);
|
||||
setInterval(function () { reg.update(); }, SW_INTERVAL_MS);
|
||||
|
||||
// SW ใหม่接管การควบคุมแล้ว → รีโหลดหน้ารับเวอร์ชันใหม่ทันที
|
||||
navigator.serviceWorker.addEventListener("controllerchange", function () {
|
||||
window.location.reload();
|
||||
/* SW ใหม่接管แล้ว → รับเวอร์ชันใหม่ (รีโหลดครั้งเดียวต่อเวอร์ชัน) */
|
||||
navigator.serviceWorker.addEventListener('controllerchange', function () {
|
||||
reloadOnce(currentVersion || 'sw');
|
||||
});
|
||||
})
|
||||
.catch(function () {
|
||||
/* ไม่มี SW = ไม่มีโหมดออฟไลน์ — เว็บยังใช้งานปกติ */
|
||||
});
|
||||
|
||||
checkVersion();
|
||||
setInterval(checkVersion, CHECK_INTERVAL_MS);
|
||||
|
||||
/* กลับมาที่แท็บ/สลับแอปกลับมา (มือถือ) → ตรวจทันที */
|
||||
document.addEventListener('visibilitychange', function () {
|
||||
if (document.visibilityState === 'visible') {
|
||||
hideToast();
|
||||
checkVersion();
|
||||
}
|
||||
});
|
||||
|
||||
/* เน็ตกลับมา → ตรวจอีกรอบ */
|
||||
window.addEventListener('online', function () { checkVersion(); });
|
||||
});
|
||||
|
||||
/* เปิดให้ทดสอบ/สั่งเองได้ */
|
||||
window.ATUpdate = {
|
||||
check: checkVersion,
|
||||
version: currentVersion,
|
||||
apply: applyUpdate,
|
||||
_toast: showToast
|
||||
};
|
||||
})();
|
||||
@@ -13,7 +13,7 @@
|
||||
- ขั้นตอนอัปเดต: ดู AGENTS.md หัวข้อ PWA
|
||||
============================================================ */
|
||||
|
||||
const VERSION = "2026.09.27.107"; // ← bump ทุกครั้งที่แก้ SW เอง
|
||||
const VERSION = "2026.09.27.108"; // ← bump ทุกครั้งที่แก้ SW เอง
|
||||
const CACHE = `at-transit-${VERSION}`;
|
||||
|
||||
const PRECACHE = [
|
||||
@@ -166,7 +166,8 @@ async function networkFirst(request) {
|
||||
if (res.ok) cache.put(request, res.clone());
|
||||
return res;
|
||||
} catch {
|
||||
const hit = await cache.match(request);
|
||||
/* ?v=<version> ทำให้ URL ต่างจากตอน precache → จับคู่แบบไม่สน query */
|
||||
const hit = (await cache.match(request)) || (await cache.match(request, { ignoreSearch: true }));
|
||||
return hit || Response.error();
|
||||
}
|
||||
}
|
||||
@@ -187,7 +188,7 @@ async function networkFirstDocument(request) {
|
||||
}
|
||||
|
||||
async function cacheFirst(request) {
|
||||
const hit = await caches.match(request);
|
||||
const hit = (await caches.match(request)) || (await caches.match(request, { ignoreSearch: true }));
|
||||
if (hit) return hit;
|
||||
const cache = await caches.open(CACHE);
|
||||
try {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"version": "2026.09.27.108"}
|
||||
Reference in new issue
Block a user