在Google App Engine上部署create-react-app [英] Deploy create-react-app on Google App Engine
问题描述
我正尝试部署使用 create-react-app 创建的应用Google App Engine。
I'm trying to deploy my app created using create-react-app on Google App Engine.
我的应用在本地运行良好( npm start
和 npm运行build& serve -s build
)。
但是,在Google App Engine上部署的应用程序仅显示index.html,并且不会调用我的反应代码。
My app works well on local (both npm start
and npm run build & serve -s build
).
However, the deployed app on Google App Engine shows only the index.html, and does not call my react code.
Chrome开发者控制台显示未捕获的语法错误:意外的令牌<
。
The Chrome Developer Console is showing Uncaught SyntaxError: Unexpected token <
.
此外,我在console.cloud.google.com上发现已部署应用未包含构建文件夹。
Also, I found on console.cloud.google.com that the deployed app did not include the build folder. Is this correct?
任何人都可以解决这个问题吗?
Anyone can solve this problem?
这是我的package.json:
Here is my 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。
Also, here is my app.yaml.
runtime: nodejs10
handlers:
- url: /.*
static_files: build/index.html
upload: build/index.html
- url: /
static_dir: build
推荐答案
由于多次搜索网络,我发现我的应用程序的路由系统是原因。
As a result of searching the web many times, I found that the routing system of my react app was the cause.
如这篇文章指出,我们在编写app.yaml时需要谨慎。
如果我们首先编写以下项目,则GAE对于所有查询都返回 build / index.html
,包括对 / static / js
和 / static / css
。
As this post states, we need to be careful about authoring app.yaml.
If we write the following item first, GAE returns build/index.html
for all queries, including access to /static/js
and /static/css
.
- url: /
static_files: build/index.html
upload: build/index.html
create-react-app
为 npm run build
创建一个构建文件夹和静态子文件夹。
因此,我必须像这样更具体地编写我的app.yaml:
create-react-app
creates a build folder and static subfolders for npm run build
.
Thus, I had to write my app.yaml more specific like this:
handlers:
- url: /static/js/(.*)
static_files: build/static/js/\1
upload: build/static/js/(.*)
- url: /static/css/(.*)
static_files: build/static/css/\1
upload: build/static/css/(.*)
- url: /static/media/(.*)
static_files: build/static/media/\1
upload: build/static/media/(.*)
- url: /(.*\.(json|ico))$
static_files: build/\1
upload: build/.*\.(json|ico)$
- url: /
static_files: build/index.html
upload: build/index.html
- url: /.*
static_files: build/index.html
upload: build/index.html
这篇关于在Google App Engine上部署create-react-app的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!