Skip to content

Commit

Permalink
fix: pwa cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Dec 5, 2024
1 parent 4032ff6 commit 9073fa6
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nazha-dashboard-vite",
"private": true,
"version": "0.0.0",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -45,7 +45,7 @@
"devDependencies": {
"@eslint/js": "^9.16.0",
"@types/node": "^22.10.1",
"@types/react": "^18.3.12",
"@types/react": "^18.3.13",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.7.2",
"autoprefixer": "^10.4.20",
Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Server from "./pages/Server";
import ServerDetail from "./pages/ServerDetail";
import NotFound from "./pages/NotFound";
import ErrorPage from "./pages/ErrorPage";
import ReloadPrompt from "./components/ReloadPrompt";

const App: React.FC = () => {
return (
Expand All @@ -20,6 +21,7 @@ const App: React.FC = () => {
<Route path="*" element={<NotFound />} />
</Routes>
<Footer />
<ReloadPrompt />
</main>
</div>
</Router>
Expand Down
52 changes: 52 additions & 0 deletions src/components/ReloadPrompt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useRegisterSW } from 'virtual:pwa-register/react';
import { useTranslation } from 'react-i18next';
import { toast } from 'sonner';

function ReloadPrompt() {
const { t } = useTranslation();

const {
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker,
} = useRegisterSW({
onRegisteredSW(swUrl) {
console.log(`SW Registered: ${swUrl} (Version: ${import.meta.env.VITE_APP_VERSION})`);
},
onRegisterError(error) {
console.log('SW registration error', error);
},
onOfflineReady() {
toast.success(t('pwa.offlineReady'));
},
});

const close = () => {
setNeedRefresh(false);
};

const update = () => {
updateServiceWorker(true);
};

if (!needRefresh) {
return null;
}



toast.message(
`${t('pwa.newContent')} (${import.meta.env.VITE_APP_VERSION})`,
{
action: {
label: t('pwa.reload'),
onClick: () => update(),
},
onDismiss: close,
duration: Infinity,
}
);

return null;
}

export default ReloadPrompt;
5 changes: 5 additions & 0 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,10 @@
"noData": "No server monitor data",
"avgDelay": "Latency",
"monitorCount": "Services"
},
"pwa": {
"offlineReady": "App ready to work offline",
"newContent": "New content available",
"reload": "Update"
}
}
5 changes: 5 additions & 0 deletions src/locales/zh-CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,10 @@
"noData": "没有服务器监控数据",
"avgDelay": "延迟",
"monitorCount": "个监控服务"
},
"pwa": {
"offlineReady": "应用可以离线使用了",
"newContent": "发现新版本",
"reload": "更新"
}
}
21 changes: 21 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
/// <reference types="vite/client" />

declare module 'virtual:pwa-register/react' {
import type { Dispatch, SetStateAction } from 'react'

export interface RegisterSWOptions {
immediate?: boolean
onNeedRefresh?: () => void
onOfflineReady?: () => void
onRegistered?: (registration: ServiceWorkerRegistration | undefined) => void
onRegisteredSW?: (swScriptUrl: string, registration: ServiceWorkerRegistration | undefined) => void
onRegisterError?: (error) => void
}

export interface RegisterSWHook {
needRefresh: [boolean, Dispatch<SetStateAction<boolean>>]
offlineReady: [boolean, Dispatch<SetStateAction<boolean>>]
updateServiceWorker: (reloadPage?: boolean) => Promise<void>
}

export function useRegisterSW(options?: RegisterSWOptions): RegisterSWHook
}
53 changes: 52 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineConfig({
base: "/",
define: {
"import.meta.env.VITE_GIT_HASH": JSON.stringify(getGitHash()),
"import.meta.env.VITE_APP_VERSION": JSON.stringify(process.env.npm_package_version || "1.0.0"),
},
plugins: [
react(),
Expand Down Expand Up @@ -50,8 +51,58 @@ export default defineConfig({
],
},
workbox: {
navigateFallbackDenylist: [/^\/dashboard/],
clientsClaim: true,
skipWaiting: true,
cleanupOutdatedCaches: true,
runtimeCaching: [
{
urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-cache',
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24 * 365 // <== 365 days
},
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern: /^https:\/\/fonts\.gstatic\.com\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'gstatic-fonts-cache',
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24 * 365 // <== 365 days
},
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern: /^https:\/\/api\.nezha\.dev\/.*/i,
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 5 // <== 5 minutes
},
cacheableResponse: {
statuses: [0, 200]
}
}
}
]
},
devOptions: {
enabled: true,
type: 'module'
}
}),
],
resolve: {
Expand Down

0 comments on commit 9073fa6

Please sign in to comment.