PWA: publish an app version so stale shells are detectable
The service worker version had never moved past 1.0.0 and nothing on the page said which build was loaded, so an old cached shell looked identical to a fresh one. - public/version.json: app, version, builtAt, pwa - sw.js VERSION -> 2026.10.24.1 (new cache name, old caches dropped on activate) - next.config: sw.js and version.json are served no-cache, no-store, plus Service-Worker-Allowed: / ; ui-manifest.json must-revalidate - layout: <html data-app-version> read from version.json at build time - PwaBoot: paints the version into the footer badge, polls /version.json (1.5s then every 5 min) and reloads once when it differs; guards against reload loops - footer shows v2026.10.24.1 in mono next to the copyright Verified: sw.js, /version.json and <html data-app-version> all report 2026.10.24.1, the footer badge shows it, and /stops renders the three search tabs with the near-me panel in its own tab.
This commit is contained in:
1 parent
7652ddaae5
commit
0cf391afba
8 files changed
+96
-4
No files matched your search
@@ -1,6 +1,20 @@
|
||||
import type { Metadata, Viewport } from "next";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { SITE_URL, SITE_NAME, SITE_TITLE, SITE_DESCRIPTION, SITE_LOCALE, SITE_OG_IMAGE } from "@/lib/site";
|
||||
import { SiteJsonLd } from "@/components/JsonLd";
|
||||
|
||||
/* รุ่นแอป — อ่านจาก public/version.json ตอน build
|
||||
ใช้ฝังใน <html data-app-version> เพื่อให้ PwaBoot เทียบกับฝั่งเซิร์ฟเวอร์ได้ */
|
||||
function readAppVersion(): string {
|
||||
try {
|
||||
const raw = readFileSync(join(process.cwd(), "public", "version.json"), "utf8");
|
||||
return String(JSON.parse(raw).version || "");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
const APP_VERSION = readAppVersion();
|
||||
import IconSprite from "@/components/IconSprite";
|
||||
import PwaBoot from "@/components/PwaBoot";
|
||||
import SiteBoot from "@/components/SiteBoot";
|
||||
@@ -103,7 +117,7 @@ export const viewport: Viewport = {
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<html lang="th">
|
||||
<html lang="th" data-app-version={APP_VERSION}>
|
||||
<head>
|
||||
{/* ฟอนต์ K2D ฝังในโปรเจกต์ + Leaflet CSS (ไฟล์ static ใน public) */}
|
||||
<link rel="stylesheet" href="/vendor/fonts/k2d.css" />
|
||||
|
||||
@@ -56,9 +56,44 @@ export default function PwaBoot() {
|
||||
/* SW ใหม่เข้าควบคุม → โหลดใหม่เงียบ ๆ (ผู้ใช้ไม่ต้องล้างแคช) */
|
||||
navigator.serviceWorker.addEventListener("message", (e) => {
|
||||
if (e.data?.type === "SW_ACTIVATED") {
|
||||
// กัน reload วนซ้ำ: ทำได้ครั้งเดียวต่อการเปิดหน้า
|
||||
const w = window as unknown as { __swReloaded?: boolean };
|
||||
if (w.__swReloaded) return;
|
||||
w.__swReloaded = true;
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
|
||||
/* เทียบรุ่นที่โหลดอยู่ กับ /version.json บนเซิร์ฟเวอร์
|
||||
— ถ้าต่างกัน แปลว่า deploy รุ่นใหม่แล้ว แต่หน้านี้ยังเป็นของเก่า → โหลดใหม่ */
|
||||
/* แสดงรุ่นที่โหลดอยู่บนหน้าเว็บ (footer) */
|
||||
const paintVersion = () => {
|
||||
const v = document.documentElement.dataset.appVersion || "";
|
||||
if (!v) return;
|
||||
document.querySelectorAll("[data-app-ver]").forEach((el) => {
|
||||
el.textContent = "v" + v;
|
||||
});
|
||||
};
|
||||
paintVersion();
|
||||
|
||||
const checkVersion = () => {
|
||||
fetch("/version.json", { cache: "no-store" })
|
||||
.then((r) => (r.ok ? r.json() : null))
|
||||
.then((v) => {
|
||||
const remote = String(v?.version || "");
|
||||
const local = document.documentElement.dataset.appVersion || "";
|
||||
paintVersion();
|
||||
if (remote && local && remote !== local) {
|
||||
const w = window as unknown as { __verReloaded?: boolean };
|
||||
if (w.__verReloaded) return;
|
||||
w.__verReloaded = true;
|
||||
window.location.reload();
|
||||
}
|
||||
})
|
||||
.catch(() => undefined);
|
||||
};
|
||||
setTimeout(checkVersion, 1500);
|
||||
setInterval(checkVersion, 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
/* ---------- 2) install prompt (Android) ---------- */
|
||||
|
||||
@@ -115,7 +115,13 @@ export default function SiteFooter() {
|
||||
</div>
|
||||
|
||||
<div className="mu-footer-bottom">
|
||||
<p>© 2026 Atroute</p>
|
||||
<p>
|
||||
© 2026 Atroute{" "}
|
||||
{/* รุ่นแอป — เห็นด้วยตาได้ว่าเบราว์เซอร์โหลดของใหม่แล้วหรือยัง */}
|
||||
<span className="mu-app-ver" data-app-ver="">
|
||||
v—
|
||||
</span>
|
||||
</p>
|
||||
<span className="mu-visitor" title="จำนวนผู้เข้าชมเว็บไซต์">
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
|
||||
@@ -710,3 +710,13 @@ body:has(.mu-site-footer) .mu-footer { display: none; }
|
||||
.mu-pop-btn-lang { min-width: 60px; }
|
||||
.mu-pop-item { min-height: 44px; }
|
||||
}
|
||||
|
||||
/* รุ่นแอปที่แสดงท้ายเว็บ — ตัวเล็ก ๆ ไว้ตรวจว่าหน้านี้เป็น build ล่าสุดไหม */
|
||||
.mu-app-ver {
|
||||
margin-left: 6px;
|
||||
font-family: ui-monospace, "IBM Plex Mono", monospace;
|
||||
font-size: .72rem;
|
||||
font-variant-numeric: tabular-nums;
|
||||
color: var(--c-ink-2);
|
||||
opacity: .85;
|
||||
}
|
||||
Reference in new issue
Block a user