From 7a5c30998e7afa1e58556c038fd776dfaee06544 Mon Sep 17 00:00:00 2001 From: hudy9x <95471659+hudy9x@users.noreply.github.com> Date: Thu, 18 Jul 2024 17:09:22 +0700 Subject: [PATCH] feat(cache): cache resources (project pages, css, font, js, image) using service worker (#239) --- .../app/[orgName]/meeting/MeetingRoomList.tsx | 2 +- .../app/_features/ServiceWorker/index.tsx | 13 ++ packages/ui-app/app/layout.tsx | 2 + packages/ui-app/public/sw-cache-resources.js | 169 ++++++++++++++++++ 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 packages/ui-app/app/_features/ServiceWorker/index.tsx create mode 100644 packages/ui-app/public/sw-cache-resources.js diff --git a/packages/ui-app/app/[orgName]/meeting/MeetingRoomList.tsx b/packages/ui-app/app/[orgName]/meeting/MeetingRoomList.tsx index 78865e79..8911a304 100644 --- a/packages/ui-app/app/[orgName]/meeting/MeetingRoomList.tsx +++ b/packages/ui-app/app/[orgName]/meeting/MeetingRoomList.tsx @@ -48,7 +48,7 @@ export default function MeetingRoomList() { // only clear fixed loading as the page unmount useEffect(() => { return () => { - setFixLoading(false) + // setFixLoading(false) } }) diff --git a/packages/ui-app/app/_features/ServiceWorker/index.tsx b/packages/ui-app/app/_features/ServiceWorker/index.tsx new file mode 100644 index 00000000..e7b8eb9e --- /dev/null +++ b/packages/ui-app/app/_features/ServiceWorker/index.tsx @@ -0,0 +1,13 @@ +'use client' +import { useEffect } from "react" + +export default function RegisterServiceWorker() { + useEffect(() => { + if ('serviceWorker' in window.navigator) { + window.navigator.serviceWorker + .register('/sw-cache-resources.js') + .then(registration => console.log('Scope is:', registration, registration.scope)) + } + }) + return <>> +} diff --git a/packages/ui-app/app/layout.tsx b/packages/ui-app/app/layout.tsx index 1dd835b6..99fab0f4 100644 --- a/packages/ui-app/app/layout.tsx +++ b/packages/ui-app/app/layout.tsx @@ -7,6 +7,7 @@ import RootLayoutComp from '../layouts/RootLayout' import { GoalieProvider } from '@goalie/nextjs' import dynamic from 'next/dynamic' +import RegisterServiceWorker from './_features/ServiceWorker' const inter = Inter({ subsets: ['latin'] }) const PushNotification = dynamic( @@ -33,6 +34,7 @@ export default function RootLayout({