在 Google App Engine 上部署 create-react-app

Deploy create-react-app on Google App Engine

我正在尝试在 Google App Engine 上部署使用 create-react-app 创建的应用程序。

我的应用程序在本地运行良好(npm startnpm run build & serve -s build)。 但是,Google App Engine 上部署的应用程序仅显示 index.html,并没有调用我的反应代码。

Chrome 开发者控制台显示 Uncaught SyntaxError: Unexpected token <

此外,我在 console.cloud.google.com 上发现部署的应用程序不包含构建文件夹。这是正确的吗?

谁能解决这个问题?

这是我的 package.json:

{
  "name": "XXXXX",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.8.2",
    "react-dom": "^16.8.2",
    "react-ga": "^2.5.7",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
 },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

此外,这是我的app.yaml。

runtime: nodejs10

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

你能看看这个文件吗 - https://medium.com/tech-tajawal/deploying-react-app-to-google-app-engine-a6ea0d5af132

这对你有帮助。我已经使用相同的步骤部署了我的应用程序。

示例 yaml 文件

    runtime: python27
    api_version: 1
    threadsafe: true
    handlers:
    - url: /
      static_files: build/index.html
      upload: build/index.html
    - url: /
      static_dir: build

参考 link - https://medium.com/google-cloud/how-to-deploy-a-static-react-site-to-google-cloud-platform-55ff0bd0f509

谢谢!

在网上搜索了很多次,发现是我的react app的路由系统导致的。

正如 this post 所述,我们需要谨慎创作 app.yaml。 如果我们先写下面的项目,GAE returns build/index.html 用于所有查询,包括访问 /static/js/static/css.

- url: /
  static_files: build/index.html
  upload: build/index.html

create-react-appnpm run build 创建一个构建文件夹和静态子文件夹。 因此,我不得不像这样更具体地写 app.yaml:

handlers:
  - url: /static/js/(.*)
    static_files: build/static/js/
    upload: build/static/js/(.*)
  - url: /static/css/(.*)
    static_files: build/static/css/
    upload: build/static/css/(.*)
  - url: /static/media/(.*)
    static_files: build/static/media/
    upload: build/static/media/(.*)
  - url: /(.*\.(json|ico))$
    static_files: build/
    upload: build/.*\.(json|ico)$
  - url: /
    static_files: build/index.html
    upload: build/index.html
  - url: /.*
    static_files: build/index.html
    upload: build/index.html