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

Full remaining path in selected layout segment #41562

Merged
merged 7 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 35 additions & 6 deletions packages/next/client/components/navigation.ts
@@ -1,5 +1,6 @@
// useLayoutSegments() // Only the segments for the current place. ['children', 'dashboard', 'children', 'integrations'] -> /dashboard/integrations (/dashboard/layout.js would get ['children', 'dashboard', 'children', 'integrations'])

import type { FlightRouterState } from '../../server/app-render'
import { useContext, useMemo } from 'react'
import {
SearchParamsContext,
Expand Down Expand Up @@ -105,16 +106,44 @@ export function usePathname(): string {
// return useContext(LayoutSegmentsContext)
// }

// TODO-APP: handle parallel routes
function getSelectedLayoutSegmentPath(
tree: FlightRouterState,
parallelRouteKey: string,
first = true,
segmentPath: string[] = []
): string[] {
let node: FlightRouterState
if (first) {
// Use the provdided parallel route key on the first parallel route
hanneslund marked this conversation as resolved.
Show resolved Hide resolved
node = tree[1][parallelRouteKey]
} else {
// After first parallel route prefer children, if there's no children pick the first parallel route.
const parallelRoutes = tree[1]
node = parallelRoutes.children ?? Object.values(parallelRoutes)[0]
}

const segment = node[0]
const segmentString = Array.isArray(segment) ? segment[1] : segment
hanneslund marked this conversation as resolved.
Show resolved Hide resolved
if (!segmentString) return segmentPath

segmentPath.push(segmentString)

return getSelectedLayoutSegmentPath(
node,
parallelRouteKey,
false,
segmentPath
)
}

// TODO-APP: Expand description when the docs are written for it.
/**
* Get the current segment one level down from the layout.
* Get the canonical segment path from this level to the leaf node.
*/
export function useSelectedLayoutSegment(
parallelRouteKey: string = 'children'
): string {
): string[] {
const { tree } = useContext(LayoutRouterContext)

const segment = tree[1][parallelRouteKey][0]

return Array.isArray(segment) ? segment[1] : segment
return getSelectedLayoutSegmentPath(tree, parallelRouteKey)
}
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
@@ -0,0 +1,14 @@
'use client'

import { useSelectedLayoutSegment } from 'next/navigation'

export default function Layout({ children }) {
const selectedLayoutSegment = useSelectedLayoutSegment()

return (
<>
<p id="inner-layout">{JSON.stringify(selectedLayoutSegment)}</p>
{children}
</>
)
}
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
@@ -0,0 +1,14 @@
'use client'

import { useSelectedLayoutSegment } from 'next/navigation'

export default function Layout({ children }) {
const selectedLayoutSegment = useSelectedLayoutSegment()

return (
<>
<p id="outer-layout">{JSON.stringify(selectedLayoutSegment)}</p>
{children}
</>
)
}
@@ -1,4 +1,3 @@
'use client'
// TODO-APP: enable once test is not skipped.
// import { useSelectedLayoutSegment } from 'next/navigation'

Expand Down
12 changes: 12 additions & 0 deletions test/e2e/app-dir/app/middleware.js
Expand Up @@ -14,6 +14,18 @@ export function middleware(request) {
return NextResponse.rewrite(new URL('/dashboard', request.url))
}

if (
request.nextUrl.pathname ===
'/hooks/use-selected-layout-segment/rewritten-middleware'
) {
return NextResponse.rewrite(
new URL(
'/hooks/use-selected-layout-segment/first/slug3/second/catch/all',
request.url
)
)
}

if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app/next.config.js
Expand Up @@ -12,6 +12,11 @@ module.exports = {
source: '/rewritten-to-dashboard',
destination: '/dashboard',
},
{
source: '/hooks/use-selected-layout-segment/rewritten',
destination:
'/hooks/use-selected-layout-segment/first/slug3/second/catch/all',
},
],
}
},
Expand Down
21 changes: 21 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -1194,6 +1194,27 @@ describe('app dir', () => {
expect(el.attr('data-query')).toBe('query')
})
})

describe('useSelectedLayoutSegment', () => {
test.each`
path | outerLayout | innerLayout
${'/hooks/use-selected-layout-segment/first'} | ${['first']} | ${[]}
hanneslund marked this conversation as resolved.
Show resolved Hide resolved
${'/hooks/use-selected-layout-segment/first/slug1'} | ${['first', 'slug1']} | ${['slug1']}
${'/hooks/use-selected-layout-segment/first/slug2/second'} | ${['first', 'slug2', '(group)', 'second']} | ${['slug2', '(group)', 'second']}
${'/hooks/use-selected-layout-segment/first/slug2/second/a/b'} | ${['first', 'slug2', '(group)', 'second', 'a/b']} | ${['slug2', '(group)', 'second', 'a/b']}
${'/hooks/use-selected-layout-segment/rewritten'} | ${['first', 'slug3', '(group)', 'second', 'catch/all']} | ${['slug3', '(group)', 'second', 'catch/all']}
${'/hooks/use-selected-layout-segment/rewritten-middleware'} | ${['first', 'slug3', '(group)', 'second', 'catch/all']} | ${['slug3', '(group)', 'second', 'catch/all']}
`(
'should have the correct layout segments at $path',
async ({ path, outerLayout, innerLayout }) => {
const html = await renderViaHTTP(next.url, path)
const $ = cheerio.load(html)

expect(JSON.parse($('#outer-layout').text())).toEqual(outerLayout)
expect(JSON.parse($('#inner-layout').text())).toEqual(innerLayout)
}
)
})
})

if (isDev) {
Expand Down