From 0cf391afba438fa2aa352e932617798de5c55448 Mon Sep 17 00:00:00 2001 From: Bkk1234 Date: Sat, 24 Oct 2026 00:27:23 +0700 Subject: [PATCH] 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: 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 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. --- frontend/next.config.mjs | 20 +++++++++++++++ frontend/public/sw.js | 3 ++- frontend/public/version.json | 6 +++++ frontend/src/app/layout.tsx | 16 +++++++++++- frontend/src/components/PwaBoot.tsx | 35 ++++++++++++++++++++++++++ frontend/src/components/SiteFooter.tsx | 8 +++++- frontend/src/styles/_shared.css | 10 ++++++++ frontend/tsconfig.tsbuildinfo | 2 +- 8 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 frontend/public/version.json diff --git a/frontend/next.config.mjs b/frontend/next.config.mjs index a17bdc3..5806e46 100644 --- a/frontend/next.config.mjs +++ b/frontend/next.config.mjs @@ -8,6 +8,26 @@ const nextConfig = { images: { remotePatterns: [{ protocol: "http", hostname: "**" }, { protocol: "https", hostname: "**" }] }, + async headers() { + return [ + { + /* sw.js และ version.json ต้องไม่ถูก HTTP cache — ไม่งั้นผู้ใช้ค้างรุ่นเก่า */ + source: "/sw.js", + headers: [ + { key: "Cache-Control", value: "no-cache, no-store, must-revalidate" }, + { key: "Service-Worker-Allowed", value: "/" }, + ], + }, + { + source: "/version.json", + headers: [{ key: "Cache-Control", value: "no-cache, no-store, must-revalidate" }], + }, + { + source: "/ui-manifest.json", + headers: [{ key: "Cache-Control", value: "public, max-age=0, must-revalidate" }], + }, + ]; + }, async rewrites() { return [ { diff --git a/frontend/public/sw.js b/frontend/public/sw.js index 8c5022c..3ae6201 100644 --- a/frontend/public/sw.js +++ b/frontend/public/sw.js @@ -15,9 +15,10 @@ - SW ใหม่ → skipWaiting + clients.claim + แจ้งหน้าเว็บ ============================================================ */ -const VERSION = "1.0.1"; /* bump เมื่อแก้ UI เพื่อบังคับให้ cache เก่าถูกทิ้ง */ +const VERSION = "2026.10.24.1"; /* bump เมื่อแก้ UI เพื่อบังคับให้ cache เก่าถูกทิ้ง */ const CACHE = `atroute-${VERSION}`; const OFFLINE_URL = "/offline.html"; +const VERSION_URL = "/version.json"; /* ไฟล์ที่ precache ไว้ใช้ตอนออฟไลน์ (เฉพาะที่ path คงที่) */ const PRECACHE = [ diff --git a/frontend/public/version.json b/frontend/public/version.json new file mode 100644 index 0000000..449f5c1 --- /dev/null +++ b/frontend/public/version.json @@ -0,0 +1,6 @@ +{ + "app": "Atroute", + "version": "2026.10.24.1", + "builtAt": "2026-10-24T00:25:14", + "pwa": true +} diff --git a/frontend/src/app/layout.tsx b/frontend/src/app/layout.tsx index ff04dc4..b1118c6 100644 --- a/frontend/src/app/layout.tsx +++ b/frontend/src/app/layout.tsx @@ -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 + ใช้ฝังใน เพื่อให้ 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 ( - + {/* ฟอนต์ K2D ฝังในโปรเจกต์ + Leaflet CSS (ไฟล์ static ใน public) */} diff --git a/frontend/src/components/PwaBoot.tsx b/frontend/src/components/PwaBoot.tsx index 98aa679..10286e7 100644 --- a/frontend/src/components/PwaBoot.tsx +++ b/frontend/src/components/PwaBoot.tsx @@ -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) ---------- */ diff --git a/frontend/src/components/SiteFooter.tsx b/frontend/src/components/SiteFooter.tsx index 9afc620..3fc4142 100644 --- a/frontend/src/components/SiteFooter.tsx +++ b/frontend/src/components/SiteFooter.tsx @@ -115,7 +115,13 @@ export default function SiteFooter() {
-

© 2026 Atroute

+

+ © 2026 Atroute{" "} + {/* รุ่นแอป — เห็นด้วยตาได้ว่าเบราว์เซอร์โหลดของใหม่แล้วหรือยัง */} + + v— + +