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
@@ -0,0 +1,47 @@
|
||||
<!doctype html>
|
||||
<html lang="th">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
|
||||
<title>ออฟไลน์ | Atroute</title>
|
||||
<link rel="stylesheet" href="/vendor/fonts/k2d.css" />
|
||||
<style>
|
||||
:root { color-scheme: light; }
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100dvh;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: 24px;
|
||||
background: #fcfbff;
|
||||
color: #2c2340;
|
||||
font-family: "K2D", "Noto Sans Thai", system-ui, sans-serif;
|
||||
text-align: center;
|
||||
}
|
||||
.box { max-width: 360px; }
|
||||
img { width: 72px; height: 72px; object-fit: contain; margin-bottom: 18px; }
|
||||
h1 { font-size: 1.25rem; margin: 0 0 8px; }
|
||||
p { margin: 0 0 20px; color: #6f6a80; line-height: 1.6; font-size: 0.95rem; }
|
||||
button {
|
||||
min-height: 44px;
|
||||
padding: 10px 22px;
|
||||
border: 0;
|
||||
border-radius: 999px;
|
||||
background: #7a4cc0;
|
||||
color: #fff;
|
||||
font: inherit;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="box">
|
||||
<img src="/image/icons/minibus-512x512.png" alt="" />
|
||||
<h1>ไม่มีการเชื่อมต่อ</h1>
|
||||
<p>หน้านี้ต้องใช้อินเทอร์เน็ต — หน้ำที่เคยเปิดไว้จะยังใช้งานได้ตอนออฟไลน์</p>
|
||||
<button type="button" onclick="location.reload()">ลองอีกครั้ง</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,123 @@
|
||||
/* ============================================================
|
||||
Atroute — Service Worker (Next.js)
|
||||
|
||||
ทำไมต้องมี:
|
||||
Chrome/Edge/Samsung Internet จะ "ติดตั้งเป็นแอป" ได้ก็ต่อเมื่อ
|
||||
1) เสิร์ฟผ่าน HTTPS (หรือ localhost)
|
||||
2) มี manifest ที่ถูกต้อง (192 + 512 icon, start_url, display)
|
||||
3) ★ มี service worker ที่มี fetch handler ควบคุมหน้าเว็บ
|
||||
ข้อ 3 คือสิ่งที่ Next.js ไม่มีให้เอง — ไฟล์นี้คือคำตอบ
|
||||
|
||||
กลยุทธ์
|
||||
- นำทาง (HTML) → network-first (ออนไลน์ได้ของใหม่, ออฟไลน์ใช้ cache)
|
||||
- /_next/static, /image, /vendor, ฟอนต์ → cache-first (เร็ว + ออฟไลน์ได้)
|
||||
- /api → ไม่แตะ (ให้ network จัดการ)
|
||||
- SW ใหม่ → skipWaiting + clients.claim + แจ้งหน้าเว็บ
|
||||
============================================================ */
|
||||
|
||||
const VERSION = "1.0.0";
|
||||
const CACHE = `atroute-${VERSION}`;
|
||||
const OFFLINE_URL = "/offline.html";
|
||||
|
||||
/* ไฟล์ที่ precache ไว้ใช้ตอนออฟไลน์ (เฉพาะที่ path คงที่) */
|
||||
const PRECACHE = [
|
||||
"/",
|
||||
OFFLINE_URL,
|
||||
"/ui-manifest.json",
|
||||
"/image/icons/minibus-192x192.png",
|
||||
"/image/icons/minibus-512x512.png",
|
||||
"/image/icons/minibus-maskable-512x512.png",
|
||||
"/logo.svg",
|
||||
"/vendor/fonts/k2d.css",
|
||||
"/vendor/leaflet/leaflet.css",
|
||||
];
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.open(CACHE)
|
||||
.then((cache) => cache.addAll(PRECACHE).catch(() => undefined))
|
||||
.then(() => self.skipWaiting()),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
|
||||
.then(() => self.clients.claim())
|
||||
.then(() => {
|
||||
self.clients.matchAll({ type: "window" }).then((cs) => {
|
||||
cs.forEach((c) => c.postMessage({ type: "SW_ACTIVATED", version: VERSION }));
|
||||
});
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("message", (event) => {
|
||||
if (event.data === "SKIP_WAITING") self.skipWaiting();
|
||||
});
|
||||
|
||||
/** ไฟล์ static ที่ cache-first ได้ (path มี hash หรือไม่เปลี่ยนบ่อย) */
|
||||
function isStaticAsset(url) {
|
||||
return (
|
||||
url.pathname.startsWith("/_next/static/") ||
|
||||
url.pathname.startsWith("/image/") ||
|
||||
url.pathname.startsWith("/vendor/") ||
|
||||
url.pathname.startsWith("/content/") ||
|
||||
/\.(?:png|jpe?g|webp|avif|svg|ico|woff2?|ttf|css|js)$/i.test(url.pathname)
|
||||
);
|
||||
}
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
const req = event.request;
|
||||
if (req.method !== "GET") return;
|
||||
|
||||
const url = new URL(req.url);
|
||||
|
||||
/* ไม่ยุ่งกับ API และ cross-origin */
|
||||
if (url.origin !== self.location.origin) return;
|
||||
if (url.pathname.startsWith("/api/") || url.pathname.startsWith("/files/")) return;
|
||||
|
||||
/* ---- นำทาง: network-first + offline fallback ---- */
|
||||
if (req.mode === "navigate") {
|
||||
event.respondWith(
|
||||
fetch(req)
|
||||
.then((res) => {
|
||||
const copy = res.clone();
|
||||
caches.open(CACHE).then((c) => c.put(req, copy)).catch(() => undefined);
|
||||
return res;
|
||||
})
|
||||
.catch(async () => {
|
||||
const cached = await caches.match(req);
|
||||
return cached || (await caches.match(OFFLINE_URL)) || Response.error();
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/* ---- static: cache-first แล้วค่อยเติม cache ---- */
|
||||
if (isStaticAsset(url)) {
|
||||
event.respondWith(
|
||||
caches.match(req).then((cached) => {
|
||||
if (cached) return cached;
|
||||
return fetch(req).then((res) => {
|
||||
if (res.ok && res.type === "basic") {
|
||||
const copy = res.clone();
|
||||
caches.open(CACHE).then((c) => c.put(req, copy)).catch(() => undefined);
|
||||
}
|
||||
return res;
|
||||
});
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
/* ---- อื่น ๆ: network-first ---- */
|
||||
event.respondWith(
|
||||
fetch(req)
|
||||
.then((res) => res)
|
||||
.catch(() => caches.match(req).then((r) => r || Response.error())),
|
||||
);
|
||||
});
|
||||
@@ -10,28 +10,50 @@
|
||||
"orientation": "any",
|
||||
"background_color": "#fcfbff",
|
||||
"theme_color": "#7a4cc0",
|
||||
"categories": ["travel", "navigation", "transportation"],
|
||||
"categories": [
|
||||
"travel",
|
||||
"navigation",
|
||||
"transportation"
|
||||
],
|
||||
"shortcuts": [
|
||||
{
|
||||
"name": "เช็กเวลาเดินรถ",
|
||||
"short_name": "เช็กเวลา",
|
||||
"description": "ดูตารางเวลาเดินรถทุกจุดจอด",
|
||||
"url": "/check",
|
||||
"icons": [{ "src": "/image/icons/minibus-192x192.png", "sizes": "192x192", "type": "image/png" }]
|
||||
"icons": [
|
||||
{
|
||||
"src": "/image/icons/minibus-192x192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "ป้ายหยุดรถ",
|
||||
"short_name": "ป้ายหยุดรถ",
|
||||
"description": "ค้นหาป้ายหยุดรถทั้งหมด",
|
||||
"url": "/stops",
|
||||
"icons": [{ "src": "/image/icons/minibus-192x192.png", "sizes": "192x192", "type": "image/png" }]
|
||||
"icons": [
|
||||
{
|
||||
"src": "/image/icons/minibus-192x192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "รายละเอียดสายรถ",
|
||||
"short_name": "สายรถ",
|
||||
"description": "ดูรายละเอียดเส้นทางเดินรถ",
|
||||
"url": "/routes",
|
||||
"icons": [{ "src": "/image/icons/minibus-192x192.png", "sizes": "192x192", "type": "image/png" }]
|
||||
"icons": [
|
||||
{
|
||||
"src": "/image/icons/minibus-192x192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"icons": [
|
||||
@@ -52,6 +74,34 @@
|
||||
"sizes": "512x512",
|
||||
"type": "image/png",
|
||||
"purpose": "maskable"
|
||||
},
|
||||
{
|
||||
"src": "/image/icons/minibus-maskable-512x512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png",
|
||||
"purpose": "any"
|
||||
}
|
||||
],
|
||||
"id": "/",
|
||||
"display_override": [
|
||||
"standalone",
|
||||
"minimal-ui",
|
||||
"browser"
|
||||
],
|
||||
"prefer_related_applications": false,
|
||||
"launch_handler": {
|
||||
"client_mode": "navigate-existing"
|
||||
},
|
||||
"handle_links": "preferred",
|
||||
"scope_extensions": [],
|
||||
"edge_side_panel": {},
|
||||
"screenshots": [
|
||||
{
|
||||
"src": "/image/network/overview-map.png",
|
||||
"sizes": "1440x1080",
|
||||
"type": "image/png",
|
||||
"form_factor": "wide",
|
||||
"label": "แผนที่เครือข่ายป้าย Atroute"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user