This commit is contained in:
biss committed 2026-08-15 16:11:45 +08:00
1 parent 999723508a
commit 18151fda81
7 files changed
+4958 -2

No files matched your search

+2
View File
@@ -9,6 +9,7 @@ import App from './App.vue'
import router from './router'
import { initializeAppUpdates } from './services/appUpdates'
import { initializeNativeHome } from './services/nativeHome'
import { initializePwa } from './services/pwa'
import { setRouter } from './utils/navigate'
import { initializeAuthSession } from './auth/session'
@@ -22,5 +23,6 @@ window.addEventListener('mingxu-session-expired', () => {
})
initializeAuthSession()
app.mount('#app')
void initializePwa()
void initializeAppUpdates()
initializeNativeHome(router)
+71
View File
@@ -0,0 +1,71 @@
import { Capacitor } from '@capacitor/core'
import { ElMessageBox } from 'element-plus'
let updatePromptVisible = false
function serviceWorkerUrl() {
return new URL('sw.js', import.meta.env.BASE_URL).toString()
}
async function promptToRefresh(registration: ServiceWorkerRegistration) {
const waitingWorker = registration.waiting
if (!waitingWorker || updatePromptVisible) return
updatePromptVisible = true
try {
await ElMessageBox.confirm(
'已有新版本准备完成。刷新后将加载新版本,当前未保存的内容请先提交。',
'发现应用更新',
{
confirmButtonText: '立即刷新',
cancelButtonText: '稍后',
type: 'info',
},
)
waitingWorker.postMessage({ type: 'SKIP_WAITING' })
} catch {
// Keeping the current version is an intentional, safe user choice.
} finally {
updatePromptVisible = false
}
}
export async function initializePwa() {
if (
Capacitor.isNativePlatform() ||
!('serviceWorker' in navigator) ||
!window.isSecureContext
) {
return
}
try {
const registration = await navigator.serviceWorker.register(serviceWorkerUrl())
let refreshing = false
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (refreshing) return
refreshing = true
window.location.reload()
})
const watchForUpdate = () => {
const worker = registration.installing
if (!worker) return
worker.addEventListener('statechange', () => {
if (worker.state === 'installed') {
void promptToRefresh(registration)
}
})
}
registration.addEventListener('updatefound', watchForUpdate)
watchForUpdate()
// Browsers can otherwise throttle routine update checks; a normal app
// launch should still discover a newly deployed service worker promptly.
await registration.update()
await promptToRefresh(registration)
} catch (error) {
// PWA installation and caching are progressive enhancements.
console.warn('[pwa] service worker registration failed', error)
}
}