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

@@ -33,6 +33,38 @@ export default defineConfig({
VitePWA({
injectRegister: 'auto',
registerType: 'autoUpdate',
// Workbox 缓存策略配置
workbox: {
// API 请求使用网络优先策略,确保获取最新数据
runtimeCaching: [
{
urlPattern: /^https?.*\/api\/.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24, // 24小时
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 静态资源使用缓存优先,但设置较短过期时间
{
urlPattern: /^https?.*\.(js|css|woff2?|png|jpg|jpeg|svg|gif|ico)/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'static-resources',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 7, // 7天
},
},
},
],
},
}),
],
resolve: {