Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<title>NewsSystemPro</title>
<script>
// GitHub Pages SPA 重定向解决方案
// 将路径参数存储在 sessionStorage 中,然后重定向到 index.html
// 自动适配根域名和子路径部署
sessionStorage.redirect = location.href;
var basePath = location.pathname.replace(/\/404\.html$/, '/') || '/';
location.replace(location.origin + basePath);
</script>
<meta http-equiv="refresh" content="0;URL='/NewsSystemPro/'">
</head>
<body>
<p>正在加载...</p>
Expand Down
25 changes: 19 additions & 6 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -119,4 +131,5 @@ export default defineConfig(({ mode }) => ({
],
},
},
}))
}
})
Loading