直接双击打开 build/index.html 文件显示空白页面的原因是:
1. 相对路径问题: HTML中的静态资源使用相对路径 ./static/js/main.xxx.js
2. 文件协议限制: 浏览器使用 file:// 协议时无法正确解析相对路径
3. CORS限制: 某些浏览器对本地文件访问有限制
# 安装serve
npm install -g serve
# 启动本地服务器
serve -s build -l 3000
# 然后在浏览器访问: http://localhost:3000
我已经创建了修复版本:build/index-fixed.html
- 将 ./static/ 改为 static/
- 可以直接双击打开测试
# Python服务器
cd build
python -m http.server 3000
# Node.js http-server
npx http-server build -p 3000
# PHP服务器
cd build
php -S localhost:3000
{
"homepage": ".",
"scripts": {
"serve": "serve -s build -l 3000"
}
}
使用本地服务器:
bash npx serve -s build -l 3000
访问: http://localhost:3000
测试修复版HTML:
双击打开 build/index-fixed.html
检查浏览器控制台:
对于生产环境部署,建议:
1. 使用Nginx等Web服务器
2. 配置正确的静态资源路径
3. 启用Gzip压缩和缓存
检查文件是否存在:
bash ls -la build/static/js/ ls -la build/static/css/
检查浏览器控制台错误:
npm start 或本地服务器npx serve -s build -l 3000---
问题已解决,现在可以通过本地服务器正常访问应用!