使用App Router的Next.js应用部署至Firebase Hosting失败求助
Next.js App Router部署Firebase Hosting的Cloud Functions问题解决指南
问题1:部署时触发Cloud Function构建,随后报错Error: Failed to list functions for project-987abc
问题背景
Next.js App Router的动态路由(如/doc/[id]/page.tsx)和图片优化功能依赖Cloud Functions托管SSR/ISR内容,Firebase会自动创建对应函数,但CLI版本过低、权限异常或路由配置缺失会导致函数列表查询失败。
解决步骤
- 更新Firebase CLI:旧版本CLI对App Router支持不完善,执行命令升级:
npm install -g firebase-tools - 添加Hosting重写规则:在
firebase.json的hosting节点中配置路由重写,确保动态路由指向生成的Cloud Function:{ "hosting": { "site": "project-name", "rewrites": [ { "source": "/doc/**", "function": "nextjs-server" }, { "source": "**", "function": "nextjs-server" } ] } } - 重置Firebase权限:若存在权限问题,重新登录CLI:
firebase logout && firebase login
问题2:maxInstances配额超限,且firebase.json中设置maxInstances: 3无效
问题原因
直接在firebase.json根节点的functions下设置maxInstances是全局配置,不会作用于Firebase为Next.js自动创建的专属函数(默认名为nextjs-server)。
解决方法
方法一:通过CLI配置目标函数实例数
- 先执行一次部署(即使失败,Firebase会生成函数的基础框架)
- 运行命令设置
nextjs-server函数的实例上限:firebase functions:config:set nextjs.max_instances=3 - 重新部署Hosting:
firebase deploy --only hosting:project-name
方法二:在next.config.js中传递函数配置
在Next.js配置文件中添加Firebase函数实例限制,部署时会自动应用到生成的Cloud Function:
/** @type {import('next').NextConfig} */ const nextConfig = { experimental: { appDir: true, }, serverRuntimeConfig: { firebaseFunctions: { maxInstances: 3, }, }, }; module.exports = nextConfig;
方法三:修正firebase.json的函数配置
确保functions节点指向Next.js生成的函数目录,并添加实例限制:
{ "hosting": { "site": "project-name", "source": ".", "rewrites": [ { "source": "**", "function": "nextjs-server" } ] }, "functions": { "source": ".next/functions", "runtime": "nodejs18", "maxInstances": 3, "memory": "256MB" } }
额外提示
- 确认项目处于Blaze套餐,Cloud Functions的基础配额已激活
- 若调整实例数后仍提示配额不足,可在Google Cloud控制台的「IAM与管理」→「配额」页面申请提升
maxInstances配额
内容的提问来源于stack exchange,提问作者demhareesan
相关产品推荐
相关产品推荐

