You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vite Dev Server结合React Router动态路由重写失效,rewrite-all插件无效

Vite + React 动态路由(list/:id)路径重写问题解决

问题分析

你遇到的vite:html-fallback Not rewriting GET /list/src/index.tsx because the path includes a dot (.) character错误,本质是Vite默认不会重写包含点(.)的路径,导致动态路由请求无法正确fallback到index.html。而你之前的插件配置方式有误,导致pluginRewriteAll未生效。

正确解决方案

1. 确认插件安装

先安装依赖:

npm install vite-plugin-rewrite-all --save-dev

2. 修正Vite配置

你之前用{...pluginRewriteAll()}的写法会破坏插件内部结构,正确配置需直接调用插件函数并放入plugins数组:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import pluginRewriteAll from 'vite-plugin-rewrite-all';

export default defineConfig({
  plugins: [react(), pluginRewriteAll()],
  // 可选:显式声明historyApiFallback,插件已默认处理,但可增强可读性
  server: {
    historyApiFallback: true
  }
});

3. 额外检查项

  • 确保路由使用BrowserRouter而非HashRouter,Hash模式不会触发服务器端路径重写。
  • 升级Vite至4.x及以上版本,避免旧版本与插件的兼容性问题。

内容的提问来源于stack exchange,提问作者papryk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 14:20:19