fix: url prefix page redirect

This commit is contained in:
engigu
2026-01-13 22:03:21 +08:00
parent 44c8db491d
commit bc87184f84
3 changed files with 8 additions and 17 deletions
+3 -4
View File
@@ -246,10 +246,9 @@ func Setup(c *Controllers) *gin.Engine {
html := string(data)
// 在第一个 script 标签中注入配置
// 这样可以确保在资源加载前就设置好 base 标签
configScript := `window.__BASE_URL__ = "` + urlPrefix + `"; window.__API_VERSION__ = "/api/v1";`
html = strings.Replace(html, "// 后端会在这里注入:", configScript + "\n //", 1)
// 注入配置变量供前端使用(API 调用和路由)
configScript := `<script>window.__BASE_URL__ = "` + urlPrefix + `"; window.__API_VERSION__ = "/api/v1";</script>`
html = strings.Replace(html, "</head>", configScript+"</head>", 1)
ctx.Header("Cache-Control", "no-cache, no-store, must-revalidate")
ctx.Data(200, "text/html; charset=utf-8", []byte(html))
+2 -12
View File
@@ -2,22 +2,12 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<script>
// 这个脚本会被后端注入 __BASE_URL__,必须在所有资源加载前执行
// 后端会在这里注入: window.__BASE_URL__ = "/baihu";
</script>
<script>
// 在任何资源加载前设置 base 标签
if (window.__BASE_URL__) {
document.write('<base href="' + window.__BASE_URL__ + '/">');
}
</script>
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<link rel="icon" type="image/svg+xml" href="./logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
<script type="module" src="./src/main.ts"></script>
</body>
</html>
+3 -1
View File
@@ -19,5 +19,7 @@ export default defineConfig({
}
}
},
base: '/'
// 使用相对路径,这样动态导入的模块也会使用相对路径
// 浏览器会根据当前页面 URL 解析相对路径
base: './'
})