feat: PWA offline capture — IndexedDB queue, service worker cache, auto-sync on reconnect

This commit is contained in:
2026-07-11 19:07:35 +08:00
parent 8c88118b15
commit c048878600
7 changed files with 251 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 192 192">
<rect width="192" height="192" fill="#2563eb" rx="24"/>
<text x="96" y="80" font-family="sans-serif" font-size="36" font-weight="bold" fill="white" text-anchor="middle">IMS</text>
<text x="96" y="126" font-family="sans-serif" font-size="22" fill="#bfdbfe" text-anchor="middle">HSE Report</text>
</svg>

After

Width:  |  Height:  |  Size: 371 B

+18
View File
@@ -0,0 +1,18 @@
{
"name": "IMS — Incident Management",
"short_name": "IMS",
"description": "HSE Incident Management System — Setia Corporation",
"start_url": "/ims/report",
"scope": "/ims/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#2563eb",
"icons": [
{
"src": "/ims/icons/icon.svg",
"sizes": "any",
"type": "image/svg+xml",
"purpose": "any maskable"
}
]
}
+60
View File
@@ -0,0 +1,60 @@
const CACHE_NAME = 'ims-v1'
self.addEventListener('install', () => {
self.skipWaiting()
})
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k)))
)
)
self.clients.claim()
})
self.addEventListener('fetch', event => {
const url = new URL(event.request.url)
// Only handle same-origin GET requests under /ims/
if (
event.request.method !== 'GET' ||
url.origin !== self.location.origin ||
!url.pathname.startsWith('/ims/')
) {
return
}
// Skip API routes — always go to network
if (url.pathname.startsWith('/ims/api/')) return
// Cache-first for immutable Next.js static assets
if (url.pathname.startsWith('/ims/_next/static/')) {
event.respondWith(
caches.match(event.request).then(cached => {
if (cached) return cached
return fetch(event.request).then(res => {
if (res.ok) {
const clone = res.clone()
caches.open(CACHE_NAME).then(c => c.put(event.request, clone))
}
return res
})
})
)
return
}
// Network-first for pages — fall back to cache when offline
event.respondWith(
fetch(event.request)
.then(res => {
if (res.ok) {
const clone = res.clone()
caches.open(CACHE_NAME).then(c => c.put(event.request, clone))
}
return res
})
.catch(() => caches.match(event.request))
)
})