React应用部署路径配置问题:修复gh-pages部署后链接失效
问题1:部署到根域名(jsitges.github.io/)时弹出“no shallow build allowed”错误
- 首先得明确:GitHub用户主页仓库必须命名为
jsitges.github.io,你当前仓库名是servitodo,这是核心问题之一。先去仓库设置里把名称改成jsitges.github.io。 - 要是克隆仓库时用了
--depth 1做浅克隆,也会触发这个错误,得重新完整克隆:git clone --no-shallow https://github.com/jsitges/jsitges.github.io.git - 修改
package.json里的homepage字段:"homepage": "https://jsitges.github.io/" - 最后重新执行部署命令:
npm run deploy
问题2:部署到子路径(jsitges.github.io/servitodo)时路径配置混乱
- 先把
package.json的homepage设置正确:"homepage": "https://jsitges.github.io/servitodo" - 如果是React项目且用了React Router,得给
BrowserRouter添加basename属性指定子路径:<BrowserRouter basename="/servitodo"> {/* 你的路由内容 */} </BrowserRouter> - 静态资源(图片、CSS等)的引用要注意:要么用相对路径,要么加上
process.env.PUBLIC_URL前缀,比如:<img src={`${process.env.PUBLIC_URL}/logo.png`} alt="Logo" /> - 重新构建并部署:
npm run build npm run deploy
内容的提问来源于stack exchange,提问作者Julio Sitges
相关产品推荐
相关产品推荐

