Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parallel routes: fix catch-all slots being treated as optional catch-all #61174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions packages/next/src/build/normalize-catchall-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ describe('normalizeCatchallRoutes', () => {

// ensure values are correct after normalizing
expect(appPaths).toMatchObject({
'/': [
'/page',
'/@slot/[...catchAll]/page', // inserted
],
'/': ['/page'],
'/[...catchAll]': ['/[...catchAll]/page', '/@slot/[...catchAll]/page'],
'/bar': [
'/bar/page',
Expand All @@ -103,6 +100,29 @@ describe('normalizeCatchallRoutes', () => {
})
})

it('should only match optional catch-all paths to the "index" of a segment', () => {
const appPaths = {
'/': ['/page'],
'/[[...catchAll]]': ['/@slot/[[...catchAll]]/page'],
'/foo': ['/foo/page'],
'/foo/[[...catchAll]]': ['/foo/@slot/[[...catchAll]]/page'],
}

// normalize appPaths against catchAlls
normalizeCatchAllRoutes(appPaths)

// ensure values are correct after normalizing
expect(appPaths).toMatchObject({
'/': [
'/page',
'/@slot/[[...catchAll]]/page', // inserted
],
'/[[...catchAll]]': ['/@slot/[[...catchAll]]/page'],
'/foo': ['/foo/page', '/@slot/[[...catchAll]]/page'],
'/foo/[[...catchAll]]': ['/foo/@slot/[[...catchAll]]/page'],
})
})

it('should not add the catch-all route to segments that have a more specific default', () => {
const appPaths = {
'/': ['/page'],
Expand Down
22 changes: 20 additions & 2 deletions packages/next/src/build/normalize-catchall-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ export function normalizeCatchAllRoutes(
// check if appPath is a catch-all OR is not more specific than the catch-all
(isCatchAllRoute(appPath) || !isMoreSpecific(appPath, catchAllRoute))
) {
appPaths[appPath].push(catchAllRoute)
if (isOptionalCatchAll(catchAllRoute)) {
// optional catch-all routes should match both the root segment and any segment after it
// for example, `/[[...slug]]` should match `/` and `/foo` and `/foo/bar`
appPaths[appPath].push(catchAllRoute)
} else if (isCatchAll(catchAllRoute)) {
// regular catch-all (single bracket) should only match segments after it
// for example, `/[...slug]` should match `/foo` and `/foo/bar` but not `/`
if (normalizedCatchAllRouteBasePath !== appPath) {
appPaths[appPath].push(catchAllRoute)
}
}
}
}
}
Expand Down Expand Up @@ -82,7 +92,15 @@ function isMatchableSlot(segment: string): boolean {
const catchAllRouteRegex = /\[?\[\.\.\./

function isCatchAllRoute(pathname: string): boolean {
return pathname.includes('[...') || pathname.includes('[[...')
return isOptionalCatchAll(pathname) || isCatchAll(pathname)
}

function isOptionalCatchAll(pathname: string): boolean {
return pathname.includes('[[...')
}

function isCatchAll(pathname: string): boolean {
return pathname.includes('[...')
}

// test to see if a path is more specific than a catch-all route
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return '@slot default'
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ createNextDescribe(
({ next }) => {
it('should match correctly when defining an explicit page & slot', async () => {
const browser = await next.browser('/')
await check(() => browser.elementById('slot').text(), /slot catchall/)
await check(() => browser.elementById('slot').text(), /@slot default/)

await browser.elementByCss('[href="/foo"]').click()

Expand All @@ -21,7 +21,7 @@ createNextDescribe(

it('should match correctly when defining an explicit page but no slot', async () => {
const browser = await next.browser('/')
await check(() => browser.elementById('slot').text(), /slot catchall/)
await check(() => browser.elementById('slot').text(), /@slot default/)

await browser.elementByCss('[href="/bar"]').click()

Expand All @@ -37,11 +37,7 @@ createNextDescribe(

it('should match correctly when defining an explicit slot but no page', async () => {
const browser = await next.browser('/')
await check(() => browser.elementById('slot').text(), /slot catchall/)
await check(
() => browser.elementById('slot').text(),
/catchall slot client component/
)
await check(() => browser.elementById('slot').text(), /@slot default/)

await browser.elementByCss('[href="/baz"]').click()

Expand All @@ -53,11 +49,8 @@ createNextDescribe(

it('should match both the catch-all page & slot', async () => {
const browser = await next.browser('/')
await check(() => browser.elementById('slot').text(), /slot catchall/)
await check(
() => browser.elementById('slot').text(),
/catchall slot client component/
)
await check(() => browser.elementById('slot').text(), /@slot default/)

await browser.elementByCss('[href="/quux"]').click()

// quux doesn't have a page or slot defined. It should use the catch-all for both
Expand Down