前端热更新

This commit is contained in:
biss committed 2026-08-21 22:02:33 +08:00
1 parent af88732a07
commit 5b95fe8f40
3 files changed
+19 -16

No files matched your search

+7 -11
View File
@@ -18,10 +18,8 @@ interface AppUpdateCheckResponse {
const updateChannel =
import.meta.env.VITE_APP_UPDATE_CHANNEL?.trim() || 'production'
const foregroundCheckInterval = 5 * 60 * 1000
let checkInProgress: Promise<void> | undefined
let lastCheckedAt = 0
function absoluteApiUrl(relativePath: string) {
const configuredBase = String(http.defaults.baseURL ?? '/api')
@@ -36,9 +34,7 @@ function absoluteApiUrl(relativePath: string) {
async function checkForAppUpdate() {
if (checkInProgress) return checkInProgress
if (!navigator.onLine || Date.now() - lastCheckedAt < foregroundCheckInterval) {
return
}
if (!navigator.onLine) return
checkInProgress = (async () => {
try {
@@ -64,6 +60,9 @@ async function checkForAppUpdate() {
nativeVersion: current.native,
currentVersion,
},
// Update discovery is deliberately public. Do not delay startup for a
// token refresh or send a stale Authorization header to this endpoint.
skipAuthSession: true,
timeout: 15000,
},
)
@@ -96,7 +95,6 @@ async function checkForAppUpdate() {
// OTA failure must never stop users from entering the bundled application.
console.warn('[app-update] update check failed', error)
} finally {
lastCheckedAt = Date.now()
checkInProgress = undefined
}
})()
@@ -116,10 +114,8 @@ export async function initializeAppUpdates() {
console.warn('[app-update] bundle readiness confirmation failed', error)
}
// Every cold start checks the latest public release immediately. A bundle
// downloaded here is activated when the app is next launched.
void checkForAppUpdate()
window.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
void checkForAppUpdate()
}
})
window.addEventListener('online', () => void checkForAppUpdate())
}