Clerk 中间件加载时间过长,导致 NextJS / vercel - 504 错误“FUNCTION_INVOCATION_TIMEOUT”

问题描述 投票:0回答:1

我正在制作一个网页,我必须使用 clerk 进行身份验证和登录,但中间件加载时间太长,因此应用程序无法正确部署。

这是 middleware.ts 代码:

import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({
    publicRoutes: ['/', '/api/webhook/clerk', "/api/uploadthing"],
    ignoredRoutes: ['/api/webhook/clerk']
});

export const config = {
    matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

Vercel log:

如何解决这个问题?请帮忙!

typescript next.js runtime-error middleware clerk
1个回答
0
投票

我是 Clerk 的 DevX 工程师,也许我可以在这里提供帮助。我的第一个建议是从公共中删除 API 路由,尤其是您在

ignoreRoutes
中复制的路由,原因是
publicRoutes
允许在这些路由上访问 Auth 对象,而
ignoreRoutes
允许中间件跳过在该路由上运行的中间件路线,两者都有它可能是中间件运行缓慢的原因。

这里是一个示例,如果您在 API 中不需要 Auth 对象,如果您将其放入

publicRoutes
中,希望有所帮助:)


import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({
    publicRoutes: ['/'],
    ignoredRoutes: ['/api(.*)']
});

export const config = {
    matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
© www.soinside.com 2019 - 2024. All rights reserved.