diff --git a/src/lib/__tests__/dweb-update.test.ts b/src/lib/__tests__/dweb-update.test.ts index 011d0b206..008bed1b9 100644 --- a/src/lib/__tests__/dweb-update.test.ts +++ b/src/lib/__tests__/dweb-update.test.ts @@ -9,6 +9,7 @@ type FetchResponse = { function stubGlobals() { vi.stubGlobal('__KEYAPP_SITE_ORIGIN__', 'https://example.com/KeyApp/') + vi.stubGlobal('__KEYAPP_BASE_URL__', './') vi.stubGlobal('__DEV_MODE__', false) vi.stubGlobal('__APP_VERSION__', '1.2.3') Object.defineProperty(window, '__native_close_watcher_kit__', { @@ -33,6 +34,13 @@ describe('dweb update', () => { expect(url).toBe('https://example.com/KeyApp/dweb-dev/metadata.json') }) + it('resolves metadata url with base url when origin has no path', () => { + vi.stubGlobal('__KEYAPP_SITE_ORIGIN__', 'https://example.com') + vi.stubGlobal('__KEYAPP_BASE_URL__', '/KeyApp/') + const url = resolveDwebMetadataUrl() + expect(url).toBe('https://example.com/KeyApp/dweb/metadata.json') + }) + it('builds install url', () => { const url = resolveInstallUrl('https://example.com/KeyApp/dweb/metadata.json') expect(url).toBe('dweb://install?url=https%3A%2F%2Fexample.com%2FKeyApp%2Fdweb%2Fmetadata.json') diff --git a/src/lib/dweb-update.ts b/src/lib/dweb-update.ts index 4f9c43e46..153ea6f8d 100644 --- a/src/lib/dweb-update.ts +++ b/src/lib/dweb-update.ts @@ -68,8 +68,29 @@ function normalizeOrigin(origin: string): string { return origin.endsWith('/') ? origin : `${origin}/` } +function normalizeBasePath(basePath: string): string { + if (!basePath) return './' + return basePath.endsWith('/') ? basePath : `${basePath}/` +} + +function resolveSiteBase(): string { + const origin = normalizeOrigin(__KEYAPP_SITE_ORIGIN__) + try { + const originUrl = new URL(origin) + const originPath = originUrl.pathname + if (originPath && originPath !== '/') { + return normalizeOrigin(origin) + } + } catch { + return origin + } + + const basePath = normalizeBasePath(__KEYAPP_BASE_URL__) + return normalizeOrigin(new URL(basePath, origin).toString()) +} + export function resolveDwebMetadataUrl(): string { - const base = normalizeOrigin(__KEYAPP_SITE_ORIGIN__) + const base = resolveSiteBase() const path = __DEV_MODE__ ? 'dweb-dev/metadata.json' : 'dweb/metadata.json' return new URL(path, base).toString() } diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index b096129b1..1cf4f5ed0 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -14,6 +14,9 @@ declare global { /** KeyApp 官网 Origin - 通过 vite.config.ts define 配置 */ const __KEYAPP_SITE_ORIGIN__: string + /** KeyApp Base URL - 通过 vite.config.ts define 配置 */ + const __KEYAPP_BASE_URL__: string + /** 默认生态源列表 - 通过 vite.config.ts define 配置 */ const __ECOSYSTEM_SOURCES__: Array<{ name: string; url: string; icon?: string }> diff --git a/vite.config.ts b/vite.config.ts index 0f52c2f19..8e1f72f37 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -209,6 +209,8 @@ export default defineConfig(({ mode }) => { __APP_VERSION__: JSON.stringify(appVersion), // KeyApp 官网 Origin(DWEB 升级检查使用) __KEYAPP_SITE_ORIGIN__: JSON.stringify(siteOrigin), + // KeyApp Base URL(DWEB 升级链接拼接使用) + __KEYAPP_BASE_URL__: JSON.stringify(BASE_URL), // 默认生态源列表(用于订阅源管理展示) __ECOSYSTEM_SOURCES__: JSON.stringify(ecosystemSources), // API Keys 对象(用于动态读取环境变量)