Unverified Commit ed3b1271 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Create initial tree using same function as navigations (#87147)

Based on:

- #87128 

---

Small refactor. During the initial render to hydrate the page, we create
the CacheNode tree using the same function that is used to create new
trees during soft navigation.

Unlike during a soft navigation, we intentionally don't check if any
data needs to be fetched from the server. We assume the initial
hydration payload is sufficient to render the page.

The completeness of the initial data is an important property that we
rely on as a last-ditch mechanism for recovering the app; we must always
be able to reload a fresh HTML document to get to a consistent state.

This was one of the last remaining places where
fillLazyItemsTillLeafWithHead was used.
parent 6270c09c
......@@ -318,7 +318,6 @@ export async function hydrate(
initialFlightData: initialRSCPayload.f,
initialCanonicalUrlParts: initialRSCPayload.c,
initialRenderedSearch: initialRSCPayload.q,
initialParallelRoutes: new Map(),
location: window.location,
}),
instrumentationHooks
......
import React from 'react'
import type {
FlightRouterState,
CacheNode,
} from '../../../shared/lib/app-router-types'
import { createInitialRouterState } from './create-initial-router-state'
const getInitialRouterStateTree = (): FlightRouterState => [
'',
{
children: [
'linking',
{
children: ['', {}],
},
],
},
undefined,
undefined,
true,
]
const navigatedAt = Date.now()
describe('createInitialRouterState', () => {
it('should return the correct initial router state', () => {
const initialTree = getInitialRouterStateTree()
const initialCanonicalUrl = '/linking'
const children = (
<html>
<head></head>
<body>Root layout</body>
</html>
)
const initialParallelRoutes: CacheNode['parallelRoutes'] = new Map()
const state = createInitialRouterState({
navigatedAt,
initialFlightData: [[initialTree, [children, {}, null]]],
initialCanonicalUrlParts: initialCanonicalUrl.split('/'),
initialRenderedSearch: '',
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
})
const state2 = createInitialRouterState({
navigatedAt,
initialFlightData: [[initialTree, [children, {}, null]]],
initialCanonicalUrlParts: initialCanonicalUrl.split('/'),
initialRenderedSearch: '',
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
})
const expectedCache: CacheNode = {
navigatedAt,
lazyData: null,
rsc: children,
prefetchRsc: null,
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'linking',
{
navigatedAt,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
navigatedAt,
lazyData: null,
rsc: null,
prefetchRsc: null,
parallelRoutes: new Map(),
loading: null,
head: null,
prefetchHead: null,
},
],
]),
],
]),
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
loading: null,
},
],
]),
],
]),
}
const expected: ReturnType<typeof createInitialRouterState> = {
tree: initialTree,
canonicalUrl: initialCanonicalUrl,
renderedSearch: '',
pushRef: {
pendingPush: false,
mpaNavigation: false,
preserveCustomHistoryState: true,
},
focusAndScrollRef: {
apply: false,
onlyHashChange: false,
hashFragment: null,
segmentPaths: [],
},
cache: expectedCache,
nextUrl: '/linking',
previousNextUrl: null,
debugInfo: null,
}
expect(state).toMatchObject(expected)
expect(state2).toMatchObject(expected)
})
})
import type {
CacheNode,
FlightDataPath,
} from '../../../shared/lib/app-router-types'
import type { FlightDataPath } from '../../../shared/lib/app-router-types'
import { createHrefFromUrl } from './create-href-from-url'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { extractPathFromFlightRouterState } from './compute-changed-path'
import type { AppRouterState } from './router-reducer-types'
import { getFlightDataPartsFromPath } from '../../flight-data-helpers'
import { createInitialCacheNodeForHydration } from './ppr-navigations'
export interface InitialRouterStateParameters {
navigatedAt: number
initialCanonicalUrlParts: string[]
initialRenderedSearch: string
initialParallelRoutes: CacheNode['parallelRoutes']
initialFlightData: FlightDataPath[]
location: Location | null
}
......@@ -24,7 +20,6 @@ export function createInitialRouterState({
initialFlightData,
initialCanonicalUrlParts,
initialRenderedSearch,
initialParallelRoutes,
location,
}: InitialRouterStateParameters): AppRouterState {
// When initialized on the server, the canonical URL is provided as an array of parts.
......@@ -40,20 +35,6 @@ export function createInitialRouterState({
} = normalizedFlightData
// For the SSR render, seed data should always be available (we only send back a `null` response
// in the case of a `loading` segment, pre-PPR.)
const rsc = initialSeedData?.[0]
const loading = initialSeedData?.[2] ?? null
const cache: CacheNode = {
lazyData: null,
rsc,
prefetchRsc: null,
head: null,
prefetchHead: null,
// The cache gets seeded during the first render. `initialParallelRoutes` ensures the cache from the first render is there during the second render.
parallelRoutes: initialParallelRoutes,
loading,
navigatedAt,
}
const canonicalUrl =
// location.href is read as the initial value for canonicalUrl in the browser
......@@ -63,21 +44,14 @@ export function createInitialRouterState({
createHrefFromUrl(location)
: initialCanonicalUrl
// When the cache hasn't been seeded yet we fill the cache with the head.
if (initialParallelRoutes === null || initialParallelRoutes.size === 0) {
fillLazyItemsTillLeafWithHead(
const initialState = {
tree: initialTree,
cache: createInitialCacheNodeForHydration(
navigatedAt,
cache,
undefined,
initialTree,
initialSeedData,
initialHead
)
}
const initialState = {
tree: initialTree,
cache,
),
pushRef: {
pendingPush: false,
mpaNavigation: false,
......
......@@ -536,6 +536,7 @@ async function navigateDynamicallyWithNoPrefetch(
case FreshnessPolicy.HistoryTraversal:
dynamicRequestTree = currentFlightRouterState
break
case FreshnessPolicy.Hydration: // <- shouldn't happen during client nav
case FreshnessPolicy.RefreshAll:
dynamicRequestTree = DynamicRequestTreeForEntireRoute
break
......
......@@ -1710,7 +1710,6 @@ function App<T>({
initialFlightData: response.f,
initialCanonicalUrlParts: response.c,
initialRenderedSearch: response.q,
initialParallelRoutes: new Map(),
// location is not initialized in the SSR render
// it's set to window.location during hydration
location: null,
......@@ -1774,7 +1773,6 @@ function ErrorApp<T>({
initialFlightData: response.f,
initialCanonicalUrlParts: response.c,
initialRenderedSearch: response.q,
initialParallelRoutes: new Map(),
// location is not initialized in the SSR render
// it's set to window.location during hydration
location: 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