diff --git a/apps/web-dashboard/public/offline.html b/apps/web-dashboard/public/offline.html new file mode 100644 index 0000000..0e28394 --- /dev/null +++ b/apps/web-dashboard/public/offline.html @@ -0,0 +1,59 @@ + + + + + + Offline - Telecom Dashboard + + + +
+

You're Offline

+

Check your internet connection and try again.

+ +
+ + diff --git a/apps/web-dashboard/public/sw.js b/apps/web-dashboard/public/sw.js new file mode 100644 index 0000000..4e867b8 --- /dev/null +++ b/apps/web-dashboard/public/sw.js @@ -0,0 +1,51 @@ +const CACHE_NAME = 'telecom-dashboard-v1'; +const urlsToCache = [ + '/', + '/login', + '/dashboard', + '/services', + '/monitoring', + '/deploy', + '/plugins', + '/automation', + '/chaos', + '/billing', + '/config', +]; + +self.addEventListener('install', (event) => { + event.waitUntil( + caches.open(CACHE_NAME) + .then((cache) => { + return cache.addAll(urlsToCache); + }) + ); +}); + +self.addEventListener('fetch', (event) => { + event.respondWith( + caches.match(event.request) + .then((response) => { + if (response) { + return response; + } + return fetch(event.request).catch(() => { + return caches.match('/offline.html'); + }); + }) + ); +}); + +self.addEventListener('activate', (event) => { + event.waitUntil( + caches.keys().then((cacheNames) => { + return Promise.all( + cacheNames.map((cacheName) => { + if (cacheName !== CACHE_NAME) { + return caches.delete(cacheName); + } + }) + ); + }) + ); +}); diff --git a/apps/web-dashboard/src/app/layout.tsx b/apps/web-dashboard/src/app/layout.tsx index 01dcac4..e14b555 100644 --- a/apps/web-dashboard/src/app/layout.tsx +++ b/apps/web-dashboard/src/app/layout.tsx @@ -4,6 +4,7 @@ import "./globals.css"; import { cn } from "@/lib/utils"; import { Sidebar } from "@/components/sidebar"; import { Providers } from "./providers"; +import { ServiceWorkerRegistration } from "@/components/service-worker-registration"; const inter = Inter({ subsets: ["latin"], variable: "--font-sans" }); @@ -34,6 +35,7 @@ export default function RootLayout({ > +
{children}
diff --git a/apps/web-dashboard/src/components/service-worker-registration.tsx b/apps/web-dashboard/src/components/service-worker-registration.tsx new file mode 100644 index 0000000..259682b --- /dev/null +++ b/apps/web-dashboard/src/components/service-worker-registration.tsx @@ -0,0 +1,22 @@ +"use client"; + +import { useEffect } from "react"; + +export function ServiceWorkerRegistration() { + useEffect(() => { + if ("serviceWorker" in navigator && process.env.NEXT_PUBLIC_ENABLE_SW === "true") { + window.addEventListener("load", () => { + navigator.serviceWorker + .register("/sw.js") + .then((registration) => { + console.log("Service Worker registered: ", registration); + }) + .catch((registrationError) => { + console.log("Service Worker registration failed: ", registrationError); + }); + }); + } + }, []); + + return null; +}