Vite(React / Vue SPA) 部署指南
Vite 构建的单页应用(React / Vue / Svelte)输出静态文件到 dist/。关键是路由回退:所有未知路径都要返回 index.html。
- 构建命令
- npm run build
- 输出目录
- dist/
- 官方文档
- Vite Deploying a Static Site · Cloudflare Pages SPA rendering
部署方式
- npm run build,上传 dist/。
- Nginx:location / { try_files $uri $uri/ /index.html; }(nginx.cc 生成器有 SPA 场景)。
- Netlify:在 public/_redirects 写 /* /index.html 200。
- Cloudflare Pages:项目里没有 404.html 时默认按 SPA 处理(见官方 Serving Pages 文档)。
- GitHub Pages:部署到子路径时设置 Vite 的 base;GitHub Pages 本身没有 SPA 回退,常见做法是复制 index.html 为 404.html(会返回 404 状态码)。
各平台说明
| 平台 | 说明 |
|---|---|
| Cloudflare Pages / Netlify / Vercel | 构建 npm run build,输出 dist。 |
| VPS + Nginx | try_files 回退到 index.html。 |
注意事项
- 使用 History 路由时一定要配置回退,否则刷新页面会 404。