【问题标题】:React app not locating .js files when deployed to custom domain with Google Cloud (GAE)使用 Google Cloud (GAE) 部署到自定义域时,React 应用程序无法定位 .js 文件
【发布时间】:2021-04-28 09:08:25
【问题描述】:

我有一个 React 应用程序,我正试图在带有 GCP 的自定义域 app.[customDomain].com 上运行它。它找不到静态文件,我不知道为什么。

React 应用程序作为服务部署在 Google 云上(不是默认的,是项目中的第二个服务)。它在 Google 分配的 URL (https://[Service_ID]-dot-[Project_ID].ue.r.appspot.com/) 上完美运行。通过自定义域运行时,会加载 index.html,但找不到任何 .js 文件(因此结果是空白屏幕)并给出以下错误:

.js 文件出现 404 错误

项目中的其他服务(默认和 Django API)在同一个域上服务得很好,我的目标是让 React 应用程序在 app. 子域上服务。为了实现这一点,我有以下dispatch.yaml

dispatch:
- url: "www.[customDomain].com/"
  service: default

- url: "api.[customDomain].com/"
  service: default

- url: "app.[customDomain].com/"
  service: React-App

我怀疑问题出在 app.yaml 的 react 应用程序中,但我无法确定需要更改的内容。这是当前的app.yaml

env: standard
runtime: nodejs14
service: React-App
handlers:
- url: /static
  secure: always
  static_dir: build/static
- url: /(.*\.(json|ico|js))$
  secure: always
  static_files: build/\1
  upload: build/.*\.(json|ico|js)$
- url: .*
  secure: always
  redirect_http_response_code: 301
  static_files: build/index.html
  upload: build/index.html

我尝试将"homepage" 更改为package.json,但没有成功。

除了依赖项之外,package.json 还包含:

"name": "React-App",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:8000",

【问题讨论】:

  • 您在部署期间是否在日志中获得任何相关信息/错误?您可以尝试使用设置为调试的详细程度标志重新部署以获取更多信息(只需添加--verbosity=debug tp 您的命令)?另外,如果您重新创建自定义域会怎样?
  • @RafaelLemos 使用gcloud app deploy --verbosity=debug 重新运行部署,唯一突出的是DEBUG: No bucket specified, retrieving default bucket.。否则一切似乎都很成功。重新创建自定义域是什么意思?

标签: reactjs google-app-engine google-cloud-platform subdomain


【解决方案1】:

自己发现了问题。问题在dispatch.yaml。 urls 字段必须是[subDomain].[customDomain].com/*

在当前形式中,它仅是 URL 上的精确文本匹配(回退到 default 服务)。这意味着页面的初始加载是命中React-App 服务,但资源的加载却落在了default 服务上,因此无法定位它们。将 * 添加到 url 意味着资源请求被路由到正确的服务。

【讨论】:

    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多