Skip to content

Commit 222187d

Browse files
Azir-11honghuangdc
authored andcommitted
optimize(hooks): update detection function to cover the exceptions that occur when the request fails.
1 parent 75455b0 commit 222187d

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/plugins/app.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function setupAppVersionNotification() {
2525

2626
const buildTime = await getHtmlBuildTime();
2727

28-
// If build time hasn't changed, no update is needed
29-
if (buildTime === BUILD_TIME) {
28+
// If failed to get build time or build time hasn't changed, no update is needed.
29+
if (!buildTime || buildTime === BUILD_TIME) {
3030
return;
3131
}
3232

@@ -88,16 +88,22 @@ export function setupAppVersionNotification() {
8888
}
8989
}
9090

91-
async function getHtmlBuildTime() {
91+
async function getHtmlBuildTime(): Promise<string | null> {
9292
const baseUrl = import.meta.env.VITE_BASE_URL || '/';
9393

94-
const res = await fetch(`${baseUrl}index.html?time=${Date.now()}`);
94+
try {
95+
const res = await fetch(`${baseUrl}index.html?time=${Date.now()}`);
9596

96-
const html = await res.text();
97-
98-
const match = html.match(/<meta name="buildTime" content="(.*)">/);
99-
100-
const buildTime = match?.[1] || '';
97+
if (!res.ok) {
98+
console.error('getHtmlBuildTime error:', res.status, res.statusText);
99+
return null;
100+
}
101101

102-
return buildTime;
102+
const html = await res.text();
103+
const match = html.match(/<meta name="buildTime" content="(.*)">/);
104+
return match?.[1] || null;
105+
} catch (error) {
106+
console.error('getHtmlBuildTime error:', error);
107+
return null;
108+
}
103109
}

0 commit comments

Comments
 (0)