我的 NextJS 应用程序存在一个严重问题,即对我的 API 路由的所有请求在生产中都超时。直到最近它都可以完美运行,但现在我的应用程序无法使用(静态登录页面除外)。它在我当地的环境中运行得很好。
我正在使用 NextJS 14.0.1 和 App Router; MongoDB + 猫鼬
以下是来自简单 /api/lead 端点的定时日志(在数据库中创建线索)
// app/api/leads/route.js
import { NextResponse } from "next/server";
import connectMongo from "@/libs/mongoose";
import Lead from "@/models/Lead";
export async function POST(req) {
console.time("MongoDB Connection");
await connectMongo();
console.timeEnd("MongoDB Connection");
console.time("Request JSON Parsing");
const body = await req.json();
console.timeEnd("Request JSON Parsing");
if (!body.email) {
return NextResponse.json({ error: "Email is required" }, { status: 400 });
}
try {
console.time("Finding Lead");
const lead = await Lead.findOne({ email: body.email });
console.timeEnd("Finding Lead");
if (!lead) {
console.time("Creating Lead");
await Lead.create({ email: body.email });
console.timeEnd("Creating Lead");
}
console.time("Sending Response");
const response = NextResponse.json({});
console.timeEnd("Sending Response");
return response;
} catch (e) {
console.error("Database operation failed", e);
console.time("Sending Error Response");
const errorResponse = NextResponse.json(
{ error: e.message },
{ status: 500 }
);
console.timeEnd("Sending Error Response");
return errorResponse;
}
}
通过邮递员获取此信息时,我仍然需要 10 秒以上才能收到响应。 Netlify / Vercel 上的默认超时发生在 10 秒后。
浏览器控制台中没有错误,在“网络”选项卡中检查请求也没有帮助。
到目前为止我做了什么:
有什么想法可能是这里的问题吗?我没主意了。
在探索了很多可能的问题之后,我设法通过升级所有依赖项并通过删除 Posthog 来解决该错误。似乎该库的依赖项之一存在一些问题。不知道为什么这个错误只会导致应用程序在生产时崩溃,但是还好。希望这可以在未来为其他人节省几个小时。