react项目和静态页面一起部署,应该如何实现?
我们有个react项目,还有一个同项目的静态首页。
最后打包后的目录结构大致是
dist
- index.html // 静态页面
- react // react项目打包页面
- - index.html
- - umi.js
- - layout.js
- - layout.css
希望访问http://localhost:8081的时候访问index.html,访问http://localhost:8081/react之后的都访问react中的页面。
umi配置文件
export default {
npmClient: 'pnpm',
outputPath: 'dist/react',
copy: [
{
from: 'web', // web文件夹里就是index.html 静态页面
to: 'dist',
},
],
};
dist放到nginx上,配置nginx
server {
listen 8081;
server_name localhost;
location / {
root /demo/dist;
index index.html index.htm;
}
}
部署后,访问http://localhost:8081可以访问静态首页,但是访问http://localhost:8081/react的时候,react/index.html和react/umi.js都可以正确访问,但是layout.js和layout.css没有访问,导致页面空白。
请问这种需求如何解决?
回复
1个回答

test
2024-07-13
配置一下 publicPath
就行了(和你配置的 outputPath
同级的属性),就是部署的二级目录,也就是 /react/
或者 ./
都可以。你这个问题就是把 outputPath
和 publciPath
的作用记错/记混了,导致最后编译后的项目静态资源缺少了二级目录的路径。
这里是 UmiJS
的配置文档 👉 publicpath - 配置 | UmiJS
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容