Next-Intl与Clerk v5集成问题:/api路由404及保护冲突
问题:Next.js集成next-intl与Clerk v5时/api路由返回404且中间件冲突
我在测试next-intl与Clerk v5的集成时,遇到了/api文件夹无法访问的问题,请求会返回404错误。看起来是在启用并保护/api路径时,next-intl和Clerk的中间件发生了冲突。
当前Middleware配置
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server"; import createMiddleware from "next-intl/middleware"; import { NextResponse } from "next/server"; import { defaultLocale, locales } from "./messages/config"; const intlMiddleware = createMiddleware({ locales: locales, defaultLocale: defaultLocale, alternateLinks: false, localePrefix: "as-needed", }); const isProtectedRoute = createRouteMatcher(["/:locale/(.*)", "/", "/api/:path*"]); export default clerkMiddleware((auth, req) => { const { pathname } = req.nextUrl; if (pathname.includes("/:locale/")) { const locale = pathname.split("/")[1]; const replacedPathname = pathname.replace("/:locale/", ""); return NextResponse.redirect( new URL(`/${(locales.includes(locale) ?? locale) || defaultLocale}/${replacedPathname}`, req.url), ); } if (isProtectedRoute(req)) { auth().protect(); } return intlMiddleware(req); }); export const config = { matcher: ["/((?!api/config/:path*).*)", "/((?!_next|_vercel|.*\\..*).*)"] };
文件夹结构

.env.local部分配置
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/:locale/sign-in NEXT_PUBLIC_CLERK_SIGN_UP_URL=/:locale/sign-up
我希望能配置匹配器,实现部分或全部/api路由的保护。按照指南操作后,不仅/api文件夹没被正常保护,还出现了404错误,恳请协助解决。
内容的提问来源于stack exchange,提问作者etrigo
相关产品推荐
相关产品推荐

