feat(web): support deployment under a configurable base path#576
Open
Phil-OSophy-42 wants to merge 1 commit into
Open
feat(web): support deployment under a configurable base path#576Phil-OSophy-42 wants to merge 1 commit into
Phil-OSophy-42 wants to merge 1 commit into
Conversation
支持将 Web 应用部署在反向代理的子路径下(如 /skillhub/),不再局限于 域名根目录。构建期由 VITE_BASE_PATH 驱动 Vite base;镜像默认以占位符 /__SKILLHUB_WEB_BASE_PATH__/ 构建,entrypoint 启动时按 SKILLHUB_WEB_BASE_PATH 环境变量替换占位符(默认 /),同一镜像可通过 环境变量部署在任意子路径,无需重建。 - vite.config.ts: base 读取 VITE_BASE_PATH,默认 / - router.tsx: basepath 取自 import.meta.env.BASE_URL - base-path.ts: 新增 withBasePath(),为绕过路由的整页跳转与动态资源 地址补前缀;绝对 URL 原样放行 - api-error.ts / user-menu.tsx / login-button.tsx: 硬编码跳转与 provider 图标地址改经 withBasePath() - client.ts: logout 改经 buildApiUrl(),修复子路径下注销请求打到域名 根导致会话无法清除的问题 - use-notification-sse.ts: EventSource URL 改经 buildApiUrl() - Dockerfile / 20-base-path.sh: 占位符构建 + 启动时替换 - compose.release.yml / deploy/k8s / .env.release.example: 暴露 SKILLHUB_WEB_BASE_PATH 默认行为不变:根路径部署(SKILLHUB_WEB_BASE_PATH=/ 或未设置)与改动 前逐字节等价。OAuth 回调地址由后端 forward-headers-strategy 处理, 属部署配置,不在本次改动内。 Signed-off-by: philsun <xinyi.sun@daocloud.io>
Author
|
@dongmucat Hi! please take a took |
Author
|
/cc @dongmucat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
支持将 Web 应用部署在反向代理的子路径下(例如
https://example.com/skillhub/),不再局限于域名根目录。默认行为完全不变:根路径部署与改动前逐字节等价。
背景
当 SkillHub 与其他应用共用一个域名、部署在网关之后时,需要挂在自己的路径前缀下。
目前构建产物将根路径
/写死,资源 URL、前端路由和少数硬编码跳转在子路径下全部失效。实现
两层设计:
构建期:
VITE_BASE_PATH驱动 Vitebase,其余位置统一读import.meta.env.BASE_URL(router
basepath、withBasePath()跳转辅助),一处配置保持一致。运行时(单镜像多路径):镜像默认以占位符
/__SKILLHUB_WEB_BASE_PATH__/构建,entrypoint(
20-base-path.sh)启动时将占位符替换为SKILLHUB_WEB_BASE_PATH环境变量的值(默认
/)。同一镜像通过环境变量即可部署在任意子路径,无需重建:如需构建期烤死固定 base(跳过运行时替换):
--build-arg VITE_BASE_PATH=/skillhub/。改动清单
vite.config.ts—base: process.env.VITE_BASE_PATH ?? '/'src/app/router.tsx—basepath: import.meta.env.BASE_URLsrc/shared/lib/base-path.ts(新增)—withBasePath(),为绕过路由的整页跳转与动态资源地址补前缀,绝对 URL 原样放行
api-error.ts/user-menu.tsx/login-button.tsx— 硬编码的/login、/跳转及 provider 图标地址改经
withBasePath()api/client.ts— 顺带修复:logout原先用原始fetch('/api/v1/auth/logout')绕过
apiBaseUrl,子路径下请求打到域名根、会话无法清除;改经buildApiUrl()use-notification-sse.ts—EventSourceURL 改经buildApiUrl(),与其他 API 一致src/vite-env.d.ts(新增)—vite/client类型Dockerfile/docker-entrypoint.d/20-base-path.sh— 占位符构建 + 启动替换compose.release.yml/deploy/k8s/base/frontend-deployment.yaml/.env.release.example— 暴露SKILLHUB_WEB_BASE_PATHAPI 请求前缀沿用已有的运行时
apiBaseUrl(SKILLHUB_WEB_API_BASE_URL),无需新机制。测试
pnpm vitest run— 609 个用例全部通过index.html引用/assets/...,与原先一致(向后兼容)VITE_BASE_PATH=/skillhub/ pnpm build:产物引用/skillhub/assets/.../(根);env=/skillhub/→/skillhub/assets/...,零占位符残留/skillhub/部署验证:页面加载、客户端路由深链/刷新、API 调用、登录/注销均正常