问题描述
在 apps/studio 项目部署到 Vercel 后,所有 /api/* 路由都返回 HTML(即 SPA 的 index.html),而不是期望的 JSON API 响应。项目 hotcrm 在同环境下工作正常。
原因分析
vercel.json 中设置 "framework": null,Vercel 无法识别 api/ 目录为 Serverless Functions。- 没有使用
outputDirectory 指定静态文件路径,导致 /api/index.js 作为静态文件提供。 - rewrite 规则
{ "source": "/api/(.*)", "destination": "/api" } 实际上将 API 路由到静态文件,未匹配时 fallback 到 /index.html。 - 参考
hotcrm,Next.js 项目 API routes 能正常被识别为 Function。
复现步骤
- 部署 studio 项目到 Vercel。
- 访问任何
/api/* 路径。 - 结果均返回 HTML 页面内容,而不是 JSON 响应。
期望行为
- 任何
/api/* 路由应由 Serverless Function 处理,Ajax 应返回正确的 JSON 响应。
建议修复方案
- 显式在
vercel.json 配置 outputDirectory: "public" 和 functions.api/index.js,让 Vercel 正确识别 serverless 函数。 - 修改 rewrite,将
/api/(.*) 路由到 /api/index.js 而不是 /api。 - 样例配置:
{
"framework": null,
"outputDirectory": "public",
"functions": {
"api/index.js": { "memory": 1024, "maxDuration": 60 }
},
"rewrites": [
{ "source": "/api/(.*)", "destination": "/api/index.js" },
{ "source": "/((?!api/).*)", "destination": "/index.html" }
]
}- 注意在构建脚本
build-vercel.sh 中,不要把 api/ 目录包含进 public/ 下。
补充
- 请完成修复后务必自测所有 API 路由,并同步更新 CHANGELOG/ROADMAP。
如需协助可 @Copilot 进行进一步分析或 PR。
问题描述
在
apps/studio项目部署到 Vercel 后,所有/api/*路由都返回 HTML(即 SPA 的index.html),而不是期望的 JSON API 响应。项目hotcrm在同环境下工作正常。原因分析
vercel.json中设置"framework": null,Vercel 无法识别api/目录为 Serverless Functions。outputDirectory指定静态文件路径,导致/api/index.js作为静态文件提供。{ "source": "/api/(.*)", "destination": "/api" }实际上将 API 路由到静态文件,未匹配时 fallback 到/index.html。hotcrm,Next.js 项目 API routes 能正常被识别为 Function。复现步骤
/api/*路径。期望行为
/api/*路由应由 Serverless Function 处理,Ajax 应返回正确的 JSON 响应。建议修复方案
vercel.json配置outputDirectory: "public"和functions.api/index.js,让 Vercel 正确识别 serverless 函数。/api/(.*)路由到/api/index.js而不是/api。{ "framework": null, "outputDirectory": "public", "functions": { "api/index.js": { "memory": 1024, "maxDuration": 60 } }, "rewrites": [ { "source": "/api/(.*)", "destination": "/api/index.js" }, { "source": "/((?!api/).*)", "destination": "/index.html" } ] }build-vercel.sh中,不要把api/目录包含进public/下。补充
如需协助可 @Copilot 进行进一步分析或 PR。