mirror of
https://github.com/Buriburizaem0n/nezha-dash-v1.git
synced 2026-02-04 20:50:08 +00:00
fix: pwa cache
This commit is contained in:
52
src/components/ReloadPrompt.tsx
Normal file
52
src/components/ReloadPrompt.tsx
Normal file
@@ -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;
|
||||
Reference in New Issue
Block a user