Skip to content

[Bug] studio 部署到 Vercel 后,API 路由返回 HTML 而不是 JSON #1051

@xuyushun441-sys

Description

@xuyushun441-sys

问题描述

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。

复现步骤

  1. 部署 studio 项目到 Vercel。
  2. 访问任何 /api/* 路径。
  3. 结果均返回 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。

Metadata

Metadata

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions