-
Notifications
You must be signed in to change notification settings - Fork 1
[Bug] studio 部署到 Vercel 后,API 路由返回 HTML 而不是 JSON #1051
Copy link
Copy link
Labels
bugSomething isn't workingSomething isn't working
Description
问题描述
在 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。
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working