mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:36:30 +00:00
Add PWA support with offline fallback
Add installable app metadata and icons, register a service worker for asset and page caching, and provide an offline page when navigation fails.
This commit is contained in:
@@ -13,6 +13,7 @@ import { Toaster } from '@/components/ui/sonner';
|
||||
import AntdConfigProvider from '@/providers/AntdConfigProvider';
|
||||
import ProgressBarProviders from '@/providers/ProgressBarProvider';
|
||||
import { TooltipProvider } from '@/components/ui/tooltip';
|
||||
import ServiceWorkerRegister from '@/components/pwa/ServiceWorkerRegister';
|
||||
|
||||
|
||||
const jakarta = Plus_Jakarta_Sans({ subsets: ['latin'] })
|
||||
@@ -25,13 +26,40 @@ export const metadata: Metadata = {
|
||||
keywords: ["react", "server components", 'rsc', 'nextjs', 'tailwind', 'typescript', 'shadcn', 'presence', 'absence', 'task', 'admin', 'cms', 'dashboard'],
|
||||
authors: [{ name: 'ichsankurnia', url: 'https://ichsankurnia-portfolio.vercel.app' }],
|
||||
creator: 'ichsankurnia',
|
||||
manifest: '/manifest.webmanifest',
|
||||
appleWebApp: {
|
||||
capable: true,
|
||||
statusBarStyle: 'default',
|
||||
title: 'H.A.T.s',
|
||||
},
|
||||
icons: {
|
||||
icon: [
|
||||
{ url: '/assets/logo192.png', sizes: '192x192', type: 'image/png' },
|
||||
{ url: '/assets/logo512.png', sizes: '512x512', type: 'image/png' },
|
||||
],
|
||||
apple: [
|
||||
{ url: '/assets/apple-touch-icon.png', sizes: '180x180', type: 'image/png' },
|
||||
],
|
||||
},
|
||||
formatDetection: {
|
||||
telephone: false,
|
||||
},
|
||||
openGraph: {
|
||||
title: "HIS Attendance Task System (H.A.T.s)",
|
||||
description: "H.A.T.s HIS Attendance Task System (smart absence, task management, work permit and leave mangement, etc).",
|
||||
images: [
|
||||
{
|
||||
url: '/assets/logo512.png',
|
||||
width: 512,
|
||||
height: 512,
|
||||
alt: 'HIS Attendance Task System (H.A.T.s)',
|
||||
},
|
||||
],
|
||||
},
|
||||
twitter: {
|
||||
title: "HIS Attendance Task System (H.A.T.s)",
|
||||
description: "H.A.T.s HIS Attendance Task System (smart absence, task management, work permit and leave mangement, etc).",
|
||||
images: ['/assets/logo512.png'],
|
||||
},
|
||||
pinterest: {
|
||||
richPin: true
|
||||
@@ -57,6 +85,8 @@ export const viewport: Viewport = {
|
||||
{ media: '(prefers-color-scheme: light)', color: '#EA512C' },
|
||||
{ media: '(prefers-color-scheme: dark)', color: '#EA512C' },
|
||||
],
|
||||
width: 'device-width',
|
||||
initialScale: 1,
|
||||
}
|
||||
|
||||
export default async function RootLayout({
|
||||
@@ -74,9 +104,11 @@ export default async function RootLayout({
|
||||
return (
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<head>
|
||||
<link rel="apple-touch-icon" href="/assets/apple-touch-icon.png" />
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tabler/icons-webfont@3.35.0/dist/tabler-icons.min.css" crossOrigin="anonymous" referrerPolicy="no-referrer"></link>
|
||||
</head>
|
||||
<body className={jakarta.className}>
|
||||
<ServiceWorkerRegister />
|
||||
<SessionProvider session={session} basePath={process.env.BASE_PATH}>
|
||||
|
||||
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { MetadataRoute } from 'next';
|
||||
|
||||
export default function manifest(): MetadataRoute.Manifest {
|
||||
return {
|
||||
name: 'HIS Attendance Task System (H.A.T.s)',
|
||||
short_name: 'H.A.T.s',
|
||||
description: 'H.A.T.s HIS Attendance Task System (smart absence, task management, work permit and leave mangement, etc).',
|
||||
start_url: '/',
|
||||
id: '/',
|
||||
scope: '/',
|
||||
display: 'standalone',
|
||||
background_color: '#ffffff',
|
||||
theme_color: '#EA512C',
|
||||
orientation: 'portrait',
|
||||
icons: [
|
||||
{
|
||||
src: '/assets/logo192.png',
|
||||
sizes: '192x192',
|
||||
type: 'image/png',
|
||||
purpose: 'any',
|
||||
},
|
||||
{
|
||||
src: '/assets/hats-256x256.png',
|
||||
sizes: '256x256',
|
||||
type: 'image/png',
|
||||
purpose: 'any',
|
||||
},
|
||||
{
|
||||
src: '/assets/logo512.png',
|
||||
sizes: '512x512',
|
||||
type: 'image/png',
|
||||
purpose: 'any',
|
||||
},
|
||||
{
|
||||
src: '/assets/logo512.png',
|
||||
sizes: '512x512',
|
||||
type: 'image/png',
|
||||
purpose: 'maskable',
|
||||
},
|
||||
{
|
||||
src: '/assets/apple-touch-icon.png',
|
||||
sizes: '180x180',
|
||||
type: 'image/png',
|
||||
purpose: 'any',
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
'use client';
|
||||
|
||||
import { WifiOff, RefreshCw } from 'lucide-react';
|
||||
import Image from 'next/image';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
export default function OfflinePage() {
|
||||
return (
|
||||
<div className="min-h-screen flex flex-col items-center justify-center p-6 bg-background text-foreground text-center">
|
||||
<div className="max-w-md w-full flex flex-col items-center space-y-6 rounded-2xl border border-border bg-card p-8 shadow-sm">
|
||||
<div className="relative flex h-20 w-20 items-center justify-center rounded-2xl bg-muted/60 p-3">
|
||||
<Image
|
||||
src="/assets/logo192.png"
|
||||
alt="H.A.T.s Logo"
|
||||
width={64}
|
||||
height={64}
|
||||
className="rounded-xl object-contain"
|
||||
/>
|
||||
<div className="absolute -bottom-1 -right-1 rounded-full bg-destructive/10 p-1.5 text-destructive border border-destructive/20">
|
||||
<WifiOff className="h-4 w-4" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-xl font-bold tracking-tight sm:text-2xl">
|
||||
You are currently offline
|
||||
</h1>
|
||||
<p className="text-sm text-muted-foreground leading-relaxed">
|
||||
It looks like your internet connection was lost. Please check your network and try again.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={() => window.location.reload()}
|
||||
className="w-full gap-2 font-medium"
|
||||
>
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
Retry Connection
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export default function ServiceWorkerRegister() {
|
||||
useEffect(() => {
|
||||
if (typeof window !== 'undefined' && 'serviceWorker' in navigator) {
|
||||
const registerSW = async () => {
|
||||
try {
|
||||
const registration = await navigator.serviceWorker.register('/sw.js', {
|
||||
scope: '/',
|
||||
});
|
||||
|
||||
// Listen for updates
|
||||
registration.addEventListener('updatefound', () => {
|
||||
const newWorker = registration.installing;
|
||||
if (newWorker) {
|
||||
newWorker.addEventListener('statechange', () => {
|
||||
if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
|
||||
console.log('[PWA] New content is available; please refresh.');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[PWA] Service Worker registration failed:', error);
|
||||
}
|
||||
};
|
||||
|
||||
if (document.readyState === 'complete') {
|
||||
registerSW();
|
||||
} else {
|
||||
window.addEventListener('load', registerSW);
|
||||
return () => window.removeEventListener('load', registerSW);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -13,7 +13,7 @@ export const config = {
|
||||
* 6. /favicon.ico, /sitemap.xml (static files)
|
||||
* 7. /auth page
|
||||
*/
|
||||
"/((?!api/|_next/|_proxy/|_static|_vercel|auth|terms|privacy-policy|not-found|avatars/|assets/|files/|admin/sourceMap/chrome/css/|admin/administration/sourceMap/chrome/css/|firebase-messaging-sw.js|favicon.ico|sitemap.xml).*)",
|
||||
"/((?!api/|_next/|_proxy/|_static|_vercel|auth|terms|privacy-policy|not-found|avatars/|assets/|files/|admin/sourceMap/chrome/css/|admin/administration/sourceMap/chrome/css/|firebase-messaging-sw.js|sw.js|manifest.json|manifest.webmanifest|offline|favicon.ico|sitemap.xml).*)",
|
||||
],
|
||||
// matcher: [
|
||||
// "/",
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "HIS Attendance Task System (H.A.T.s)",
|
||||
"short_name": "H.A.T.s",
|
||||
"description": "H.A.T.s HIS Attendance Task System (smart absence, task management, work permit and leave mangement, etc).",
|
||||
"start_url": "/",
|
||||
"id": "/",
|
||||
"scope": "/",
|
||||
"display": "standalone",
|
||||
"background_color": "#ffffff",
|
||||
"theme_color": "#EA512C",
|
||||
"orientation": "portrait",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/assets/logo192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png",
|
||||
"purpose": "any"
|
||||
},
|
||||
{
|
||||
"src": "/assets/hats-256x256.png",
|
||||
"sizes": "256x256",
|
||||
"type": "image/png",
|
||||
"purpose": "any"
|
||||
},
|
||||
{
|
||||
"src": "/assets/logo512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png",
|
||||
"purpose": "any"
|
||||
},
|
||||
{
|
||||
"src": "/assets/logo512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png",
|
||||
"purpose": "maskable"
|
||||
},
|
||||
{
|
||||
"src": "/assets/apple-touch-icon.png",
|
||||
"sizes": "180x180",
|
||||
"type": "image/png",
|
||||
"purpose": "any"
|
||||
}
|
||||
]
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
const CACHE_NAME = 'hats-pwa-v1';
|
||||
|
||||
const PRECACHE_ASSETS = [
|
||||
'/',
|
||||
'/offline',
|
||||
'/assets/logo192.png',
|
||||
'/assets/logo512.png',
|
||||
'/assets/apple-touch-icon.png',
|
||||
'/assets/hats-256x256.png',
|
||||
'/assets/illustrations/maintenance.svg',
|
||||
];
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME).then(async (cache) => {
|
||||
// Attempt to precache essential assets, ignoring individual failures
|
||||
for (const asset of PRECACHE_ASSETS) {
|
||||
try {
|
||||
await cache.add(asset);
|
||||
} catch (err) {
|
||||
console.warn(`[PWA SW] Precache failed for ${asset}:`, err);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then((keys) => {
|
||||
return Promise.all(
|
||||
keys.map((key) => {
|
||||
if (key !== CACHE_NAME) {
|
||||
return caches.delete(key);
|
||||
}
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const { request } = event;
|
||||
const url = new URL(request.url);
|
||||
|
||||
// Only handle GET requests
|
||||
if (request.method !== 'GET') return;
|
||||
|
||||
// Do not intercept browser extensions, HMR, or internal telemetry
|
||||
if (url.protocol !== 'http:' && url.protocol !== 'https:') return;
|
||||
if (url.pathname.includes('webpack-hmr')) return;
|
||||
|
||||
// Do not cache API requests or authentication endpoints
|
||||
if (
|
||||
url.pathname.startsWith('/api/') ||
|
||||
url.hostname.includes('api') ||
|
||||
url.pathname.startsWith('/auth')
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Navigation requests (HTML pages)
|
||||
if (request.mode === 'navigate') {
|
||||
event.respondWith(
|
||||
fetch(request)
|
||||
.then((response) => {
|
||||
// Only cache successful standard responses
|
||||
if (response && response.status === 200 && response.type === 'basic') {
|
||||
const responseToCache = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => {
|
||||
cache.put(request, responseToCache);
|
||||
});
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch(async () => {
|
||||
const cachedResponse = await caches.match(request);
|
||||
if (cachedResponse) {
|
||||
return cachedResponse;
|
||||
}
|
||||
const offlineFallback = await caches.match('/offline');
|
||||
if (offlineFallback) {
|
||||
return offlineFallback;
|
||||
}
|
||||
return new Response(
|
||||
`<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Offline - H.A.T.s</title>
|
||||
<style>
|
||||
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; background: #fafafa; color: #18181b; text-align: center; padding: 20px; }
|
||||
.card { max-width: 420px; background: white; border: 1px solid #e4e4e7; border-radius: 16px; padding: 32px 24px; box-shadow: 0 4px 12px rgba(0,0,0,0.05); }
|
||||
h1 { font-size: 20px; margin: 16px 0 8px; color: #09090b; }
|
||||
p { font-size: 14px; color: #71717a; margin-bottom: 24px; line-height: 1.5; }
|
||||
button { background: #EA512C; color: white; border: none; padding: 10px 20px; border-radius: 8px; font-weight: 500; cursor: pointer; font-size: 14px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<img src="/assets/logo192.png" width="64" height="64" alt="H.A.T.s" style="border-radius: 12px;" />
|
||||
<h1>You are currently offline</h1>
|
||||
<p>Please check your internet connection to continue using H.A.T.s.</p>
|
||||
<button onclick="window.location.reload()">Retry Connection</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>`,
|
||||
{ headers: { 'Content-Type': 'text/html; charset=utf-8' } }
|
||||
);
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Static assets (images, icons, fonts) - Cache First, Fallback to Network
|
||||
if (
|
||||
url.pathname.startsWith('/assets/') ||
|
||||
url.pathname.match(/\.(png|jpg|jpeg|svg|webp|gif|ico|woff|woff2|ttf|css)$/)
|
||||
) {
|
||||
event.respondWith(
|
||||
caches.match(request).then((cached) => {
|
||||
if (cached) return cached;
|
||||
return fetch(request).then((response) => {
|
||||
if (response && response.status === 200) {
|
||||
const clone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => {
|
||||
cache.put(request, clone);
|
||||
});
|
||||
}
|
||||
return response;
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user