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 incorrect optimistic tree when there are multiple parallel routes #48449

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ export function createOptimisticTree(

const segmentMatches =
existingSegment !== null && matchSegment(existingSegment, segment)
const shouldRefetchThisLevel = !flightRouterState || !segmentMatches

// if there are multiple parallel routes at this level, we need to refetch here
// to ensure we get the correct tree. This is because we don't know which
// parallel route will match the next segment.
const hasMultipleParallelRoutes =
Object.keys(existingParallelRoutes).length > 1
const shouldRefetchThisLevel =
!flightRouterState || !segmentMatches || hasMultipleParallelRoutes

let parallelRoutes: FlightRouterState[1] = {}
if (existingSegment !== null && segmentMatches) {
parallelRoutes = existingParallelRoutes
}

let childTree
if (!isLastSegment) {

// if there's multiple parallel routes at this level, we shouldn't create an
// optimistic tree for the next level because we don't know which one will
// match the next segment.
if (!isLastSegment && !hasMultipleParallelRoutes) {
const childItem = createOptimisticTree(
segments.slice(1),
parallelRoutes ? parallelRoutes.children : null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export default function Page() {
return <div id="default-parallel">default view for parallel</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export default function Page() {
return <div id="parallel-foo">parallel for foo</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => null
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'

export default function Layout({ children, parallel }) {
return (
<>
{children}
{parallel}
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Link from 'next/link'
import React from 'react'

export default function Layout() {
return (
<div>
<Link prefetch={false} href="/parallel-prefetch-false/foo">
link
</Link>
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ createNextDescribe(
'slot catchall'
)
})

it('should navigate with a link with prefetch=false', async () => {
const browser = await next.browser('/parallel-prefetch-false')

// check if the default view loads
await check(
() => browser.waitForElementByCss('#default-parallel').text(),
'default view for parallel'
)

// check that navigating to /foo re-renders the layout to display @parallel/foo
await check(
() =>
browser
.elementByCss('[href="/parallel-prefetch-false/foo"]')
.click()
.waitForElementByCss('#parallel-foo')
.text(),
'parallel for foo'
)
})
})

describe('route intercepting', () => {
Expand Down