fix: pwa cache

This commit is contained in:
hamster1963
2024-12-05 10:20:45 +08:00
parent 4032ff62a2
commit 9073fa6429
8 changed files with 139 additions and 3 deletions

View 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;