diff --git a/public/404.html b/public/404.html index 704909d..3b5f9c5 100644 --- a/public/404.html +++ b/public/404.html @@ -5,10 +5,11 @@
正在加载...
diff --git a/vite.config.js b/vite.config.js index 98a1d3b..3f03b63 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,11 +1,23 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import react from '@vitejs/plugin-react' import path from 'path' -export default defineConfig(({ mode }) => ({ - // GitHub Pages 部署时需要设置 base - // 本地开发时为 '/', 部署到 GitHub Pages 时为 '/仓库名/' - base: mode === 'production' ? '/NewsSystemPro/' : '/', +const normalizeBasePath = (value) => { + if (!value || value === '/') { + return '/' + } + + const trimmed = value.replace(/^\/+|\/+$/g, '') + return `/${trimmed}/` +} + +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, process.cwd(), '') + const basePath = normalizeBasePath(env.VITE_BASE_PATH) + + return { + // 允许通过 VITE_BASE_PATH 在根域名和子路径部署间切换 + base: basePath, plugins: [ react({ @@ -119,4 +131,5 @@ export default defineConfig(({ mode }) => ({ ], }, }, -})) + } +})