fix(update): 别让坏包卡住更新入口

- restorePending 只认 success / pending 的包:插件把失败包也列在 list() 里,
  之前会被当成「已下载待应用」,每次启动就弹窗
- apply 失败时删除该包并清空待应用状态,不再卡在「已下载,重启生效」
- 版本更新页在应用失败后提示原因,用户可以直接重新检查更新
This commit is contained in:
Timi
2026-11-17 16:07:43 +08:00
parent 2fb5e93262
commit 02a41663a1
2 changed files with 18 additions and 2 deletions
+14 -2
View File
@@ -135,7 +135,16 @@ async function download(): Promise<BundleInfo | null> {
/** 立即应用并重启 */
async function apply(bundle: BundleInfo): Promise<void> {
await CapacitorUpdater.set({ id: bundle.id });
error.value = "";
try {
await CapacitorUpdater.set({ id: bundle.id });
} catch (e) {
// 包目录已被清理或本身就是坏的(例如把安装包当热更包下下来),
// set 会报 id 不存在。不清理的话每次启动都卡在「已下载,重启生效」
await CapacitorUpdater.delete({ id: bundle.id }).catch(() => undefined);
ready.value = null;
error.value = e instanceof Error ? e.message : "更新失败";
}
}
/** 排队到下次启动应用 */
@@ -143,6 +152,9 @@ async function defer(bundle: BundleInfo): Promise<void> {
await CapacitorUpdater.next({ id: bundle.id });
}
/** 可以应用的包状态:剩下的都是删除中或下载失败的残骸,set 会报 id 不存在 */
const USABLE_STATUS: Array<BundleInfo["status"]> = ["success", "pending"];
/** 查找已下载但尚未应用的版本 */
async function restorePending(): Promise<void> {
if (!supported) {
@@ -152,7 +164,7 @@ async function restorePending(): Promise<void> {
const [current, list] = await Promise.all([CapacitorUpdater.current(), CapacitorUpdater.list()]);
const currentId = current.bundle?.id;
const pending = list.bundles
.filter(bundle => bundle.id !== currentId)
.filter(bundle => bundle.id !== currentId && USABLE_STATUS.includes(bundle.status))
.sort((a, b) => Date.parse(b.downloaded) - Date.parse(a.downloaded))[0];
ready.value = pending ?? null;
} catch {