From 16c084ba800861c2c727b00f143f29b405565182 Mon Sep 17 00:00:00 2001 From: DDSRem <1448139087@qq.com> Date: Wed, 11 Mar 2026 15:12:42 +0800 Subject: [PATCH] fix(plugin): build remote entry URL with origin+pathname to fix subpath proxy 404 - Use pathBase (pathname) when building remoteEntry URL so it matches API request base - Fixes plugin static assets 404 when app is under subpath (e.g. /mp/) Made-with: Cursor --- src/utils/federationLoader.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils/federationLoader.ts b/src/utils/federationLoader.ts index bbf0293c..7e7fb6c1 100644 --- a/src/utils/federationLoader.ts +++ b/src/utils/federationLoader.ts @@ -80,9 +80,9 @@ async function fetchRemoteModules(): Promise { * @param modules 远程模块列表 */ function injectRemoteModule(module: RemoteModule): void { - // 从浏览器地址栏获取当前地址前缀 + // 与 API 请求一致:使用 origin + pathname 作为前缀,子路径代理时 pathname 含 /mp 等 const baseUrl = new URL(window.location.href) - // 环境变量 + const pathBase = baseUrl.pathname.replace(/\/$/, '') || '' let apiBase = import.meta.env.VITE_API_BASE_URL if (apiBase.startsWith('/')) { apiBase = apiBase.slice(1) @@ -90,8 +90,10 @@ function injectRemoteModule(module: RemoteModule): void { if (apiBase.endsWith('/')) { apiBase = apiBase.slice(0, -1) } + const pathWithoutLeadingSlash = module.url.startsWith('/') ? module.url.slice(1) : module.url + const remoteEntryUrl = `${baseUrl.origin}${pathBase}/${apiBase}/${pathWithoutLeadingSlash}` __federation_method_setRemote(module.id, { - url: () => Promise.resolve(`${baseUrl.origin}/${apiBase}${module.url}`), + url: () => Promise.resolve(remoteEntryUrl), format: 'esm', from: 'vite', })