From 5d9a9f3e2c841c6cbeb2f6713100c628eb015e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E8=8F=9C=E7=99=BD=E7=8E=89=E6=B1=A4?= <79054161+Azir-11@users.noreply.github.com> Date: Wed, 7 May 2025 21:09:33 +0800 Subject: [PATCH] optimize(hooks): update detection function to cover the exceptions that occur when the request fails. fixed [#90] --- src/plugins/app.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/plugins/app.ts b/src/plugins/app.ts index 36f4873..ed02dbc 100644 --- a/src/plugins/app.ts +++ b/src/plugins/app.ts @@ -26,8 +26,8 @@ export function setupAppVersionNotification() { const buildTime = await getHtmlBuildTime(); - // If build time hasn't changed, no update is needed - if (buildTime === BUILD_TIME) { + // If failed to get build time or build time hasn't changed, no update is needed + if (!buildTime || buildTime === BUILD_TIME) { return; } @@ -83,16 +83,22 @@ export function setupAppVersionNotification() { } } -async function getHtmlBuildTime() { +async function getHtmlBuildTime(): Promise { const baseUrl = import.meta.env.VITE_BASE_URL || '/'; - const res = await fetch(`${baseUrl}index.html?time=${Date.now()}`); + try { + const res = await fetch(`${baseUrl}index.html?time=${Date.now()}`); - const html = await res.text(); + if (!res.ok) { + console.error('getHtmlBuildTime error:', res.status, res.statusText); + return null; + } - const match = html.match(//); - - const buildTime = match?.[1] || ''; - - return buildTime; + const html = await res.text(); + const match = html.match(//); + return match?.[1] || null; + } catch (error) { + console.error('getHtmlBuildTime error:', error); + return null; + } }