You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase部署报错HTTP Error 400:hosting.rewrites[0]不符合子模式

解答:Firebase Deploy时hosting.rewrites[0]格式错误的成因

这个HTTP Error: 400, hosting.rewrites[0] is not exactly one from [subschema 0 ],[subschema 1]错误本质是你的firebase.json中hosting.rewrites数组的第一个元素,不符合Firebase Hosting规定的两种合法重写规则格式。

Firebase Hosting的重写规则只允许以下两种结构,二者选其一,不能混合或偏离:

合法的重写规则格式

1. 指向Cloud Functions的规则

必须包含source(匹配的请求路径)和function(对应的Cloud Functions函数名)两个必填字段,示例:

{
  "source": "/api/**",
  "function": "myApiFunction"
}

2. 指向静态资源/SPA路由的规则

必须包含source和destination(重写后的目标文件路径)两个必填字段,常用于SPA单页应用的catch-all路由,示例:

{
  "source": "**",
  "destination": "/index.html"
}

你可能踩的坑(错误成因)

结合这个错误提示,你大概率犯了以下某一种格式错误:

  • 写错了字段名称:比如把function拼成functions,或者destination拼写错误;
  • 混合了两种规则的字段:同时在同一个rewrite对象里写了function和destination(Firebase不允许这种混合);
  • 缺少必填字段:比如漏写了source字段,或者只写了function没写source;
  • 字段值格式非法:比如source的路径格式不符合Glob规则,或者function的名称和你index.js中导出的函数名不匹配(格式校验阶段也会触发这个错误)。

修正建议

拿你的firebase.json对照上面的合法格式,重点检查rewrites数组的第一个元素:

  1. 确认只使用function+source或者destination+source的组合;
  2. 核对字段拼写是否完全正确;
  3. 确保function的名称和你index.js中导出的函数名一致(比如你在index.js里写了exports.api = functions.https.onRequest(...),那function的值就得是api)。

举个完整的正确配置示例(结合Cloud Functions和SPA重写):

{
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
  "functions": {
    "runtime": "nodejs18"
  }
}

内容的提问来源于stack exchange,提问作者Patrickkx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:46:43