Skip to content

Commit

Permalink
add layerAssets property to FlightDataPath
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed May 29, 2024
1 parent b5ab9ab commit 50d59d1
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 37 deletions.
6 changes: 6 additions & 0 deletions packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type AppRouterProps = Omit<
> & {
buildId: string
initialHead: ReactNode
initialLayerAssets: ReactNode
assetPrefix: string
missingSlots: Set<string>
}
Expand Down Expand Up @@ -183,6 +184,7 @@ export function createEmptyCacheNode(): CacheNode {
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
Expand Down Expand Up @@ -290,6 +292,7 @@ function Head({
function Router({
buildId,
initialHead,
initialLayerAssets,
initialTree,
initialCanonicalUrl,
initialSeedData,
Expand All @@ -307,6 +310,7 @@ function Router({
initialParallelRoutes,
location: !isServer ? window.location : null,
initialHead,
initialLayerAssets,
couldBeIntercepted,
}),
[
Expand All @@ -315,6 +319,7 @@ function Router({
initialCanonicalUrl,
initialTree,
initialHead,
initialLayerAssets,
couldBeIntercepted,
]
)
Expand Down Expand Up @@ -656,6 +661,7 @@ function Router({
let content = (
<RedirectBoundary>
{head}
{/* {cache.layerAssets} */}
{cache.rsc}
<AppRouterAnnouncer tree={tree} />
</RedirectBoundary>
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/client/components/layout-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ function InnerLayoutRouter({
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
Expand Down Expand Up @@ -466,6 +467,7 @@ function InnerLayoutRouter({
loading: childNode.loading,
}}
>
{/* {childNode.layerAssets} */}
{resolvedRsc}
</LayoutRouterContext.Provider>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ export function applyFlightData(
prefetchEntry?: PrefetchCacheEntry
): boolean {
// The one before last item is the router state tree patch
const [treePatch, cacheNodeSeedData, head] = flightDataPath.slice(-3)
const [treePatch, cacheNodeSeedData, head, layerAssets] =
flightDataPath.slice(-4)

// Handles case where prefetch only returns the router tree patch without rendered components.
if (cacheNodeSeedData === null) {
return false
}

if (flightDataPath.length === 3) {
if (flightDataPath.length === 4) {
const rsc = cacheNodeSeedData[2]
const loading = cacheNodeSeedData[3]
cache.loading = loading
Expand All @@ -35,6 +36,7 @@ export function applyFlightData(
treePatch,
cacheNodeSeedData,
head,
layerAssets,
prefetchEntry
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
Expand All @@ -27,6 +28,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
rsc: <>Root layout</>,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand All @@ -42,6 +44,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
prefetchRsc: null,
lazyDataResolved: false,
head: null,
layerAssets: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
Expand All @@ -55,6 +58,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
rsc: <>Page</>,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
Expand All @@ -76,20 +80,23 @@ describe('clearCacheNodeDataForSegmentPath', () => {
expect(cache).toMatchInlineSnapshot(`
{
"head": null,
"layerAssets": null,
"lazyData": null,
"lazyDataResolved": false,
"loading": null,
"parallelRoutes": Map {
"children" => Map {
"linking" => {
"head": null,
"layerAssets": null,
"lazyData": null,
"lazyDataResolved": false,
"loading": null,
"parallelRoutes": Map {
"children" => Map {
"" => {
"head": null,
"layerAssets": null,
"lazyData": null,
"lazyDataResolved": false,
"loading": null,
Expand All @@ -110,6 +117,7 @@ describe('clearCacheNodeDataForSegmentPath', () => {
},
"dashboard" => {
"head": null,
"layerAssets": null,
"lazyData": null,
"lazyDataResolved": false,
"loading": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function clearCacheNodeDataForSegmentPath(
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
Expand All @@ -57,6 +58,7 @@ export function clearCacheNodeDataForSegmentPath(
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
parallelRoutes: new Map(),
lazyDataResolved: false,
Expand All @@ -72,6 +74,7 @@ export function clearCacheNodeDataForSegmentPath(
rsc: childCacheNode.rsc,
prefetchRsc: childCacheNode.prefetchRsc,
head: childCacheNode.head,
layerAssets: childCacheNode.layerAssets,
prefetchHead: childCacheNode.prefetchHead,
parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: childCacheNode.lazyDataResolved,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('createInitialRouterState', () => {
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>,
initialLayerAssets: null,
couldBeIntercepted: false,
})

Expand All @@ -52,13 +53,15 @@ describe('createInitialRouterState', () => {
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>,
initialLayerAssets: null,
})

const expectedCache: CacheNode = {
lazyData: null,
rsc: children,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand All @@ -82,6 +85,7 @@ describe('createInitialRouterState', () => {
parallelRoutes: new Map(),
loading: null,
head: <title>Test</title>,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
},
Expand All @@ -93,6 +97,7 @@ describe('createInitialRouterState', () => {
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface InitialRouterStateParameters {
initialParallelRoutes: CacheNode['parallelRoutes']
location: Location | null
initialHead: ReactNode
initialLayerAssets: ReactNode
couldBeIntercepted?: boolean
}

Expand All @@ -32,6 +33,7 @@ export function createInitialRouterState({
initialParallelRoutes,
location,
initialHead,
initialLayerAssets,
couldBeIntercepted,
}: InitialRouterStateParameters) {
const isServer = !location
Expand All @@ -42,6 +44,7 @@ export function createInitialRouterState({
rsc: rsc,
prefetchRsc: null,
head: null,
layerAssets: initialLayerAssets,
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: isServer ? new Map() : initialParallelRoutes,
Expand All @@ -68,7 +71,8 @@ export function createInitialRouterState({
undefined,
initialTree,
initialSeedData,
initialHead
initialHead,
initialLayerAssets
)
}

Expand Down Expand Up @@ -106,7 +110,7 @@ export function createInitialRouterState({
location.origin
)

const initialFlightData: FlightData = [['', initialTree, null, null]]
const initialFlightData: FlightData = [['', initialTree, null, null, null]]
createPrefetchCacheEntryForInitialLoad({
url,
kind: PrefetchKind.AUTO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map(),
Expand All @@ -41,6 +42,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: <>Root layout</>,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand All @@ -55,6 +57,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: <>Linking</>,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand All @@ -69,6 +72,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: <>Page</>,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand Down Expand Up @@ -101,6 +105,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: null,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand All @@ -115,6 +120,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: <>Linking</>,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand All @@ -130,6 +136,7 @@ describe('fillCacheWithNewSubtreeData', () => {
rsc: <>Page</>,
prefetchRsc: null,
head: null,
layerAssets: null,
prefetchHead: null,
lazyDataResolved: false,
loading: null,
Expand All @@ -142,6 +149,7 @@ describe('fillCacheWithNewSubtreeData', () => {
lazyData: null,
lazyDataResolved: false,
head: null,
layerAssets: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function fillCacheWithNewSubTreeData(
flightDataPath: FlightDataPath,
prefetchEntry?: PrefetchCacheEntry
): void {
const isLastEntry = flightDataPath.length <= 5
const isLastEntry = flightDataPath.length <= 6
const [parallelRouteKey, segment] = flightDataPath

const cacheKey = createRouterCacheKey(segment)
Expand Down Expand Up @@ -47,13 +47,15 @@ export function fillCacheWithNewSubTreeData(
childCacheNode === existingChildCacheNode
) {
const seedData: CacheNodeSeedData = flightDataPath[3]
const layerAssets = flightDataPath[5]
const rsc = seedData[2]
const loading = seedData[3]
childCacheNode = {
lazyData: null,
rsc,
prefetchRsc: null,
head: null,
layerAssets,
prefetchHead: null,
loading,
// Ensure segments other than the one we got data for are preserved.
Expand All @@ -77,6 +79,7 @@ export function fillCacheWithNewSubTreeData(
flightDataPath[2],
seedData,
flightDataPath[4],
flightDataPath[5],
prefetchEntry
)

Expand All @@ -97,6 +100,7 @@ export function fillCacheWithNewSubTreeData(
rsc: childCacheNode.rsc,
prefetchRsc: childCacheNode.prefetchRsc,
head: childCacheNode.head,
layerAssets: childCacheNode.layerAssets,
prefetchHead: childCacheNode.prefetchHead,
parallelRoutes: new Map(childCacheNode.parallelRoutes),
lazyDataResolved: false,
Expand Down
Loading

0 comments on commit 50d59d1

Please sign in to comment.