chore: early return for next.js static assets (#19678)

This commit is contained in:
Benny Joo
2025-03-03 15:35:42 -03:00
committed by GitHub
parent bec83b2342
commit efd427831d
+11
View File
@@ -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<NextResponse<unknown>> => {
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);