Skip to content

Commit

Permalink
checks if appPath ends in a dynamic route, preventing catchall from t…
Browse files Browse the repository at this point in the history
…aking control if true
  • Loading branch information
williamli committed Jan 17, 2024
1 parent 16d009e commit c4c91a9
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/next/src/build/normalize-catchall-routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isInterceptionRouteAppPath } from '../server/future/helpers/interception-routes'
import { AppPathnameNormalizer } from '../server/future/normalizers/built/app/app-pathname-normalizer'
import { isDynamicRoute } from '../shared/lib/router/utils'

/**
* This function will transform the appPaths in order to support catch-all routes and parallel routes.
Expand All @@ -12,6 +13,7 @@ export function normalizeCatchAllRoutes(
appPaths: Record<string, string[]>,
normalizer = new AppPathnameNormalizer()
) {
console.log({ appPaths })
const catchAllRoutes = [
...new Set(
Object.values(appPaths)
Expand All @@ -31,6 +33,7 @@ export function normalizeCatchAllRoutes(

for (const appPath of filteredAppPaths) {
for (const catchAllRoute of catchAllRoutes) {
console.log(`${appPath} vs ${catchAllRoute}`)
const normalizedCatchAllRoute = normalizer.normalize(catchAllRoute)
const normalizedCatchAllRouteBasePath = normalizedCatchAllRoute.slice(
0,
Expand All @@ -40,22 +43,33 @@ export function normalizeCatchAllRoutes(
// check if the appPath could match the catch-all
appPath.startsWith(normalizedCatchAllRouteBasePath) &&
// check if there's not already a slot value that could match the catch-all
!appPaths[appPath].some((path) =>
hasMatchedSlots(path, catchAllRoute)
) &&
// check if the catch-all is not already matched by a default route
!appPaths[`${appPath}/default`]
!appPaths[appPath].some((path) => {
const output = hasMatchedSlots(path, catchAllRoute)
console.log(`hasMatchedSlots: ${path} - ${catchAllRoute}: ${output}`)
return output
}) &&
// check if the catch-all is not already matched by a default route or page route
!appPaths[`${appPath}/default`] &&
// check if the appPath's end with a dynamic route
!endsInDynamic(appPath)
) {
console.log('✅')
appPaths[appPath].push(catchAllRoute)
} else {
console.log('❌')
}
}
}

console.log({ normalizedPaths: appPaths })
}

function hasMatchedSlots(path1: string, path2: string): boolean {
const slots1 = path1.split('/').filter(isMatchableSlot)
const slots2 = path2.split('/').filter(isMatchableSlot)

console.log({ slots1, slots2 })

// if the catch-all route does not have the same number of slots as the app path, it can't match
if (slots1.length !== slots2.length) return false

Expand All @@ -81,3 +95,8 @@ const catchAllRouteRegex = /\[?\[\.\.\./
function isCatchAllRoute(pathname: string): boolean {
return pathname.includes('[...') || pathname.includes('[[...')
}

function endsInDynamic(pathname: string): boolean {
const pathnameParts = pathname.split('/')
return isDynamicRoute(`/${pathnameParts[pathnameParts.length - 1]}`)
}

0 comments on commit c4c91a9

Please sign in to comment.