简体   繁体   English

Google App Engine Static 网站处理程序通配符路由

[英]Google App Engine Static Website Handler Wildcard Routes

I have a React.js website using create-react-app and just moved from Firebase Hosting to Google App Engine Standard Environment.我有一个使用 create-react-app 的 React.js 网站,并且刚刚从 Firebase 托管迁移到 Google App Engine 标准环境。

With Firebase Hosting, I could create routes like https://example.com/route or https://example.com/newRoute/123 and Firebase would know to serve the index.html for any of these routes. With Firebase Hosting, I could create routes like https://example.com/route or https://example.com/newRoute/123 and Firebase would know to serve the index.html for any of these routes.

I want to wildcard any route of our application to use the index.html while excluding.js and.css files.我想通配我们应用程序的任何路由以使用 index.html 同时排除 .js 和 .css 文件。 If I use only the wildcard /(.*) then the request for our main.js file also resolves to index.html.如果我只使用通配符 /(.*),那么对 main.js 文件的请求也会解析为 index.html。

Here is our handler config这是我们的处理程序配置

handlers:
  - url: /
    secure: always
    application_readable: false
    static_files: build/index.html
    require_matching_file: false
    upload: build/index.html
  - url: /login
    secure: always
    application_readable: false
    static_files: build/index.html
    require_matching_file: false
    upload: build/index.html
  - url: '/(.*)'
    secure: always
    application_readable: false
    static_files: "build/\\1"
    require_matching_file: false
    upload: 'build/.*'

In the current configuration, every route I create must be registered as a handler.在当前配置中,我创建的每条路由都必须注册为处理程序。 I was hoping to find a solution where all current routes and future routes can be handled by a wildcard.我希望找到一个解决方案,其中所有当前路线和未来路线都可以通过通配符处理。

The order of the handlers matters, the 1st one with a matching pattern wins. 处理程序的顺序很重要,具有匹配模式的第一个处理程序将获胜。

So to achieve what you want you could handle the exceptions first, before "wildcarding" the index.html . 因此,要实现所需的目标,可以先“处理通配符” index.html ,然后再处理异常。 Something along these lines (I assumed the .css and .js files are also relative to the build dir): 这些内容(我假设.css.js文件也与build目录有关):

handlers:

  - url: /(.*\.css)$
    secure: always
    static_files: build/\1
    upload: build/.*\.css$

  - url: /(.*\.js)$
    secure: always
    static_files: build/\1
    upload: build/.*\.js$

  # continue similarly the other static assets
  # or maybe try a more generic one covering several of them:
  - url: /(.*\.(js|css|png|jpg|svg))$
    secure: always
    static_files: build/\1
    upload: build/.*\.(js|css|png|jpg|svg)$

  # wildcard everything else, serving index.html
  - url: '/(.*)'
    secure: always
    static_files: build/index.html
    upload: build/index.html

Side note: for readability I also dropped the require_matching_file (no such thing in GAE) and application_readable (by default it is false). 旁注:为了提高可读性,我还删除了require_matching_file (在GAE中没有这样的东西)和application_readable可读(默认为false)。

The answer from Dan still does it.丹的回答仍然有效。 The only addition I had to implement was for fonts extension in the 3rd handler.我必须实现的唯一补充是第三个处理程序中的 fonts 扩展。 As a FYI, I am using this config for a Vue 3 app and the default build settings.作为仅供参考,我将此配置用于 Vue 3 应用程序和默认构建设置。

Final app.yaml for me:最终 app.yaml 对我来说:

handlers:

  - url: /(.*\.css)$
    secure: always
    static_files: build/\1
    upload: build/.*\.css$

  - url: /(.*\.js)$
    secure: always
    static_files: build/\1
    upload: build/.*\.js$

  # continue similarly the other static assets
  # or maybe try a more generic one covering several of them:
  - url: /(.*\.(js|css|png|jpg|svg|woff|woff2|ttf))$
    secure: always
    static_files: dist/\1
    upload: dist/.*\.(js|css|png|jpg|svg|woff|woff2|ttf)$

  # wildcard everything else, serving index.html
  - url: '/(.*)'
    secure: always
    static_files: build/index.html
    upload: build/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google App Engine:在找到合适的 Handler 时忽略 URL Case - Google App Engine: Ignore URL Case when finding appropriate Handler Google App Engine - Static IP 地址 - 路由域名 - Google App Engine - Static IP Address - Route Domain Name 在 Google Cloud App Engine 中部署 Django 应用后网站无法加载 - The website doesn't load after deploying Django app in Google Cloud App Engine 提供来自 Google Cloud Storage 的 static SPA 和来自 Google App Engine 的 API - Serve static SPA from Google Cloud Storage and API from Google App Engine 谷歌应用引擎 JDO 3 - Google App Engine JDO 3 Google App Engine ModuleHostname:不是 App Engine 上下文 - Google App Engine ModuleHostname: not an App Engine context 无法连接到服务器错误 Google App Engine 静态文件问题 - Can't connect to server error Google App Engine Static Files Issue 是否有用于 Google App Engine(Java 语言)的 HTML 模板引擎? - Is there a HTML templating engine for Google App Engine (in Java)? 在 Google Compute Engine 上删除或释放静态 IP - Removing or releasing a static IP on Google Compute Engine Google Compute Engine 分配静态 IP 定价? - Google Compute Engine Assigning Static IP pricing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM