Skip to content

Commit 16d176e

Browse files
committed
Pass RouteTree into navigation function
RouteTree is the client version of FlightRouterState. It's the same representation of the route tree, but it structured for optimized lookups of the client cache. The plan is the make this the primary/only type used for dealing with routes on the client; FlightRouterState will be used a transport format only.
1 parent 8709644 commit 16d176e

File tree

10 files changed

+451
-346
lines changed

10 files changed

+451
-346
lines changed

packages/next/src/client/components/router-reducer/create-initial-router-state.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { extractPathFromFlightRouterState } from './compute-changed-path'
66
import type { AppRouterState } from './router-reducer-types'
77
import { getFlightDataPartsFromPath } from '../../flight-data-helpers'
88
import { createInitialCacheNodeForHydration } from './ppr-navigations'
9+
import { convertRootFlightRouterStateToRouteTree } from '../segment-cache/cache'
10+
import type { NormalizedSearch } from '../segment-cache/cache-key'
911

1012
export interface InitialRouterStateParameters {
1113
navigatedAt: number
@@ -44,11 +46,22 @@ export function createInitialRouterState({
4446
createHrefFromUrl(location)
4547
: initialCanonicalUrl
4648

49+
// Conver the initial FlightRouterState into the RouteTree type.
50+
// NOTE: The metadataVaryPath isn't used for anything currently because the
51+
// head is embedded into the CacheNode tree, but eventually we'll lift it out
52+
// and store it on the top-level state object.
53+
const acc = { metadataVaryPath: null }
54+
const initialRouteTree = convertRootFlightRouterStateToRouteTree(
55+
initialTree,
56+
initialRenderedSearch as NormalizedSearch,
57+
acc
58+
)
59+
4760
const initialState = {
4861
tree: initialTree,
4962
cache: createInitialCacheNodeForHydration(
5063
navigatedAt,
51-
initialTree,
64+
initialRouteTree,
5265
initialSeedData,
5366
initialHead
5467
),

packages/next/src/client/components/router-reducer/is-navigating-to-new-root-layout.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { FlightRouterState } from '../../../shared/lib/app-router-types'
2+
import type { RouteTree } from '../segment-cache/cache'
23

34
export function isNavigatingToNewRootLayout(
45
currentTree: FlightRouterState,
5-
nextTree: FlightRouterState
6+
nextTree: RouteTree
67
): boolean {
78
// Compare segments
89
const currentTreeSegment = currentTree[0]
9-
const nextTreeSegment = nextTree[0]
10+
const nextTreeSegment = nextTree.segment
1011

1112
// If any segment is different before we find the root layout, the root layout has changed.
1213
// E.g. /same/(group1)/layout.js -> /same/(group2)/layout.js
@@ -27,17 +28,22 @@ export function isNavigatingToNewRootLayout(
2728
// Current tree root layout found
2829
if (currentTree[4]) {
2930
// If the next tree doesn't have the root layout flag, it must have changed.
30-
return !nextTree[4]
31+
return !nextTree.isRootLayout
3132
}
3233
// Current tree didn't have its root layout here, must have changed.
33-
if (nextTree[4]) {
34+
if (nextTree.isRootLayout) {
3435
return true
3536
}
36-
// We can't assume it's `parallelRoutes.children` here in case the root layout is `app/@something/layout.js`
37-
// But it's not possible to be more than one parallelRoutes before the root layout is found
38-
// TODO-APP: change to traverse all parallel routes
39-
const currentTreeChild = Object.values(currentTree[1])[0]
40-
const nextTreeChild = Object.values(nextTree[1])[0]
41-
if (!currentTreeChild || !nextTreeChild) return true
42-
return isNavigatingToNewRootLayout(currentTreeChild, nextTreeChild)
37+
38+
const slots = nextTree.slots
39+
if (slots !== null) {
40+
for (const slot in slots) {
41+
const nextTreeChild = slots[slot]
42+
if (isNavigatingToNewRootLayout(currentTree, nextTreeChild)) {
43+
return true
44+
}
45+
}
46+
}
47+
48+
return false
4349
}

0 commit comments

Comments
 (0)