优化android 热更新

This commit is contained in:
biss committed 2026-08-15 15:53:33 +08:00
1 parent 75d8f87f49
commit 999723508a
4 files changed
+74 -18

No files matched your search

+39 -7
View File
@@ -18,6 +18,10 @@ 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')
@@ -30,14 +34,14 @@ function absoluteApiUrl(relativePath: string) {
return new URL(relativePath, apiBase).toString()
}
export async function initializeAppUpdates() {
if (!Capacitor.isNativePlatform()) return
async function checkForAppUpdate() {
if (checkInProgress) return checkInProgress
if (!navigator.onLine || Date.now() - lastCheckedAt < foregroundCheckInterval) {
return
}
checkInProgress = (async () => {
try {
// Only confirm the bundle after Vue has mounted successfully. If a newly
// installed bundle cannot reach this point, the native plugin rolls back.
await CapacitorUpdater.notifyAppReady()
const platform = Capacitor.getPlatform()
if (platform !== 'android' && platform !== 'ios') return
@@ -68,6 +72,7 @@ export async function initializeAppUpdates() {
!data.version ||
!data.downloadUrl ||
!data.sha256 ||
data.version === currentVersion ||
next?.version === data.version
) {
return
@@ -82,12 +87,39 @@ export async function initializeAppUpdates() {
ElNotification({
title: `新版本 ${data.version} 已就绪`,
message: data.releaseNotes || '更新将在下次启动应用时自动生效。',
message:
data.releaseNotes || '切换到后台或下次启动应用时将自动启用更新。',
type: 'success',
duration: 6000,
})
} catch (error) {
// 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
}
})()
return checkInProgress
}
export async function initializeAppUpdates() {
if (!Capacitor.isNativePlatform()) return
try {
// Confirm the bundle before any network request. If a newly installed
// bundle cannot reach this point, the native plugin rolls it back.
await CapacitorUpdater.notifyAppReady()
} catch (error) {
// Do not let a native-plugin error prevent the bundled app from starting.
console.warn('[app-update] bundle readiness confirmation failed', error)
}
void checkForAppUpdate()
window.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
void checkForAppUpdate()
}
})
}