配置托管行为(续11)

您可以使用 rewrites 创建自定义网域 Dynamic Links。如需详细了解如何为 Dynamic Links 设置自定义网域,请参阅 Dynamic Links 文档。

  • 将您的自定义网域仅用于 Dynamic Links

``` "hosting": { // ...

"appAssociation": "AUTO",  // required for Dynamic Links (default is AUTO if not specified)

// Add the "rewrites" attribute within "hosting"
"rewrites": [ {
  "source": "/**",  // the Dynamic Links start with "https://CUSTOM_DOMAIN/"
  "dynamicLinks": true
} ]

} ``` - 指定要用于 Dynamic Links 的自定义网域路径前缀

``` "hosting": { // ...

"appAssociation": "AUTO",  // required for Dynamic Links (default is AUTO if not specified)

// Add the "rewrites" attribute within "hosting"
"rewrites": [ {
  "source": "/promos/**",  // the Dynamic Links start with "https://CUSTOM_DOMAIN/promos/"
  "dynamicLinks": true
}, {
  "source": "/links/share/**",  // the Dynamic Links start with "https://CUSTOM_DOMAIN/links/share/"
  "dynamicLinks": true
} ]

} ```

在 firebase.json 文件中配置 Dynamic Links 需要进行如下设置:

字段 说明
appAssociation 必须设置为 AUTO - 如果您未在配置中添加此属性,则 appAssociation 的默认值为 AUTO。 - 通过将此属性设置为 AUTO,Hosting 可以在收到相应请求时动态生成 assetlinks.json 和 apple-app-site-association 文件。
rewrites
source 要用于 Dynamic Links 的路径 与将路径重写为网址的规则不同,Dynamic Links 重写规则不可包含正则表达式。
dynamicLinks 必须设置为 true

配置标头

可选 通过标头,客户端和服务器可以将其他信息连同请求或响应一起传递。一些标头集可能会影响浏览器处理页面及其内容的方式,包括访问权限控制、身份验证、缓存和编码。

通过创建包含一系列标头对象的 headers 属性来指定文件专属的自定义响应标头。在每个对象中,指定一个网址格式,如果该格式与请求网址路径匹配,便触发 Hosting 应用指定的自定义响应标头。

以下是 headers 属性的基本结构。此示例对所有字体文件应用了 CORS 标头。

"hosting": {
  // ...

  // Applies a CORS header for all font files
  "headers": [ {
    "source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
    "headers": [ {
      "key": "Access-Control-Allow-Origin",
      "value": "*"
    } ]
  } ]
}

查看 headers 属性的更详细示例

"hosting": {
  // ...

  // Add the "headers" attribute within "hosting"
  "headers": [ {
    // Applies a CORS header for all font files
    "source": "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
    "headers": [ {
      "key": "Access-Control-Allow-Origin",
      "value": "*"
    } ]
  }, {
    // Overrides the default 1 hour browser cache with a 2 hour cache for all image files
    "source": "**/*.@(jpg|jpeg|gif|png)",
    "headers": [ {
      "key": "Cache-Control",
      "value": "max-age=7200"
    } ]
  }, {
    // A regular expression-based rewrite equivalent to the above behavior
    "regex": ".+/\w+\.(jpg|jpeg|gif|png)$",
    "headers": [ {
      "key": "Cache-Control",
      "value": "max-age=7200"
    } ]
  }, {
    // Sets the cache header for 404 pages to cache for 5 minutes
    "source": "404.html",
    "headers": [ {
      "key": "Cache-Control",
      "value": "max-age=300"
    } ]
  } ]
}

headers 属性包含一系列定义,其中每个定义都必须包含下表中的字段。

字段 说明
headers
source(推荐) 或 regex 一个网址格式,如果该格式与初始请求网址匹配,则会触发 Hosting 应用自定义标头 - 使用 source 可指定 glob(推荐)。 - 使用 regex 可指定 RE2 正则表达式。 如需创建与您的自定义 404 页面匹配的标头,请使用 404.html 作为您的 source 或 regex 值。
一系列(子)headers Hosting 应用于请求路径的自定义标头 每个子标头都必须包含一个 key-value 对(请参阅下面两行)。
key 标头的名称,例如 Cache-Control
value 标头的值,例如 max-age=7200

如需详细了解 Cache-Control,请参阅介绍如何提供动态内容和托管微服务的“Hosting”部分。您还可以详细了解 CORS 标头。

控制 .html 扩展名

可选 通过 cleanUrls 属性,您可以控制网址是否应包含 .html 扩展名。

如果值为 true,Hosting 会从已上传的文件网址中自动删除 .html 扩展名。如果在请求中添加 .html 扩展名,Hosting 会执行 301 重定向,以重定向至相同路径,但会删除 .html 扩展名。

以下是通过添加 cleanUrls 属性来控制在网址中包含 .html 的方法:

"hosting": {
  // ...

  // Drops `.html` from uploaded URLs
  "cleanUrls": true
}

控制尾随斜杠

可选 trailingSlash 属性可让您控制静态内容网址是否应包含结尾斜杠。

  • 如果值为 true,Hosting 会重定向网址以添加尾随斜杠。
  • 如果值为 false,Hosting 会重定向网址以移除尾随斜杠。
  • 如果不指定,Hosting 将仅对目录索引文件(例如 about/index.html)使用尾随斜杠。

以下是通过添加 trailingSlash 属性来控制尾随斜杠的方法:

"hosting": {
  // ...

  // Removes trailing slashes from URLs
  "trailingSlash": false
}

trailingSlash 属性不会影响对 Cloud Functions 或 Cloud Run 提供的动态内容的重写。

Glob 模式匹配

Firebase Hosting 配置选项通过 extglob 广泛使用了 glob 模式匹配表示法,其方式与 Git 处理 gitignore 规则以及 Bower 处理 ignore 规则的方式类似。此 Wiki 页提供了更加详细的参考。以下是对此页面中使用的示例的说明:

  • firebase.json - 仅与 public 目录的根目录下的 firebase.json 文件匹配
  • ** - 与任意子目录中的任何文件或文件夹匹配
  • * - 仅与 public 目录的根目录下的文件和文件夹匹配
  • **/.* - 匹配任意子目录中以 . 开头的任何文件(通常是隐藏文件,例如 .git 文件夹中的文件)
  • **/node_modules/** - 匹配 node_modules 文件夹(该文件夹本身可以位于 public 目录的任意子目录中)的任意子目录中的任何文件或文件夹
  • **/*.@(jpg|jpeg|gif|png) - 匹配任意子目录中以 .jpg、.jpeg、.gif 或 .png 结尾的任何文件