feat: 添加 PWA 更新组件,优化缓存策略和动态路由

This commit is contained in:
xiongxiao
2026-03-21 00:42:15 +08:00
committed by cnb
parent 02925f69f4
commit 9d4b3f013a
5 changed files with 112 additions and 2 deletions

View File

@@ -0,0 +1,60 @@
import { useState } from 'react';
import { useRegisterSW } from 'virtual:pwa-register/react';
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from '@/components/ui/card';
function PWAUpdate() {
const {
needRefresh: [needRefresh, setNeedRefresh],
updateServiceWorker,
} = useRegisterSW({
onNeedRefresh() {
setNeedRefresh(true);
},
});
const [isLoading, setIsLoading] = useState(false);
const handleUpdate = async () => {
setIsLoading(true);
await updateServiceWorker(true);
setIsLoading(false);
};
const handleDismiss = () => {
setNeedRefresh(false);
};
if (!needRefresh) {
return null;
}
return (
<div className="fixed bottom-4 right-4 z-50">
<Card className="w-80 shadow-lg">
<CardHeader className="pb-3">
<CardTitle></CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent className="pt-0" />
<CardFooter className="gap-2">
<Button variant="outline" size="sm" onClick={handleDismiss}>
</Button>
<Button size="sm" onClick={handleUpdate} disabled={isLoading}>
{isLoading ? '更新中...' : '立即更新'}
</Button>
</CardFooter>
</Card>
</div>
);
}
export default PWAUpdate;