From efd427831d27bf74ad0053d5d8fb388dbc760bfb Mon Sep 17 00:00:00 2001 From: Benny Joo Date: Mon, 3 Mar 2025 13:35:42 -0500 Subject: [PATCH] chore: early return for next.js static assets (#19678) --- apps/web/middleware.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index 8d6d69f8f2..b7d5faf097 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -39,10 +39,21 @@ export function checkPostMethod(req: NextRequest) { return null; } +export function checkStaticFiles(pathname: string) { + const hasFileExtension = /\.(svg|png|jpg|jpeg|gif|webp|ico)$/.test(pathname); + // Skip Next.js internal paths (_next) and static assets + if (pathname.startsWith("/_next") || hasFileExtension) { + return NextResponse.next(); + } +} + const middleware = async (req: NextRequest): Promise> => { const postCheckResult = checkPostMethod(req); if (postCheckResult) return postCheckResult; + const isStaticFile = checkStaticFiles(req.nextUrl.pathname); + if (isStaticFile) return isStaticFile; + const url = req.nextUrl; const requestHeaders = new Headers(req.headers); requestHeaders.set("x-url", req.url);