fix: url prefix page redirect
This commit is contained in:
@@ -83,9 +83,19 @@ func Setup(c *Controllers) *gin.Engine {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注入 base URL 配置到 HTML
|
|
||||||
// 前端使用 urlPrefix,后端 API 使用 urlPrefix + /api/v1
|
|
||||||
html := string(data)
|
html := string(data)
|
||||||
|
|
||||||
|
// 如果配置了 URL 前缀,需要将绝对路径资源添加前缀
|
||||||
|
if urlPrefix != "" {
|
||||||
|
// 替换 /assets/ 为 /prefix/assets/
|
||||||
|
html = strings.ReplaceAll(html, `"/assets/`, `"`+urlPrefix+`/assets/`)
|
||||||
|
html = strings.ReplaceAll(html, `'/assets/`, `'`+urlPrefix+`/assets/`)
|
||||||
|
// 替换 /logo.svg 为 /prefix/logo.svg
|
||||||
|
html = strings.ReplaceAll(html, `"/logo.svg"`, `"`+urlPrefix+`/logo.svg"`)
|
||||||
|
html = strings.ReplaceAll(html, `'/logo.svg'`, `'`+urlPrefix+`/logo.svg'`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注入 base URL 配置到 HTML
|
||||||
configScript := `<script>window.__BASE_URL__ = "` + urlPrefix + `"; window.__API_VERSION__ = "/api/v1";</script>`
|
configScript := `<script>window.__BASE_URL__ = "` + urlPrefix + `"; window.__API_VERSION__ = "/api/v1";</script>`
|
||||||
html = strings.Replace(html, "</head>", configScript+"</head>", 1)
|
html = strings.Replace(html, "</head>", configScript+"</head>", 1)
|
||||||
|
|
||||||
|
|||||||
+2
-14
@@ -19,18 +19,6 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
build: {
|
// 使用绝对路径,后端会根据 url_prefix 动态注入正确的路径
|
||||||
// 使用相对路径,这样可以部署在任何路径下
|
base: '/'
|
||||||
// 资源引用会使用 ./assets/ 而不是 /assets/
|
|
||||||
rollupOptions: {
|
|
||||||
output: {
|
|
||||||
// 确保资源使用相对路径
|
|
||||||
assetFileNames: 'assets/[name]-[hash][extname]',
|
|
||||||
chunkFileNames: 'assets/[name]-[hash].js',
|
|
||||||
entryFileNames: 'assets/[name]-[hash].js'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// 使用相对路径作为 base,这样资源会相对于 HTML 文件加载
|
|
||||||
base: './'
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user