使用Clerk中间件时Edge Function崩溃问题求助(Vercel生产环境)
解决Clerk+next-intl在Vercel生产环境Edge Function崩溃问题
我在Next.js App目录下使用Clerk实现身份验证功能,本地运行完全正常,但部署到Vercel生产环境后页面报错:This Edge Function has crashed.。使用的依赖版本如下:
"react": "18.2.0", "react-dom": "18.2.0", "next": "13.4.12", "next-intl": "^2.19.0"

相关代码
middleware.ts
import { authMiddleware } from "@clerk/nextjs"; import createMiddleware from "next-intl/middleware"; const intlMiddleware = createMiddleware({ locales: ["en", "zh"], defaultLocale: "en", }); export default authMiddleware({ beforeAuth: (req) => { return intlMiddleware(req); }, publicRoutes: ["/"], }); export const config = { matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"], };
next.config.js
/** @type {import('next').NextConfig} */ const nextConfig = { images: { domains: ["www.gravatar.com"], formats: ["image/avif", "image/webp"], }, swcMinify: false, }; module.exports = nextConfig;
排查方向及解决方案
1. 中间件兼容问题调整
Clerk的authMiddleware与next-intl中间件结合时,需确保请求链不被中断。修改middleware.ts的中间件处理逻辑:
import { authMiddleware } from "@clerk/nextjs"; import createMiddleware from "next-intl/middleware"; const intlMiddleware = createMiddleware({ locales: ["en", "zh"], defaultLocale: "en", }); export default authMiddleware({ beforeAuth: (req) => { // 确保国际化中间件的响应正确传递,避免Edge Runtime异常 const intlResponse = intlMiddleware(req); return intlResponse; }, publicRoutes: ["/"], }); export const config = { matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"], };
2. 检查Vercel环境变量
Clerk在生产环境依赖完整的环境变量,确认Vercel项目的生产环境变量已正确配置:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYCLERK_SECRET_KEY- 若使用自定义路由,需补充
NEXT_PUBLIC_CLERK_SIGN_IN_URL、NEXT_PUBLIC_CLERK_SIGN_UP_URL等变量
3. 版本兼容性升级
Next.js 13.4.12属于较早的App Router版本,可能与最新依赖存在兼容问题:
- 升级Next.js到13.5.x或14.x稳定版
- 同步升级
@clerk/nextjs到对应兼容版本(参考Clerk官方版本适配说明)
4. 显式声明Edge Runtime
在middleware.ts顶部添加Edge Runtime声明,避免自动配置冲突:
export const runtime = "edge";
5. 查看Vercel崩溃日志
登录Vercel控制台,进入项目的Functions -> Edge Functions,查看具体的崩溃报错信息——日志会直接显示异常原因(比如未捕获错误、依赖缺失、环境变量未定义等),是定位问题最直接的方式。
内容的提问来源于stack exchange,提问作者AlexMoshi
相关产品推荐
相关产品推荐

