Files
Joshua HannafordandGitHub a87a072109 fix(turbopack) Adjust the way catchall routes are handled when a more specific parallel route exists (#81634)
## Improve App Router Route Prioritization

### What?
Fixed a logic bug where we were failing to correctly identify routes as
catchall routes due to treating parallel routes as a completed route
path. This shouldn't happen because parallel routes do not affect the
routing structure.

### How?

- Modified the route selection logic to more clearly prioritize specific
routes over catch-all routes
- Make sure we only count true leaf nodes when determining when to use
the catch-all. Parallel is not a leaf node
- Added a test case to ensure middleware rewrites and redirects work
correctly with both parallel and non-parallel catch-all routes.

Fixes #PACK-4490
2025-07-17 17:22:47 -04:00

36 lines
1.1 KiB
JavaScript

import { NextResponse } from 'next/server'
export async function middleware(request) {
if (
request.nextUrl.pathname.includes('payment') &&
!request.nextUrl.pathname.includes('whoops')
) {
if (request.nextUrl.searchParams.has('redirect')) {
return NextResponse.redirect(new URL('/payment/whoops', request.url))
}
return NextResponse.rewrite(new URL('/payment/whoops', request.url))
}
if (
request.nextUrl.pathname.includes('anotherRoute') &&
!request.nextUrl.pathname.includes('whoops')
) {
if (request.nextUrl.searchParams.has('redirect')) {
return NextResponse.redirect(new URL('/anotherRoute/whoops', request.url))
}
return NextResponse.rewrite(new URL('/anotherRoute/whoops', request.url))
}
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*/
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
],
}