Add service worker and harden the web app manifest for Android 14+ / iOS 17+
Android (Samsung S23, One UI 6) refuses 'Install app' / standalone mode when the origin is not secure or the app has no service worker. The ported Next.js app shipped neither: only a manifest existed, and no sw.js at all. - public/sw.js: navigation network-first with an offline fallback, cache-first for static assets, /api and /files bypassed, versioned cache with skipWaiting + clients.claim. - public/offline.html: offline shell. - src/components/PwaBoot.tsx: registers the worker, wires beforeinstallprompt into an in-app install button, and shows a share-sheet hint on iOS/Samsung Internet where the event never fires. - ui-manifest.json: add id, display_override, prefer_related_applications, launch_handler, handle_links, a maskable icon for Android 14 and screenshots for the install preview. - layout.tsx: viewport-fit=cover, interactive-widget, maximumScale 5 (never user-scalable=no), appleWebApp metadata, format detection off, and drop manually duplicated meta tags that Next already emits. Verified: manifest passes 8/8 installability checks, sw.js and offline.html serve with correct content types, tsc clean, npm run build passes.
This commit is contained in:
1 parent
3519f92669
commit
092f551b7d
7 files changed
+405
-11
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import type { Metadata, Viewport } from "next";
|
||||
import IconSprite from "@/components/IconSprite";
|
||||
import PwaBoot from "@/components/PwaBoot";
|
||||
import SiteBoot from "@/components/SiteBoot";
|
||||
import SiteFooter from "@/components/SiteFooter";
|
||||
import SiteHeader from "@/components/SiteHeader";
|
||||
@@ -19,12 +20,31 @@ export const metadata: Metadata = {
|
||||
},
|
||||
description:
|
||||
"ศูนย์รวมข้อมูลตารางเดินรถ จุดจอด และเส้นทางในจังหวัดอ่างทองและจังหวัดใกล้เคียง",
|
||||
applicationName: "Atroute",
|
||||
manifest: "/ui-manifest.json",
|
||||
icons: { icon: "/image/icons/minibus-512x512.png" },
|
||||
icons: {
|
||||
icon: [
|
||||
{ url: "/image/icons/minibus-512x512.png", sizes: "512x512", type: "image/png" },
|
||||
{ url: "/image/icons/minibus-192x192.png", sizes: "192x192", type: "image/png" },
|
||||
],
|
||||
apple: [{ url: "/image/icons/minibus-192x192.png", sizes: "192x192", type: "image/png" }],
|
||||
},
|
||||
appleWebApp: {
|
||||
capable: true,
|
||||
title: "Atroute",
|
||||
statusBarStyle: "default",
|
||||
},
|
||||
formatDetection: { telephone: false, address: false, email: false },
|
||||
};
|
||||
|
||||
export const viewport: Viewport = {
|
||||
themeColor: "#7a4cc0",
|
||||
width: "device-width",
|
||||
initialScale: 1,
|
||||
// ห้าม maximumScale:1 / userScalable:false — ปิดการซูมจะไม่ผ่าน WCAG 1.4.4
|
||||
maximumScale: 5,
|
||||
viewportFit: "cover",
|
||||
interactiveWidget: "resizes-content",
|
||||
};
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
@@ -38,10 +58,9 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
||||
rel="stylesheet"
|
||||
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"
|
||||
/>
|
||||
<meta name="theme-color" content="#7a4cc0" />
|
||||
<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" />
|
||||
{/* theme-color / apple-touch-icon / apple-mobile-web-app-* / mobile-web-app-capable
|
||||
ถูก emit อัตโนมัติจาก metadata.appleWebApp + metadata.icons + viewport.themeColor
|
||||
— อย่าใส่ซ้ำที่นี่ (จะได้ meta ซ้ำ 2 ชุด) */}
|
||||
<script src="/vendor/leaflet/leaflet.js" defer />
|
||||
</head>
|
||||
<body className="main-theme-page">
|
||||
@@ -50,6 +69,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
||||
<div id="view">{children}</div>
|
||||
<SiteFooter />
|
||||
<SiteBoot />
|
||||
<PwaBoot />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* Atroute — PWA bootstrap (Next.js client component)
|
||||
*
|
||||
* ทำ 3 อย่าง
|
||||
* 1. ลงทะเบียน /sw.js → ทำให้ Chrome/Samsung Internet ยอมให้ "ติดตั้งเป็นแอป"
|
||||
* 2. จับ beforeinstallprompt → แสดงปุ่ม "ติดตั้งแอป" ของเราเอง
|
||||
* (Android/Chrome/Edge/Samsung — iOS ไม่มี event นี้)
|
||||
* 3. iOS/iPadOS → แสดงคำใบ้ "แชร์ → เพิ่มไปยังหน้าจอโฮม" แทนปุ่ม
|
||||
*
|
||||
* หมายเหตุความปลอดภัย: เบราว์เซอร์จะไม่ให้ติดตั้งถ้าไม่ใช่ HTTPS
|
||||
* component นี้จะไม่แสดงปุ่มและไม่ error ถ้าเงื่อนไขไม่ครบ
|
||||
*/
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type InstallPromptEvent = Event & {
|
||||
prompt: () => Promise<void>;
|
||||
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
|
||||
};
|
||||
|
||||
const isStandalone = () =>
|
||||
typeof window !== "undefined" &&
|
||||
(window.matchMedia("(display-mode: standalone)").matches ||
|
||||
// iOS
|
||||
(window.navigator as unknown as { standalone?: boolean }).standalone === true);
|
||||
|
||||
export default function PwaBoot() {
|
||||
const [deferred, setDeferred] = useState<InstallPromptEvent | null>(null);
|
||||
const [showIosHint, setShowIosHint] = useState(false);
|
||||
const [installed, setInstalled] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined") return;
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
// dev server ไม่มี HTTPS/scope ที่ถูก → ไม่ต้องรบกวน
|
||||
}
|
||||
|
||||
setInstalled(isStandalone());
|
||||
|
||||
/* ---------- 1) service worker ---------- */
|
||||
if ("serviceWorker" in navigator) {
|
||||
const register = () => {
|
||||
navigator.serviceWorker
|
||||
.register("/sw.js", { scope: "/", updateViaCache: "none" })
|
||||
.then((reg) => {
|
||||
reg.update().catch(() => undefined);
|
||||
// ตรวจอัปเดตซ้ำทุก 30 นาที (จอที่เปิดค้าง)
|
||||
setInterval(() => reg.update().catch(() => undefined), 30 * 60 * 1000);
|
||||
})
|
||||
.catch(() => undefined);
|
||||
};
|
||||
if (document.readyState === "complete") register();
|
||||
else window.addEventListener("load", register, { once: true });
|
||||
|
||||
/* SW ใหม่เข้าควบคุม → โหลดใหม่เงียบ ๆ (ผู้ใช้ไม่ต้องล้างแคช) */
|
||||
navigator.serviceWorker.addEventListener("message", (e) => {
|
||||
if (e.data?.type === "SW_ACTIVATED") {
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------- 2) install prompt (Android) ---------- */
|
||||
const onPrompt = (e: Event) => {
|
||||
e.preventDefault();
|
||||
setDeferred(e as InstallPromptEvent);
|
||||
};
|
||||
const onInstalled = () => {
|
||||
setInstalled(true);
|
||||
setDeferred(null);
|
||||
};
|
||||
window.addEventListener("beforeinstallprompt", onPrompt);
|
||||
window.addEventListener("appinstalled", onInstalled);
|
||||
|
||||
/* ---------- 3) iOS/iPadOS hint ---------- */
|
||||
const ua = navigator.userAgent;
|
||||
const isIos =
|
||||
/iPad|iPhone|iPod/.test(ua) ||
|
||||
(navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1);
|
||||
// Samsung Internet ก็ไม่ยิง beforeinstallprompt เหมือนกันในบางเวอร์ชัน
|
||||
const isSamsung = /SamsungBrowser/i.test(ua);
|
||||
if ((isIos || isSamsung) && !isStandalone()) setShowIosHint(true);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("beforeinstallprompt", onPrompt);
|
||||
window.removeEventListener("appinstalled", onInstalled);
|
||||
};
|
||||
}, []);
|
||||
|
||||
if (installed) return null;
|
||||
if (!deferred && !showIosHint) return null;
|
||||
|
||||
const btn: React.CSSProperties = {
|
||||
position: "fixed",
|
||||
insetInlineEnd: 16,
|
||||
bottom: 16,
|
||||
zIndex: 2000,
|
||||
display: "inline-flex",
|
||||
alignItems: "center",
|
||||
gap: 8,
|
||||
minHeight: 44,
|
||||
padding: "10px 18px",
|
||||
border: 0,
|
||||
borderRadius: 999,
|
||||
background: "#7a4cc0",
|
||||
color: "#fff",
|
||||
font: "inherit",
|
||||
fontSize: 14,
|
||||
fontWeight: 600,
|
||||
cursor: "pointer",
|
||||
boxShadow: "0 2px 10px rgba(0,0,0,.18)",
|
||||
};
|
||||
|
||||
if (showIosHint && !deferred) {
|
||||
return (
|
||||
<div style={{ ...btn, background: "#f4f0fb", color: "#4a2f7a", cursor: "default" }}>
|
||||
<span aria-hidden="true">↥</span>
|
||||
<span>แชร์ → เพิ่มไปยังหน้าจอโฮม</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
style={btn}
|
||||
onClick={async () => {
|
||||
if (!deferred) return;
|
||||
await deferred.prompt();
|
||||
const { outcome } = await deferred.userChoice;
|
||||
if (outcome === "accepted") setInstalled(true);
|
||||
setDeferred(null);
|
||||
}}
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
width="18"
|
||||
height="18"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
|
||||
<path d="m7 10 5 5 5-5" />
|
||||
<path d="M12 15V3" />
|
||||
</svg>
|
||||
ติดตั้งแอป
|
||||
</button>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user