reactjs - 如何使用 GCP 部署 React Production Build?

标签 reactjs google-app-engine deployment web-deployment

我正在尝试将我的 React 项目部署到生产,似乎 Google App Engine 正在构建开发而不是 Production Build 文件夹。也许我错过了一些非常简单的东西......

我首先运行“npm run build”,然后运行“npm run deploy”或“gcloud app deploy app.yaml --project [project_name]”

文件夹结构:
Here is my folder structure
这是我的 app.yaml 文件

runtime: nodejs
env: flex

env_variables:
    APP_ENV: "production"

handlers:
  - url: /
    static_files: build/index.html
    upload: build/index.html
  - url: /
    static_dir: build
  - url: /static
    static_dir: build/static

automatic_scaling:
    min_num_instances: 2
    max_num_instances: 4
    cool_down_period_sec: 180
    cpu_utilization:
       target_utilization: 0.50

resources:
    cpu: 1
    memory_gb: 3.5 
    disk_size_gb: 10

这是我的 package.json:
 "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "deploy": "gcloud app deploy app.yaml --project ....",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

当它完成部署时,我仍然会发出开发警告,当我运行页面速度时 - js 文件没有被缩小/优化。我错过了什么?

最佳答案

gcloud app deploy命令被执行,它运行 npm start命令来启动应用程序。这是在 Docs 中指定的.

在您的情况下,npm start命令执行 "react-scripts start" , 启动开发服务器。

为了在生产环境中为您的应用提供服务,您的 start命令应该启动一个 Web 服务器,该服务器为您的 React 应用程序的静态文件提供服务。为此,您可以执行以下操作:

npm install -s serve

然后,更改您的 package.json 中的启动脚本从:
"start": "react-scripts start"

到:
"start": "serve -s build -l 8080"

然后正常部署

关于reactjs - 如何使用 GCP 部署 React Production Build?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60271147/

相关文章:

python - 当我认为不应该时,App Engine urlfetch 正在引发异常

python - 在 Google App Engine 中上传图片

javascript - 从 firebase 进行身份验证后,我想在状态中存储名称、电子邮件

iphone - 从 Google App Engine 的数据存储中获取实体以用于 iOS 应用

java - 如何使Java应用程序独立?

c++ - 尝试使用共享库部署 Qt 应用程序时出错

网 bean 11.2 : No suitable Deployment Server is defined for the project or globally

javascript - 针对用户角色的不同 React Webpack JS 包

javascript - 如何在React循环中使用settimeout函数更新状态?

javascript - REACT APP 无法访问谷歌云运行环境变量