feat: 更新 Service Worker 版本管理,优化缓存策略,增加未处理更新的提示
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { fileURLToPath, URL } from 'node:url'
|
||||
import { writeFileSync, readFileSync, existsSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
@@ -9,6 +11,20 @@ export default defineConfig({
|
||||
plugins: [
|
||||
vue(),
|
||||
vueDevTools(),
|
||||
{
|
||||
name: 'update-sw-version',
|
||||
apply: 'build',
|
||||
closeBundle() {
|
||||
const swPath = resolve(fileURLToPath(new URL('.', import.meta.url)), 'dist/service-worker.js')
|
||||
if (existsSync(swPath)) {
|
||||
let content = readFileSync(swPath, 'utf8')
|
||||
const timestamp = new Date().toISOString()
|
||||
content = content.replace(/const VERSION = '.*';/, `const VERSION = '${timestamp}';`)
|
||||
writeFileSync(swPath, content)
|
||||
console.log(`[SW Update] Version updated to ${timestamp}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
resolve: {
|
||||
alias: {
|
||||
|
||||
Reference in New Issue
Block a user