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

[ppr] Support fallback params for client layouts #70301

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/next/src/client/components/client-segment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use client'

type ClientSegmentRootProps = {
Component: React.ComponentType
props: { [props: string]: any }
}

export function ClientSegmentRoot({
Component,
props,
}: ClientSegmentRootProps) {
if (typeof window === 'undefined') {
const { createDynamicallyTrackedParams } =
require('./fallback-params') as typeof import('./fallback-params')

props.params = props.params
? createDynamicallyTrackedParams(props.params)
: {}
}
return <Component {...props} />
}
14 changes: 11 additions & 3 deletions packages/next/src/server/app-render/create-component-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ async function createComponentTreeInternal({
LayoutRouter,
RenderFromTemplateContext,
ClientPageRoot,
ClientSegmentRoot,
createUntrackedSearchParams,
createDynamicallyTrackedSearchParams,
createDynamicallyTrackedParams,
Expand Down Expand Up @@ -592,15 +593,22 @@ async function createComponentTreeInternal({
isStaticGeneration={isStaticGeneration}
ready={getMetadataReady}
>
{pageElement}
{layerAssets}
{pageElement}
</Segment>
</React.Fragment>,
parallelRouteCacheNodeSeedData,
loadingData,
]
} else {
props.params = createDynamicallyTrackedParams(currentParams)
let layoutElement: React.ReactNode
if (isClientComponent) {
props.params = currentParams
layoutElement = <ClientSegmentRoot props={props} Component={Component} />
} else {
props.params = createDynamicallyTrackedParams(currentParams)
layoutElement = <Component {...props} />
}

// For layouts we just render the component
return [
Expand All @@ -617,7 +625,7 @@ async function createComponentTreeInternal({
ready={getMetadataReady}
>
{layerAssets}
<Component {...props} />
{layoutElement}
</Segment>,
parallelRouteCacheNodeSeedData,
loadingData,
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/app-render/entry-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { requestAsyncStorage } from '../../client/components/request-async-stora
import { prerenderAsyncStorage } from './prerender-async-storage.external'
import { actionAsyncStorage } from '../../client/components/action-async-storage.external'
import { ClientPageRoot } from '../../client/components/client-page'
import { ClientSegmentRoot } from '../../client/components/client-segment'
import {
createUntrackedSearchParams,
createDynamicallyTrackedSearchParams,
Expand Down Expand Up @@ -61,6 +62,7 @@ export {
Postpone,
taintObjectReference,
ClientPageRoot,
ClientSegmentRoot,
NotFoundBoundary,
patchFetch,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Dynamic({ params }) {
return (
<div data-file="app/fallback/client/params/[slug]/dynamic">
{params.slug}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use client'

export default function Layout({ children }) {
return (
<div data-file="app/fallback/client/params/[slug]/layout">{children}</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function Loading() {
return (
<div data-file="app/fallback/client/params/[slug]/loading">Loading...</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Dynamic from './dynamic'

export default function Page({ params }) {
return (
<div data-file="app/fallback/client/params/[slug]/page">
<Dynamic params={params} />
</div>
)
}
45 changes: 33 additions & 12 deletions test/e2e/app-dir/ppr-full/ppr-full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe('ppr-full', () => {
for (const [pattern, client, nested] of patterns) {
let slug: string
if (nested) {
let slugs: string[] = []
const slugs: string[] = []
for (let j = i + 1; j < i + 2; j++) {
slugs.push(`slug-${pad(i)}/slug-${pad(j)}`)
}
Expand Down Expand Up @@ -313,7 +313,7 @@ describe('ppr-full', () => {
expect(streamEnd - start).toBeGreaterThanOrEqual(delay)

if (client) {
let browser = await next.browser(pathname)
const browser = await next.browser(pathname)
try {
await browser.waitForElementByCss('[data-slug]')
expect(
Expand Down Expand Up @@ -345,15 +345,15 @@ describe('ppr-full', () => {
describe('Dynamic Shell', () => {
it('should render the fallback shell on first visit', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = '/fallback/dynamic/params/on-first-visit-' + random
const pathname = `/fallback/dynamic/params/on-first-visit-${random}`
const $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
expect($('[data-agent]').closest('[hidden]').length).toBe(1)
})

it('should render the dynamic shell on the second visit', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = '/fallback/dynamic/params/on-second-visit-' + random
const pathname = `/fallback/dynamic/params/on-second-visit-${random}`

let $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
Expand All @@ -368,7 +368,7 @@ describe('ppr-full', () => {

it('should render the dynamic shell as static if the page is static', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = '/fallback/params/on-second-visit-' + random
const pathname = `/fallback/params/on-second-visit-${random}`

// Expect that the slug had to be resumed.
let $ = await next.render$(pathname)
Expand Down Expand Up @@ -402,7 +402,7 @@ describe('ppr-full', () => {

it('will only revalidate the page', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = '/fallback/dynamic/params/revalidate-' + random
const pathname = `/fallback/dynamic/params/revalidate-${random}`

let $ = await next.render$(pathname)
const fallbackID = $('[data-layout]').data('layout') as string
Expand All @@ -418,7 +418,7 @@ describe('ppr-full', () => {

// Now let's revalidate the page.
await next.fetch(
'/api/revalidate?pathname=' + encodeURIComponent(pathname)
`/api/revalidate?pathname=${encodeURIComponent(pathname)}`
)

// We expect to get the fallback shell again.
Expand All @@ -437,7 +437,7 @@ describe('ppr-full', () => {

it('will revalidate the page and fallback shell', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = '/fallback/dynamic/params/revalidate-' + random
const pathname = `/fallback/dynamic/params/revalidate-${random}`

let $ = await next.render$(pathname)
const fallbackID = $('[data-layout]').data('layout') as string
Expand All @@ -453,20 +453,41 @@ describe('ppr-full', () => {

// Now let's revalidate the page.
await next.fetch(
'/api/revalidate?pathname=/fallback/dynamic/params/[slug]'
`/api/revalidate?pathname=${encodeURIComponent(pathname)}`
)

// We expect to get a revalidated shell, not the same one as before.
// We expect to get the fallback shell.
$ = await next.render$(pathname)
expect($('[data-layout]').data('layout')).not.toBe(fallbackID)
expect($('[data-layout]').data('layout')).toBe(fallbackID)

// Let's wait for the page to be revalidated.
let revalidatedDynamicID: string
await retry(async () => {
$ = await next.render$(pathname)
expect($('[data-layout]').data('layout')).not.toBe(dynamicID)
revalidatedDynamicID = $('[data-layout]').data('layout') as string
expect(revalidatedDynamicID).not.toBe(dynamicID)
expect(revalidatedDynamicID).not.toBe(fallbackID)
})
})
})

it('should support client layouts', async () => {
const $ = await next.render$('/fallback/client/params/slug-01')

let selector = $(
'[data-file="app/fallback/client/params/[slug]/loading"]'
)
expect(selector.length).toBe(1)
expect(selector.closest('[hidden]').length).toBe(0)

selector = $('[data-file="app/fallback/client/params/[slug]/layout"]')
expect(selector.length).toBe(1)
expect(selector.closest('[hidden]').length).toBe(0)

selector = $('[data-file="app/fallback/client/params/[slug]/page"]')
expect(selector.length).toBe(1)
expect(selector.closest('[hidden]').length).toBe(1)
})
})
}

Expand Down
Loading