Unverified Commit 546f022f authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Remove segmentPath from RSC payload (#73827)

Based on:

- #73826 

---

This removes the `segmentPath` prop from the LayoutRouter payload. This
can be derived on the client from the FlightRouterState instead.
parent 48e9b8c9
...@@ -566,6 +566,7 @@ function Router({ ...@@ -566,6 +566,7 @@ function Router({
return { return {
parentTree: tree, parentTree: tree,
parentCacheNode: cache, parentCacheNode: cache,
parentSegmentPath: null,
// Root node always has `url` // Root node always has `url`
// Provided in AppTreeContext to ensure it can be overwritten in layout-router // Provided in AppTreeContext to ensure it can be overwritten in layout-router
url: canonicalUrl, url: canonicalUrl,
......
...@@ -402,6 +402,7 @@ function InnerLayoutRouter({ ...@@ -402,6 +402,7 @@ function InnerLayoutRouter({
value={{ value={{
parentTree: tree, parentTree: tree,
parentCacheNode: cacheNode, parentCacheNode: cacheNode,
parentSegmentPath: segmentPath,
// TODO-APP: overriding of url for parallel routes // TODO-APP: overriding of url for parallel routes
url: url, url: url,
...@@ -473,7 +474,6 @@ function LoadingBoundary({ ...@@ -473,7 +474,6 @@ function LoadingBoundary({
*/ */
export default function OuterLayoutRouter({ export default function OuterLayoutRouter({
parallelRouterKey, parallelRouterKey,
segmentPath,
error, error,
errorStyles, errorStyles,
errorScripts, errorScripts,
...@@ -485,7 +485,6 @@ export default function OuterLayoutRouter({ ...@@ -485,7 +485,6 @@ export default function OuterLayoutRouter({
unauthorized, unauthorized,
}: { }: {
parallelRouterKey: string parallelRouterKey: string
segmentPath: FlightSegmentPath
error: ErrorComponent | undefined error: ErrorComponent | undefined
errorStyles: React.ReactNode | undefined errorStyles: React.ReactNode | undefined
errorScripts: React.ReactNode | undefined errorScripts: React.ReactNode | undefined
...@@ -501,7 +500,7 @@ export default function OuterLayoutRouter({ ...@@ -501,7 +500,7 @@ export default function OuterLayoutRouter({
throw new Error('invariant expected layout router to be mounted') throw new Error('invariant expected layout router to be mounted')
} }
const { parentTree, parentCacheNode, url } = context const { parentTree, parentCacheNode, parentSegmentPath, url } = context
// Get the CacheNode for this segment by reading it from the parent segment's // Get the CacheNode for this segment by reading it from the parent segment's
// child map. // child map.
...@@ -516,9 +515,18 @@ export default function OuterLayoutRouter({ ...@@ -516,9 +515,18 @@ export default function OuterLayoutRouter({
// Get the active segment in the tree // Get the active segment in the tree
// The reason arrays are used in the data format is that these are transferred from the server to the browser so it's optimized to save bytes. // The reason arrays are used in the data format is that these are transferred from the server to the browser so it's optimized to save bytes.
const parentTreeSegment = parentTree[0]
const tree = parentTree[1][parallelRouterKey] const tree = parentTree[1][parallelRouterKey]
const treeSegment = tree[0] const treeSegment = tree[0]
const segmentPath =
parentSegmentPath === null
? // TODO: The root segment value is currently omitted from the segment
// path. This has led to a bunch of special cases scattered throughout
// the code. We should clean this up.
[parallelRouterKey]
: parentSegmentPath.concat([parentTreeSegment, parallelRouterKey])
// The "state" key of a segment is the one passed to React — it represents the // The "state" key of a segment is the one passed to React — it represents the
// identity of the UI tree. Whenever the state key changes, the tree is // identity of the UI tree. Whenever the state key changes, the tree is
// recreated and the state is reset. In the App Router model, search params do // recreated and the state is reset. In the App Router model, search params do
......
...@@ -2,7 +2,6 @@ import type { ...@@ -2,7 +2,6 @@ import type {
ActionResult, ActionResult,
DynamicParamTypesShort, DynamicParamTypesShort,
FlightRouterState, FlightRouterState,
FlightSegmentPath,
RenderOpts, RenderOpts,
Segment, Segment,
CacheNodeSeedData, CacheNodeSeedData,
...@@ -309,8 +308,6 @@ function createNotFoundLoaderTree(loaderTree: LoaderTree): LoaderTree { ...@@ -309,8 +308,6 @@ function createNotFoundLoaderTree(loaderTree: LoaderTree): LoaderTree {
] ]
} }
export type CreateSegmentPath = (child: FlightSegmentPath) => FlightSegmentPath
/** /**
* Returns a function that parses the dynamic segment and return the associated value. * Returns a function that parses the dynamic segment and return the associated value.
*/ */
...@@ -466,11 +463,9 @@ async function generateDynamicRSCPayload( ...@@ -466,11 +463,9 @@ async function generateDynamicRSCPayload(
flightData = ( flightData = (
await walkTreeWithFlightRouterState({ await walkTreeWithFlightRouterState({
ctx, ctx,
createSegmentPath: (child) => child,
loaderTreeToFilter: loaderTree, loaderTreeToFilter: loaderTree,
parentParams: {}, parentParams: {},
flightRouterState, flightRouterState,
isFirst: true,
// For flight, render metadata inside leaf page // For flight, render metadata inside leaf page
rscPayloadHead: ( rscPayloadHead: (
<React.Fragment key={flightDataPathHeadKey}> <React.Fragment key={flightDataPathHeadKey}>
...@@ -759,10 +754,8 @@ async function getRSCPayload( ...@@ -759,10 +754,8 @@ async function getRSCPayload(
const seedData = await createComponentTree({ const seedData = await createComponentTree({
ctx, ctx,
createSegmentPath: (child) => child,
loaderTree: tree, loaderTree: tree,
parentParams: {}, parentParams: {},
firstItem: true,
injectedCSS, injectedCSS,
injectedJS, injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
......
import type { import type { CacheNodeSeedData, PreloadCallbacks } from './types'
FlightSegmentPath,
CacheNodeSeedData,
PreloadCallbacks,
} from './types'
import React from 'react' import React from 'react'
import { isClientReference } from '../../lib/client-reference' import { isClientReference } from '../../lib/client-reference'
import { getLayoutOrPageModule } from '../lib/app-dir-module' import { getLayoutOrPageModule } from '../lib/app-dir-module'
import type { LoaderTree } from '../lib/app-dir-module' import type { LoaderTree } from '../lib/app-dir-module'
import { interopDefault } from './interop-default' import { interopDefault } from './interop-default'
import { parseLoaderTree } from './parse-loader-tree' import { parseLoaderTree } from './parse-loader-tree'
import type { CreateSegmentPath, AppRenderContext } from './app-render' import type { AppRenderContext } from './app-render'
import { createComponentStylesAndScripts } from './create-component-styles-and-scripts' import { createComponentStylesAndScripts } from './create-component-styles-and-scripts'
import { getLayerAssets } from './get-layer-assets' import { getLayerAssets } from './get-layer-assets'
import { hasLoadingComponentInTree } from './has-loading-component-in-tree' import { hasLoadingComponentInTree } from './has-loading-component-in-tree'
...@@ -27,11 +23,9 @@ import { OUTLET_BOUNDARY_NAME } from '../../lib/metadata/metadata-constants' ...@@ -27,11 +23,9 @@ import { OUTLET_BOUNDARY_NAME } from '../../lib/metadata/metadata-constants'
* Use the provided loader tree to create the React Component tree. * Use the provided loader tree to create the React Component tree.
*/ */
export function createComponentTree(props: { export function createComponentTree(props: {
createSegmentPath: CreateSegmentPath
loaderTree: LoaderTree loaderTree: LoaderTree
parentParams: Params parentParams: Params
rootLayoutIncluded: boolean rootLayoutIncluded: boolean
firstItem?: boolean
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string> injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
...@@ -63,10 +57,8 @@ function errorMissingDefaultExport( ...@@ -63,10 +57,8 @@ function errorMissingDefaultExport(
const cacheNodeKey = 'c' const cacheNodeKey = 'c'
async function createComponentTreeInternal({ async function createComponentTreeInternal({
createSegmentPath,
loaderTree: tree, loaderTree: tree,
parentParams, parentParams,
firstItem,
rootLayoutIncluded, rootLayoutIncluded,
injectedCSS, injectedCSS,
injectedJS, injectedJS,
...@@ -77,11 +69,9 @@ async function createComponentTreeInternal({ ...@@ -77,11 +69,9 @@ async function createComponentTreeInternal({
preloadCallbacks, preloadCallbacks,
authInterrupts, authInterrupts,
}: { }: {
createSegmentPath: CreateSegmentPath
loaderTree: LoaderTree loaderTree: LoaderTree
parentParams: Params parentParams: Params
rootLayoutIncluded: boolean rootLayoutIncluded: boolean
firstItem?: boolean
injectedCSS: Set<string> injectedCSS: Set<string>
injectedJS: Set<string> injectedJS: Set<string>
injectedFontPreloadTags: Set<string> injectedFontPreloadTags: Set<string>
...@@ -412,10 +402,6 @@ async function createComponentTreeInternal({ ...@@ -412,10 +402,6 @@ async function createComponentTreeInternal({
parallelRouteKey parallelRouteKey
): Promise<[string, React.ReactNode, CacheNodeSeedData | null]> => { ): Promise<[string, React.ReactNode, CacheNodeSeedData | null]> => {
const isChildrenRouteKey = parallelRouteKey === 'children' const isChildrenRouteKey = parallelRouteKey === 'children'
const currentSegmentPath: FlightSegmentPath = firstItem
? [parallelRouteKey]
: [actualSegment, parallelRouteKey]
const parallelRoute = parallelRoutes[parallelRouteKey] const parallelRoute = parallelRoutes[parallelRouteKey]
const notFoundComponent = const notFoundComponent =
...@@ -489,9 +475,6 @@ async function createComponentTreeInternal({ ...@@ -489,9 +475,6 @@ async function createComponentTreeInternal({
} }
const seedData = await createComponentTreeInternal({ const seedData = await createComponentTreeInternal({
createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child])
},
loaderTree: parallelRoute, loaderTree: parallelRoute,
parentParams: currentParams, parentParams: currentParams,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
...@@ -517,7 +500,6 @@ async function createComponentTreeInternal({ ...@@ -517,7 +500,6 @@ async function createComponentTreeInternal({
parallelRouteKey, parallelRouteKey,
<LayoutRouter <LayoutRouter
parallelRouterKey={parallelRouteKey} parallelRouterKey={parallelRouteKey}
segmentPath={createSegmentPath(currentSegmentPath)}
// TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning. // TODO-APP: Add test for loading returning `undefined`. This currently can't be tested as the `webdriver()` tab will wait for the full page to load before returning.
error={ErrorComponent} error={ErrorComponent}
errorStyles={errorStyles} errorStyles={errorStyles}
......
...@@ -2,7 +2,6 @@ import type { ...@@ -2,7 +2,6 @@ import type {
FlightDataPath, FlightDataPath,
FlightDataSegment, FlightDataSegment,
FlightRouterState, FlightRouterState,
FlightSegmentPath,
PreloadCallbacks, PreloadCallbacks,
Segment, Segment,
} from './types' } from './types'
...@@ -14,7 +13,7 @@ import type { LoaderTree } from '../lib/app-dir-module' ...@@ -14,7 +13,7 @@ import type { LoaderTree } from '../lib/app-dir-module'
import { getLinkAndScriptTags } from './get-css-inlined-link-tags' import { getLinkAndScriptTags } from './get-css-inlined-link-tags'
import { getPreloadableFonts } from './get-preloadable-fonts' import { getPreloadableFonts } from './get-preloadable-fonts'
import { createFlightRouterStateFromLoaderTree } from './create-flight-router-state-from-loader-tree' import { createFlightRouterStateFromLoaderTree } from './create-flight-router-state-from-loader-tree'
import type { CreateSegmentPath, AppRenderContext } from './app-render' import type { AppRenderContext } from './app-render'
import { hasLoadingComponentInTree } from './has-loading-component-in-tree' import { hasLoadingComponentInTree } from './has-loading-component-in-tree'
import { import {
DEFAULT_SEGMENT_KEY, DEFAULT_SEGMENT_KEY,
...@@ -27,10 +26,8 @@ import { createComponentTree } from './create-component-tree' ...@@ -27,10 +26,8 @@ import { createComponentTree } from './create-component-tree'
* This can either be the common layout between two pages or a specific place to start rendering from using the "refetch" marker in the tree. * This can either be the common layout between two pages or a specific place to start rendering from using the "refetch" marker in the tree.
*/ */
export async function walkTreeWithFlightRouterState({ export async function walkTreeWithFlightRouterState({
createSegmentPath,
loaderTreeToFilter, loaderTreeToFilter,
parentParams, parentParams,
isFirst,
flightRouterState, flightRouterState,
parentRendered, parentRendered,
rscPayloadHead, rscPayloadHead,
...@@ -42,10 +39,8 @@ export async function walkTreeWithFlightRouterState({ ...@@ -42,10 +39,8 @@ export async function walkTreeWithFlightRouterState({
ctx, ctx,
preloadCallbacks, preloadCallbacks,
}: { }: {
createSegmentPath: CreateSegmentPath
loaderTreeToFilter: LoaderTree loaderTreeToFilter: LoaderTree
parentParams: { [key: string]: string | string[] } parentParams: { [key: string]: string | string[] }
isFirst: boolean
flightRouterState?: FlightRouterState flightRouterState?: FlightRouterState
parentRendered?: boolean parentRendered?: boolean
rscPayloadHead: React.ReactNode rscPayloadHead: React.ReactNode
...@@ -159,10 +154,8 @@ export async function walkTreeWithFlightRouterState({ ...@@ -159,10 +154,8 @@ export async function walkTreeWithFlightRouterState({
// This ensures flightRouterPath is valid and filters down the tree // This ensures flightRouterPath is valid and filters down the tree
{ {
ctx, ctx,
createSegmentPath,
loaderTree: loaderTreeToFilter, loaderTree: loaderTreeToFilter,
parentParams: currentParams, parentParams: currentParams,
firstItem: isFirst,
injectedCSS, injectedCSS,
injectedJS, injectedJS,
injectedFontPreloadTags, injectedFontPreloadTags,
...@@ -216,21 +209,13 @@ export async function walkTreeWithFlightRouterState({ ...@@ -216,21 +209,13 @@ export async function walkTreeWithFlightRouterState({
for (const parallelRouteKey of parallelRoutesKeys) { for (const parallelRouteKey of parallelRoutesKeys) {
const parallelRoute = parallelRoutes[parallelRouteKey] const parallelRoute = parallelRoutes[parallelRouteKey]
const currentSegmentPath: FlightSegmentPath = isFirst
? [parallelRouteKey]
: [actualSegment, parallelRouteKey]
const subPaths = await walkTreeWithFlightRouterState({ const subPaths = await walkTreeWithFlightRouterState({
ctx, ctx,
createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child])
},
loaderTreeToFilter: parallelRoute, loaderTreeToFilter: parallelRoute,
parentParams: currentParams, parentParams: currentParams,
flightRouterState: flightRouterState:
flightRouterState && flightRouterState[1][parallelRouteKey], flightRouterState && flightRouterState[1][parallelRouteKey],
parentRendered: parentRendered || renderComponentsOnThisLevel, parentRendered: parentRendered || renderComponentsOnThisLevel,
isFirst: false,
rscPayloadHead, rscPayloadHead,
injectedCSS: injectedCSSWithCurrentLayout, injectedCSS: injectedCSSWithCurrentLayout,
injectedJS: injectedJSWithCurrentLayout, injectedJS: injectedJSWithCurrentLayout,
......
...@@ -6,7 +6,10 @@ import type { ...@@ -6,7 +6,10 @@ import type {
PrefetchKind, PrefetchKind,
RouterChangeByServerResponse, RouterChangeByServerResponse,
} from '../../client/components/router-reducer/router-reducer-types' } from '../../client/components/router-reducer/router-reducer-types'
import type { FlightRouterState } from '../../server/app-render/types' import type {
FlightRouterState,
FlightSegmentPath,
} from '../../server/app-render/types'
import React from 'react' import React from 'react'
export type ChildSegmentMap = Map<string, CacheNode> export type ChildSegmentMap = Map<string, CacheNode>
...@@ -148,6 +151,7 @@ export const AppRouterContext = React.createContext<AppRouterInstance | null>( ...@@ -148,6 +151,7 @@ export const AppRouterContext = React.createContext<AppRouterInstance | null>(
export const LayoutRouterContext = React.createContext<{ export const LayoutRouterContext = React.createContext<{
parentTree: FlightRouterState parentTree: FlightRouterState
parentCacheNode: CacheNode parentCacheNode: CacheNode
parentSegmentPath: FlightSegmentPath | null
url: string url: string
} | null>(null) } | null>(null)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment