Skip to content

Commit

Permalink
fix(版本更新): 调整插件配置
Browse files Browse the repository at this point in the history
  • Loading branch information
hesetiema committed May 25, 2024
1 parent 5666026 commit d058ad5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
6 changes: 4 additions & 2 deletions plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PluginOption } from 'vite'
import fs from 'fs'
import path from 'path'

export default function vitePluginUpdateVersion(): PluginOption {
let config: any, lastBuildTime: string
Expand All @@ -15,9 +16,10 @@ export default function vitePluginUpdateVersion(): PluginOption {
config = resolvedConfig
},

buildEnd() {
buildStart() {
lastBuildTime = new Date().toLocaleString()
fs.writeFileSync('public/version.json', JSON.stringify({ lastBuildTime }, null, 2))
const filePath = config.publicDir + path.sep + 'version.json'
fs.writeFileSync(filePath, JSON.stringify({ lastBuildTime }, null, 2))
},

transformIndexHtml(html) {
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}

window.addEventListener('vite:preloadError', () => {
window.location.reload() // for example, refresh the page
})

app.use(router)
app.use(ElementPlus)
app.use(pinia)
Expand Down
22 changes: 1 addition & 21 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const router = createRouter({
})

const checkUpdate = async () => {
return fetch(`/version.json?t=${Date.now()}`)
return fetch(`version.json?t=${Date.now()}`)
.then((response) => {
if (!response.ok) throw new Error(`Failed to fetch version.json`)

Expand Down Expand Up @@ -208,24 +208,4 @@ router.beforeEach((to, from, next) => {
}
})

window.addEventListener(
'error',
function (e) {
if (e.target) {
checkUpdate()
}
},
true
)

window.addEventListener(
'unhandledrejection',
function (e) {
if (e.target) {
checkUpdate()
}
},
true
)

export default router

0 comments on commit d058ad5

Please sign in to comment.