xianyu-backend-java/frontend/vite.config.ts
lingxiaotian 770b9fc570 fix: 修复 React Router v7 警告和前端 SPA 路由问题
- 前端:启用 React Router v7 future flags 消除控制台警告
- 前端:修复订单列表 key 警告,使用 order_id 或 index 作为 fallback
- 前端:配置 Vite base 为 /static/ 以正确引用生产环境资源
- 后端:重构前端路由处理,统一使用 serve_frontend() 函数
- 后端:添加 catch-all 路由支持前端 SPA 直接访问(/dashboard、/accounts 等)
- 后端:删除旧的 /admin 路由,避
2025-12-02 17:02:39 +08:00

222 lines
6.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
// https://vitejs.dev/config/
export default defineConfig(({ command }) => ({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
server: {
port: 3000,
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/login': {
target: 'http://localhost:8080',
changeOrigin: true,
bypass: (req) => {
if (req.method === 'GET') {
return '/index.html'
}
},
},
'/verify': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/cookies': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/delivery-rules': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/system-settings': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/logs': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/users': {
target: 'http://localhost:8080',
changeOrigin: true,
},
// 管理员API - 前端有 /admin/* 路由,需要区分浏览器访问和 API 请求
'/admin': {
target: 'http://localhost:8080',
changeOrigin: true,
bypass: (req) => {
// 浏览器直接访问Accept 包含 text/html让前端路由处理
if (req.headers.accept?.includes('text/html')) {
return '/index.html'
}
},
},
'/risk-control-logs': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/qrcode': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/generate-captcha': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/verify-captcha': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/send-verification-code': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/registration-status': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/login-info-status': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/register': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/itemReplays': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/item-reply': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/default-replies': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/ai-reply-settings': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/ai-reply-test': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/password-login': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/qr-login': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/keywords-export': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/keywords-import': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/default-reply': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/static': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/backup': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/project-stats': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/change-password': {
target: 'http://localhost:8080',
changeOrigin: true,
},
'/search': {
target: 'http://localhost:8080',
changeOrigin: true,
},
// 商品管理 - 前端有 /items 路由,需要区分浏览器访问和 API 请求
'/items': {
target: 'http://localhost:8080',
changeOrigin: true,
bypass: (req) => {
// 浏览器直接访问Accept 包含 text/html让前端路由处理
if (req.headers.accept?.includes('text/html')) {
return '/index.html'
}
},
},
// 卡券管理 - 前端有 /cards 路由
'/cards': {
target: 'http://localhost:8080',
changeOrigin: true,
bypass: (req) => {
if (req.headers.accept?.includes('text/html')) {
return '/index.html'
}
},
},
// 通知渠道 - 前端有 /notification-channels 路由
'/notification-channels': {
target: 'http://localhost:8080',
changeOrigin: true,
bypass: (req) => {
if (req.headers.accept?.includes('text/html')) {
return '/index.html'
}
},
},
// 消息通知 - 前端有 /message-notifications 路由
'/message-notifications': {
target: 'http://localhost:8080',
changeOrigin: true,
bypass: (req) => {
if (req.headers.accept?.includes('text/html')) {
return '/index.html'
}
},
},
// 关键词 - 前端有 /keywords 路由
'/keywords': {
target: 'http://localhost:8080',
changeOrigin: true,
bypass: (req) => {
if (req.headers.accept?.includes('text/html')) {
return '/index.html'
}
},
},
// 订单 API - 后端路径是 /api/orders
'/api/orders': {
target: 'http://localhost:8080',
changeOrigin: true,
},
},
},
build: {
outDir: 'dist',
// 资源放在 assets 目录,通过 base 配置让引用路径为 /static/assets/
assetsDir: 'assets',
},
// 只在生产构建时使用 /static/ 作为 base开发模式使用 /
base: command === 'build' ? '/static/' : '/',
}))