feat: pwa and responsive
This commit is contained in:
1 parent
71dacc0da2
commit
6b612e2758
7 files changed
+269
-18
No files matched your search
@@ -491,3 +491,28 @@ body:has(.mu-site-footer) .mu-footer { display: none; }
|
||||
outline: 2px solid #7a4cc0;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
/* ---------- ปุ่มติดตั้งแอป (PWA — inject จาก pwa.js) ---------- */
|
||||
.mu-install-btn {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
right: 20px;
|
||||
z-index: 100;
|
||||
display: none; /* โผล่เมื่อ beforeinstallprompt เท่านั้น */
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-height: 48px;
|
||||
padding: 12px 20px;
|
||||
border: none;
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
font-family: inherit;
|
||||
font-size: .95rem;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
background: linear-gradient(135deg, #a855f7, #7c3aed);
|
||||
box-shadow: 0 10px 24px rgba(124, 58, 237, .35);
|
||||
transition: transform .15s ease;
|
||||
}
|
||||
.mu-install-btn:active { transform: translateY(1px); }
|
||||
.mu-install-btn svg { width: 20px; height: 20px; }
|
||||
+28
-2
@@ -2,6 +2,7 @@
|
||||
<html lang="th">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<title>หน้าแรก | AT Transit (mockup)</title>
|
||||
<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">
|
||||
@@ -13,7 +14,7 @@
|
||||
|
||||
/* Page-specific: hero */
|
||||
.mu-hero { position: relative; overflow: hidden; }
|
||||
.mu-hero-map { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: .14; filter: saturate(1.1); z-index: 0; pointer-events: none; }
|
||||
.mu-hero-map { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: .3; filter: saturate(1.1); z-index: 0; pointer-events: none; }
|
||||
.mu-hero-content { position: relative; z-index: 1; }
|
||||
|
||||
/* ===== maintheme-style cards: solid gradient block with big centered number ===== */
|
||||
@@ -64,7 +65,7 @@
|
||||
|
||||
/* Hero */
|
||||
.mu-hero { position: relative; overflow: hidden; padding: 56px 0 64px; background: linear-gradient(180deg, #f0ebff 0%, #ffe8ee 50%, #fff4d6 100%); text-align: center; }
|
||||
.mu-hero-map { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: .14; filter: saturate(1.1); z-index: 0; pointer-events: none; }
|
||||
.mu-hero-map { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: .3; filter: saturate(1.1); z-index: 0; pointer-events: none; }
|
||||
.mu-blob { position: absolute; border-radius: 50%; filter: blur(50px); opacity: .55; z-index: 0; }
|
||||
.mu-blob.one { width: 220px; height: 220px; background: #b794f4; top: -50px; left: 8%; }
|
||||
.mu-blob.two { width: 260px; height: 260px; background: #f9a8d4; top: 40%; right: 8%; }
|
||||
@@ -374,6 +375,17 @@
|
||||
<a href="#/stops" data-i18n="nav.stops">ป้ายหยุดรถ</a>
|
||||
<a href="#/routes" data-i18n="nav.routes">รายละเอียดสายรถ</a>
|
||||
</nav>
|
||||
<details class="mu-menu" id="topnav">
|
||||
<summary class="mu-menu-btn" aria-label="เมนูหลัก" aria-haspopup="true">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true"><line x1="4" x2="20" y1="6" y2="6"/><line x1="4" x2="20" y1="12" y2="12"/><line x1="4" x2="20" y1="18" y2="18"/></svg>
|
||||
</summary>
|
||||
<div class="mu-menu-panel" role="menu">
|
||||
<a href="#/" data-i18n="nav.home" role="menuitem">หน้าแรก</a>
|
||||
<a href="#/stops" data-i18n="nav.stops" role="menuitem">ป้ายหยุดรถ</a>
|
||||
<a href="#/routes" data-i18n="nav.routes" role="menuitem">รายละเอียดสายรถ</a>
|
||||
<a href="#/check" data-i18n="cta.check" role="menuitem">เช็กเวลาเดินรถ</a>
|
||||
</div>
|
||||
</details>
|
||||
<div class="mu-lang-switch" role="group" aria-label="ภาษา / Language">
|
||||
<button type="button" class="mu-lang-btn" data-i18n-switch="th">ไทย</button>
|
||||
<button type="button" class="mu-lang-btn" data-i18n-switch="en">EN</button>
|
||||
@@ -444,5 +456,19 @@
|
||||
el.textContent = n.toLocaleString('en-US');
|
||||
})();
|
||||
</script>
|
||||
|
||||
<script>
|
||||
// ปิดเมนู hamburger เมื่อ hash เปลี่ยน หรือคลิกนอกเมนู
|
||||
(function () {
|
||||
var menu = document.getElementById('topnav');
|
||||
function close() { if (menu) menu.removeAttribute('open'); }
|
||||
window.addEventListener('hashchange', close);
|
||||
document.addEventListener('click', function (e) {
|
||||
if (!menu) return;
|
||||
if (e.target.closest('.mu-menu-panel a')) { close(); return; }
|
||||
if (!e.target.closest('.mu-menu')) close();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"id": "./index.html",
|
||||
"name": "AT Transit — สถานีขนส่งผู้โดยสารจังหวัดอ่างทอง",
|
||||
"short_name": "AT Transit",
|
||||
"description": "ค้นหาเส้นทาง ตารางเดินรถ และจุดจอดในจังหวัดอ่างทองและจังหวัดใกล้เคียง",
|
||||
@@ -10,6 +11,30 @@
|
||||
"orientation": "any",
|
||||
"background_color": "#fcfbff",
|
||||
"theme_color": "#7a4cc0",
|
||||
"categories": ["travel", "navigation", "transportation"],
|
||||
"shortcuts": [
|
||||
{
|
||||
"name": "เช็กเวลาเดินรถ",
|
||||
"short_name": "เช็กเวลา",
|
||||
"description": "ดูตารางเวลาเดินรถทุกจุดจอด",
|
||||
"url": "./index.html#/check",
|
||||
"icons": [{ "src": "../.image/icons/minibus-192x192.png", "sizes": "192x192", "type": "image/png" }]
|
||||
},
|
||||
{
|
||||
"name": "ป้ายหยุดรถ",
|
||||
"short_name": "ป้ายหยุดรถ",
|
||||
"description": "ค้นหาป้ายหยุดรถทั้งหมด",
|
||||
"url": "./index.html#/stops",
|
||||
"icons": [{ "src": "../.image/icons/minibus-192x192.png", "sizes": "192x192", "type": "image/png" }]
|
||||
},
|
||||
{
|
||||
"name": "รายละเอียดสายรถ",
|
||||
"short_name": "สายรถ",
|
||||
"description": "ดูรายละเอียดเส้นทางเดินรถ",
|
||||
"url": "./index.html#/routes",
|
||||
"icons": [{ "src": "../.image/icons/minibus-192x192.png", "sizes": "192x192", "type": "image/png" }]
|
||||
}
|
||||
],
|
||||
"icons": [
|
||||
{
|
||||
"src": "../.image/icons/minibus-192x192.png",
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="th">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="theme-color" content="#7a4cc0">
|
||||
<title>ออฟไลน์ | AT Transit</title>
|
||||
<style>
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
font-family: "Noto Sans Thai", "Prompt", system-ui, sans-serif;
|
||||
color: #1f2a44;
|
||||
background:
|
||||
radial-gradient(circle at 15% 15%, rgba(122,76,192,.18), transparent 40%),
|
||||
radial-gradient(circle at 85% 10%, rgba(48,113,184,.16), transparent 40%),
|
||||
linear-gradient(180deg, #f0ebff 0%, #ffe8ee 55%, #fff4d6 100%);
|
||||
}
|
||||
.card {
|
||||
max-width: 420px;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
background: #fff;
|
||||
border: 1px solid #e7e2f6;
|
||||
border-radius: 20px;
|
||||
padding: 36px 28px;
|
||||
box-shadow: 0 18px 40px rgba(63,43,94,.12);
|
||||
}
|
||||
.icon {
|
||||
width: 72px; height: 72px;
|
||||
margin: 0 auto 18px;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
border-radius: 20px;
|
||||
background: linear-gradient(135deg, #7a4cc0, #b07ce0);
|
||||
color: #fff;
|
||||
box-shadow: 0 10px 24px rgba(122,76,192,.28);
|
||||
}
|
||||
.icon svg { width: 38px; height: 38px; }
|
||||
h1 { font-size: 1.5rem; font-weight: 800; margin-bottom: 10px; }
|
||||
p { color: #6f7b95; font-size: 1rem; line-height: 1.6; margin-bottom: 22px; }
|
||||
button {
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
gap: 8px;
|
||||
min-height: 48px; padding: 12px 24px;
|
||||
border: none; border-radius: 12px; cursor: pointer;
|
||||
font-family: inherit; font-size: 1rem; font-weight: 700;
|
||||
background: linear-gradient(135deg, #a855f7, #7c3aed); color: #fff;
|
||||
box-shadow: 0 8px 18px rgba(124,58,237,.28);
|
||||
}
|
||||
button:active { transform: translateY(1px); }
|
||||
a {
|
||||
display: inline-block; margin-top: 14px;
|
||||
color: #7a4cc0; font-size: .95rem; font-weight: 600;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<div class="icon" aria-hidden="true">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h.01"/><path d="M8.5 16.429a5 5 0 0 1 7 0"/><path d="M5 12.859a10 10 0 0 1 5.17-2.69"/><path d="M19 12.859a10 10 0 0 0-2.007-1.523"/><path d="M2 8.82a15 15 0 0 1 4.177-2.643"/><path d="M22 8.82a15 15 0 0 0-11.288-3.764"/><path d="m2 2 20 20"/></svg>
|
||||
</div>
|
||||
<h1>คุณกำลังออฟไลน์</h1>
|
||||
<p>เชื่อมต่ออินเทอร์เน็ตไม่ได้ — ไม่สามารถโหลดเนื้อหาล่าสุดได้ โปรดตรวจสอบการเชื่อมต่อแล้วลองใหม่อีกครั้ง</p>
|
||||
<button type="button" onclick="location.reload()">ลองใหม่</button>
|
||||
<br>
|
||||
<a href="./index.html">กลับหน้าแรก</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -12,6 +12,38 @@
|
||||
|
||||
var CHECK_INTERVAL_MS = 30 * 60 * 1000; // ตรวจอัปเดตทุก 30 นาที
|
||||
|
||||
/* ---- ปุ่มติดตั้งแอป (ก่อน install prompt) ---- */
|
||||
var deferredPrompt = null;
|
||||
var installBtn = null;
|
||||
|
||||
function makeInstallBtn() {
|
||||
var btn = document.createElement("button");
|
||||
btn.type = "button";
|
||||
btn.className = "mu-install-btn";
|
||||
btn.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="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> ติดตั้งแอป';
|
||||
btn.addEventListener("click", function () {
|
||||
if (!deferredPrompt) return;
|
||||
deferredPrompt.prompt();
|
||||
deferredPrompt.userChoice.then(function () {
|
||||
deferredPrompt = null;
|
||||
if (installBtn) installBtn.style.display = "none";
|
||||
});
|
||||
});
|
||||
document.body.appendChild(btn);
|
||||
return btn;
|
||||
}
|
||||
|
||||
window.addEventListener("beforeinstallprompt", function (e) {
|
||||
e.preventDefault();
|
||||
deferredPrompt = e;
|
||||
if (!installBtn) installBtn = makeInstallBtn();
|
||||
installBtn.style.display = "inline-flex";
|
||||
});
|
||||
|
||||
window.addEventListener("appinstalled", function () {
|
||||
if (installBtn) installBtn.style.display = "none";
|
||||
});
|
||||
|
||||
window.addEventListener("load", function () {
|
||||
navigator.serviceWorker
|
||||
.register("./sw.js", { updateViaCache: "none" })
|
||||
|
||||
+61
-13
@@ -17,7 +17,25 @@
|
||||
|
||||
/* ---------- base: มือถือ ---------- */
|
||||
html { -webkit-text-size-adjust: 100%; }
|
||||
body { font-size: 15px; }
|
||||
body { font-size: 16px; }
|
||||
|
||||
/* --- ขยายขนาด component บนมือถือ (desktop-oriented เดิมเล็กเกิน) --- */
|
||||
/* topbar อยู่ใน section ด้านล่าง — content ใน #view ใช้ prefix ชนะ partial style */
|
||||
#view .mu-btn { font-size: .95rem; min-height: 48px; }
|
||||
#view .mu-badge { font-size: 1.05rem; }
|
||||
#view .mu-zone-tag { font-size: 1rem; }
|
||||
#view .mu-zone-desc, #view .mu-zone-sub { font-size: 1.05rem; }
|
||||
#view .mu-route-name { font-size: 1.2rem; }
|
||||
#view .mu-next-trip { font-size: .95rem; }
|
||||
#view .mu-meta-pill { font-size: .95rem; }
|
||||
#view .mu-route-type-badge { font-size: .85rem; }
|
||||
#view .mu-stop-summary-meta { font-size: .85rem; }
|
||||
#view .mu-time-chip { font-size: .95rem; min-height: 40px; }
|
||||
#view .mu-info-card .label { font-size: .85rem; }
|
||||
#view .mu-info-card .value { font-size: 1.1rem; }
|
||||
#view .mu-stat p { font-size: .9rem; }
|
||||
#view .mu-hero-lead { font-size: 1rem; }
|
||||
#view .mu-select-box select { font-size: 1rem; }
|
||||
|
||||
/* --- กัน overflow พื้นฐาน (padding/รูป/grid/ข้อความยาว ไม่ให้ล้นจอมือถือ) --- */
|
||||
*, *::before, *::after { box-sizing: border-box; }
|
||||
@@ -35,21 +53,49 @@ img, svg, video { max-width: 100%; height: auto; }
|
||||
.mu-container { padding: 0 16px; }
|
||||
main.mu-container { padding-top: 24px; padding-bottom: 24px; }
|
||||
|
||||
/* --- topbar: 2 แถวบนมือถือ (brand+lang บรรทัด1, nav บรรทัด2) — ไม่มี scroll ซ้ายขวา --- */
|
||||
/* --- topbar: hamburger menu บนมือถือ (brand + lang + ☰ บรรทัดเดียว) --- */
|
||||
.mu-topbar-inner { gap: 8px; padding: 10px 14px; flex-wrap: wrap; }
|
||||
.mu-logo-img { width: 36px; height: 36px; }
|
||||
.mu-brand { gap: 8px; flex-shrink: 0; }
|
||||
.mu-brand-title { font-size: .92rem; white-space: nowrap; }
|
||||
.mu-brand-title { font-size: 1.05rem; white-space: nowrap; }
|
||||
.mu-brand-subtitle { display: none; } /* กันล้น — โผล่กลับ ≥768px */
|
||||
.mu-nav { order: 3; width: 100%; justify-content: space-between; margin-left: 0; gap: 8px; }
|
||||
.mu-nav a { font-size: .76rem; padding: 10px 2px; white-space: nowrap; } /* tap target ≥44px */
|
||||
.mu-lang-switch { margin-left: auto; padding: 2px; }
|
||||
.mu-lang-btn { font-size: .68rem; padding: 8px 12px; min-height: 40px; min-width: 40px; } /* tap target */
|
||||
.mu-nav { display: none; } /* ซ่อน nav — ใช้ hamburger แทน */
|
||||
.mu-lang-switch { order: 3; margin-left: auto; padding: 2px; }
|
||||
.mu-lang-btn { font-size: .8rem; padding: 8px 12px; min-height: 40px; min-width: 40px; } /* tap target */
|
||||
.mu-action-btn { display: none; } /* กันล้นบนจอเล็ก — โผล่กลับ ≥1024px */
|
||||
|
||||
/* --- hero (หน้าแรก) — ลดช่องว่างแนวตั้ง --- */
|
||||
.mu-hero { padding: 20px 0 22px; }
|
||||
.mu-hero h1 { font-size: clamp(1.6rem, 7vw, 2.4rem); margin-bottom: 10px; line-height: 1.28; }
|
||||
/* hamburger menu (ขวาสุด) */
|
||||
.mu-menu { display: block; position: relative; order: 4; margin-left: 2px; }
|
||||
.mu-menu-btn {
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
width: 44px; height: 44px;
|
||||
border: none; background: transparent; cursor: pointer;
|
||||
color: #1f2a44; list-style: none;
|
||||
border-radius: 10px;
|
||||
}
|
||||
.mu-menu-btn::-webkit-details-marker { display: none; }
|
||||
.mu-menu-btn svg { width: 26px; height: 26px; }
|
||||
.mu-menu-panel {
|
||||
position: absolute; top: calc(100% + 8px); right: 0;
|
||||
min-width: 220px;
|
||||
background: #fff; border: 1px solid #e7e2f6; border-radius: 14px;
|
||||
box-shadow: 0 12px 28px rgba(20, 40, 90, .16);
|
||||
padding: 8px;
|
||||
z-index: 60;
|
||||
display: flex; flex-direction: column; gap: 2px;
|
||||
}
|
||||
.mu-menu-panel a {
|
||||
display: flex; align-items: center;
|
||||
min-height: 48px; padding: 10px 14px;
|
||||
border-radius: 10px;
|
||||
color: #1f2a44; text-decoration: none;
|
||||
font-size: 1.05rem; font-weight: 600;
|
||||
}
|
||||
.mu-menu-panel a:hover, .mu-menu-panel a:active { background: #f5efff; }
|
||||
|
||||
/* --- hero (หน้าแรก) — ช่องว่าง + ตัวหนังสือใหญ่ขึ้น --- */
|
||||
.mu-hero { padding: 30px 0 34px; }
|
||||
.mu-hero h1 { font-size: clamp(1.9rem, 8vw, 2.9rem); margin-bottom: 14px; line-height: 1.28; }
|
||||
.mu-badge { margin-bottom: 10px; padding: 5px 14px; }
|
||||
.mu-hero-lead { margin: 0 0 8px; }
|
||||
.mu-hero-actions { flex-direction: column; align-items: stretch; padding: 0 6px; margin-top: 10px; gap: 8px; }
|
||||
@@ -72,7 +118,7 @@ main.mu-container { padding-top: 14px; padding-bottom: 14px; }
|
||||
.mu-zone { margin-bottom: 20px; }
|
||||
.mu-zone-head { margin-bottom: 10px; gap: 10px; }
|
||||
.mu-grid { gap: 12px; }
|
||||
.mu-route-info { padding: 12px 12px 13px; }
|
||||
.mu-route-info { padding: 16px 16px 18px; }
|
||||
.mu-route-name { margin-bottom: 6px; }
|
||||
.mu-route-chip-wrap { gap: 6px; }
|
||||
.mu-next-trip { padding: 3px 10px; font-size: .74rem; }
|
||||
@@ -85,6 +131,7 @@ main.mu-container { padding-top: 14px; padding-bottom: 14px; }
|
||||
#view .mu-toolbar select,
|
||||
#view .mu-toolbar .mu-btn { width: 100%; min-height: 44px; }
|
||||
#view .mu-search-field { width: 100%; min-width: 0; }
|
||||
#view .mu-search-field input { min-height: 44px; }
|
||||
#view .mu-province-strip {
|
||||
display: flex; flex-wrap: wrap; padding-bottom: 6px;
|
||||
}
|
||||
@@ -102,7 +149,7 @@ main.mu-container { padding-top: 14px; padding-bottom: 14px; }
|
||||
#view .mu-photo-strip { grid-template-columns: repeat(2, 1fr); gap: 8px; }
|
||||
#view .mu-quickpick { display: flex; flex-wrap: wrap; gap: 8px; }
|
||||
#view .mu-inline-actions { flex-wrap: wrap; }
|
||||
#view .mu-back-btn { width: 100%; justify-content: center; padding: 10px; }
|
||||
#view .mu-back-btn { width: 100%; justify-content: center; padding: 10px; min-height: 44px; }
|
||||
#view .mu-schedule-note { font-size: .82rem; }
|
||||
|
||||
/* --- stopDetail --- */
|
||||
@@ -146,8 +193,9 @@ main.mu-container { padding-top: 14px; padding-bottom: 14px; }
|
||||
.mu-brand-title { font-size: 1.05rem; }
|
||||
.mu-topbar-inner { gap: 18px; padding: 14px 28px; flex-wrap: nowrap; }
|
||||
.mu-logo-img { width: 44px; height: 44px; }
|
||||
.mu-nav { order: 0; width: auto; margin-left: auto; justify-content: flex-start; gap: 24px; }
|
||||
.mu-nav { order: 0; width: auto; margin-left: auto; justify-content: flex-start; gap: 24px; display: flex; }
|
||||
.mu-nav a { font-size: .92rem; }
|
||||
.mu-menu { display: none; }
|
||||
.mu-lang-btn { font-size: .72rem; padding: 6px 10px; }
|
||||
.mu-hero { padding: 44px 0 52px; }
|
||||
.mu-hero-actions { margin-top: 14px; }
|
||||
|
||||
@@ -13,11 +13,12 @@
|
||||
- ขั้นตอนอัปเดต: ดู AGENTS.md หัวข้อ PWA
|
||||
============================================================ */
|
||||
|
||||
const VERSION = "2026.09.02.9"; // ← bump ทุกครั้งที่แก้ SW เอง
|
||||
const VERSION = "2026.09.12.1"; // ← bump ทุกครั้งที่แก้ SW เอง
|
||||
const CACHE = `at-transit-${VERSION}`;
|
||||
|
||||
const PRECACHE = [
|
||||
"./index.html",
|
||||
"./offline.html",
|
||||
"./busStop.html",
|
||||
"./busDetail.html",
|
||||
"./busDetailExpanded.html",
|
||||
@@ -87,9 +88,14 @@ self.addEventListener("fetch", (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// หน้า HTML + CSS + JS + partials + ข้อมูล JSON → network-first
|
||||
// หน้า HTML (navigation) → network-first แต่ถ้าออฟไลน์ไม่มี cache → offline.html
|
||||
if (request.destination === "document") {
|
||||
event.respondWith(networkFirstDocument(request));
|
||||
return;
|
||||
}
|
||||
|
||||
// CSS + JS + partials + ข้อมูล JSON → network-first
|
||||
if (
|
||||
request.destination === "document" ||
|
||||
url.pathname.endsWith(".css") ||
|
||||
url.pathname.endsWith(".js") ||
|
||||
url.pathname.endsWith(".html") ||
|
||||
@@ -121,6 +127,21 @@ async function networkFirst(request) {
|
||||
}
|
||||
}
|
||||
|
||||
/* สำหรับ navigation: ถ้าออฟไลน์และไม่มี cache ให้หน้า offline.html */
|
||||
async function networkFirstDocument(request) {
|
||||
const cache = await caches.open(CACHE);
|
||||
try {
|
||||
const res = await fetch(request, { cache: "no-store" });
|
||||
if (res.ok) cache.put(request, res.clone());
|
||||
return res;
|
||||
} catch {
|
||||
const hit = await cache.match(request);
|
||||
if (hit) return hit;
|
||||
const offline = await cache.match("./offline.html");
|
||||
return offline || Response.error();
|
||||
}
|
||||
}
|
||||
|
||||
async function cacheFirst(request) {
|
||||
const hit = await caches.match(request);
|
||||
if (hit) return hit;
|
||||
|
||||
Reference in new issue
Block a user