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

使用Github Actions部署NextJS到Firebase Hosting时public字段报错

解决NextJS + Firebase Hosting + Github Actions自动化部署问题

核心问题定位

  1. 配置文件放错位置:你把Hosting的配置写在了.firebaserc里,这个文件仅用于管理Firebase项目别名/环境,Hosting的配置必须放在firebase.json中,这是本地和CI环境行为不一致的根本原因之一。
  2. Github Action版本过旧:使用的FirebaseExtended/action-hosting-deploy@v0版本不支持source字段的框架托管配置,依然强制要求public字段;而本地CLI版本较新,能识别框架托管逻辑,所以本地部署正常。

解决方案

方案一:使用Firebase框架托管(推荐,支持SSR/ISR等NextJS高级特性)

1. 修正配置文件结构

创建/修改firebase.json(而非.firebaserc),写入正确的Hosting配置:

{
  "hosting": {
    "source": ".",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "frameworksBackend": {
      "region": "us-central1"
    }
  }
}

.firebaserc仅保留项目别名配置,示例如下:

{
  "projects": {
    "default": "myproject"
  }
}

2. 升级Github Action版本

将deploy-to-firebase-hosting.yml中的action版本从v0升级到最新稳定版(当前为v2),旧版本不支持框架托管的source字段:

- uses: FirebaseExtended/action-hosting-deploy@v2
  with:
    repoToken: "${{ secrets.GITHUB_TOKEN }}"
    firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT_CHECKMATE_PROD }}"
    channelId: live
    projectId: myproject

3. 验证流程

  • 本地执行npm run build && firebase deploy --only hosting确认部署正常
  • 推送代码到main分支触发CI,此时Action会自动识别NextJS框架,无需public字段即可完成部署

方案二:静态导出NextJS(仅适用于纯静态站点)

如果你的站点不需要SSR/ISR等动态特性,可以用静态导出模式:

1. 配置NextJS静态导出

在next.config.js中添加output: 'export':

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: 'export',
  // 其他自定义配置...
}

module.exports = nextConfig

2. 配置Firebase Hosting

修改firebase.json:

{
  "hosting": {
    "public": "out",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

3. 确认CI流程

npm run build会自动生成out目录,即使使用旧版本的Action也能正常部署,且页面不会出现异常。

问题答疑

  1. 指定public后页面异常:因为你错误地在.firebaserc中配置Hosting,且框架托管模式下指定public会覆盖Firebase对NextJS构建产物的自动处理,导致加载了错误的静态文件。
  2. CI要求public字段:旧版本的Action未适配Firebase的框架托管特性,升级Action版本后即可支持source字段,无需指定public。

内容的提问来源于stack exchange,提问作者engineer-x

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 21:07:10