如何将React build文件夹部署到Heroku

时间:2019-02-01 07:58:19

标签: reactjs heroku

我在互联网上转了一圈,发现this medium article使用了服务。这篇文章指导将packange.json文件修改为此:

"scripts": { 
  "dev": "react-scripts 
  "start", "start": "serve -s build", 
  "build": "react-scripts build", 
  "test": "react-scripts test --env=jsdom",
  "eject": "react-scripts eject", 
  "heroku-postbuild": "npm run build" 
},

现在这可以在开发中使用了。我收到控制台日志说内容被缓存以供脱机使用。谷歌灯塔为我提供了90%以上的高级网络应用程序。问题是,当我部署到Heroku时,在构建过程中它将运行package.json脚本中指定的npm run build。但是在打开应用程序时,我在Firehouse中获得了21%的性能,并获得了50%的渐进式Web应用程序。另外,它说服务工作者未注册。这意味着它不在构建文件夹中提供。另外,我在开发机器中运行了npm build myself,并使用build文件夹部署了TOGETHER项目,但结果仍然相同。现在,我还this other article提出建议使用node.js服务器并将package.json中的脚本更改为此:

start: "node server.js"

我对node.js一无所知,所以我决定在这里咨询以获得更好的选择。

编辑:如果需要服务器命令,例如第二篇中篇文章中的node.js以及下面Garesh的php代码中的内容-如果有人可以在python(django)中提供类似的代码,那将是很好的选择

2 个答案:

答案 0 :(得分:0)

使用以下代码构建代码:

npm run build

现在将以下代码放入index.php文件中:

<?php header( 'Location: /index.html' ) ;  ?>

现在将这个index.php文件放入您的构建文件夹中。

将此构建文件夹复制到其他位置。

现在使用heroku应用配置新文件夹。

然后

git push heroku master

答案 1 :(得分:0)

在此评论中找到了答案:

在部署应用程序之前,请转到: Heroku仪表板>设置> buildpacks>添加buildpack,然后添加github.com/mars/create-react-app-b ...

或者,您可以在命令行中完成

的Heroku buildpacks:组github.com/mars/create-react-app-b ...

如果不执行此步骤,heroku将部署您的react应用的开发版本(速度很慢),而不是优化的生产版本。

相关问题