Unverified Commit 6dff8853 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

ensure component tree is only rendered once during SSG/revalidations (#67680)

### What
When statically generating a page, Next.js will attempt to render the
application tree twice, resulting in unexpected calls to upstream APIs
and also slowing down the application during build as it needs to make 2
passes for the same data.

### Why
Next.js currently calls React's `renderToReadableStream` API in two
spots: once for the HTML render, and again for the Flight render. This
is because we want to generate a `.rsc` output for statically generated
pages, so that on navigation, we don't need to call the server to
retrieve the flight payload.

### How
Rather than calling `renderToReadableStream` twice, this refactors the
approach to instead only perform it once. This introduces a new type to
describe the response from calls to `createFromFetch`: `RSCPayload`,
including a set of utils to access the relevant payload data, as it can
change depending on calling context.

- During the initial SSR response & when statically generating the
flight payload, this will be an object containing the top-level props
that are used to render `<AppRouter />`
- For dynamic pages, this will only return the diffed component tree,
which is computed by `walkTreeWithFlightRouterState` traversing the
provided `next-router-state-tree` (from the client) to determine where
to start rendering (**this is unchanged from today**)
- For server actions, this will return the action result, in addition to
the same payload as above (**this is also unchanged**)

Fixes #58736
Fixes #43254
Fixes #60562

Closes NDX-17
parent 36e8c2f4
......@@ -172,9 +172,9 @@ const initialServerResponse = createFromReadableStream(readable, {
})
function ServerRoot(): React.ReactNode {
const initialRSCPayload = use<InitialRSCPayload>(initialServerResponse)
const initialResponse = use<InitialRSCPayload>(initialServerResponse)
return <AppRouter initialRSCPayload={initialRSCPayload} />
return <AppRouter initialRSCPayload={initialResponse} />
}
const StrictModeIfEnabled = process.env.__NEXT_STRICT_MODE_APP
......
......@@ -214,20 +214,16 @@ function Router({
() =>
createInitialRouterState({
buildId: initialRSCPayload.b,
initialSeedData: initialRSCPayload.d,
initialFlightData: initialRSCPayload.f,
initialCanonicalUrl: initialRSCPayload.c,
initialTree: initialRSCPayload.t,
initialParallelRoutes,
location: !isServer ? window.location : null,
initialHead: initialRSCPayload.h,
couldBeIntercepted: initialRSCPayload.i,
}),
[
initialRSCPayload.b,
initialRSCPayload.c,
initialRSCPayload.t,
initialRSCPayload.d,
initialRSCPayload.h,
initialRSCPayload.f,
initialRSCPayload.i,
]
)
......
......@@ -35,23 +35,23 @@ describe('createInitialRouterState', () => {
const state = createInitialRouterState({
buildId,
initialTree,
initialFlightData: [
[initialTree, ['', {}, children, null], <title>Test</title>],
],
initialCanonicalUrl,
initialSeedData: ['', {}, children, null],
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>,
couldBeIntercepted: false,
})
const state2 = createInitialRouterState({
buildId,
initialTree,
initialFlightData: [
[initialTree, ['', {}, children, null], <title>Test</title>],
],
initialCanonicalUrl,
initialSeedData: ['', {}, children, null],
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
initialHead: <title>Test</title>,
})
const expectedCache: CacheNode = {
......
import type { ReactNode } from 'react'
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import type {
FlightRouterState,
CacheNodeSeedData,
FlightData,
} from '../../../server/app-render/types'
import type { FlightDataPath } from '../../../server/app-render/types'
import { createHrefFromUrl } from './create-href-from-url'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
......@@ -15,25 +10,24 @@ import { addRefreshMarkerToActiveParallelSegments } from './refetch-inactive-par
export interface InitialRouterStateParameters {
buildId: string
initialTree: FlightRouterState
initialCanonicalUrl: string
initialSeedData: CacheNodeSeedData
initialParallelRoutes: CacheNode['parallelRoutes']
initialFlightData: FlightDataPath[]
location: Location | null
initialHead: ReactNode
couldBeIntercepted?: boolean
}
export function createInitialRouterState({
buildId,
initialTree,
initialSeedData,
initialFlightData,
initialCanonicalUrl,
initialParallelRoutes,
location,
initialHead,
couldBeIntercepted,
}: InitialRouterStateParameters) {
// The initialFlightData is an array of FlightDataPath arrays.
// For the root render, there'll only be a top-level FlightDataPath array.
const [initialTree, initialSeedData, initialHead] = initialFlightData[0]
const isServer = !location
const rsc = initialSeedData[2]
......@@ -105,9 +99,6 @@ export function createInitialRouterState({
location.origin
)
const initialFlightData: FlightData = [
[initialTree, initialSeedData, initialHead],
]
createPrefetchCacheEntryForInitialLoad({
url,
kind: PrefetchKind.AUTO,
......
......@@ -14,7 +14,7 @@ const { createFromFetch } = (
import type {
FlightRouterState,
FlightData,
NextFlightResponse,
NavigationFlightResponse,
} from '../../../server/app-render/types'
import {
NEXT_ROUTER_PREFETCH_HEADER,
......@@ -183,14 +183,18 @@ export async function fetchServerResponse(
}
// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
const [buildIdFromResponse, flightData]: NextFlightResponse =
await createFromFetch(Promise.resolve(res), { callServer })
const response: NavigationFlightResponse = await createFromFetch(
Promise.resolve(res),
{
callServer,
}
)
if (buildId !== buildIdFromResponse) {
if (buildId !== response.b) {
return doMpaNavigation(res.url)
}
return [flightData, canonicalUrl, postponed, interception]
return [response.f, canonicalUrl, postponed, interception]
} catch (err) {
console.error(
`Failed to fetch RSC payload for ${url}. Falling back to browser navigation.`,
......
......@@ -120,19 +120,16 @@ async function fetchServerAction(
if (location) {
// if it was a redirection, then result is just a regular RSC payload
const [, actionFlightData] = (response as any) ?? []
return {
actionFlightData: actionFlightData,
actionFlightData: response.f,
redirectLocation,
revalidatedParts,
}
}
// otherwise it's a tuple of [actionResult, actionFlightData]
const [actionResult, [, actionFlightData]] = (response as any) ?? []
return {
actionResult,
actionFlightData,
actionResult: response.a,
actionFlightData: response.f,
redirectLocation,
revalidatedParts,
}
......
......@@ -117,15 +117,6 @@ export type FlightData = Array<FlightDataPath> | string
export type ActionResult = Promise<any>
// Response from `createFromFetch` for normal rendering
export type NextFlightResponse = [buildId: string, flightData: FlightData]
// Response from `createFromFetch` for server actions. Action's flight data can be null
export type ActionFlightResponse =
| [ActionResult, [buildId: string, flightData: FlightData | null]]
// This case happens when `redirect()` is called in a server action.
| NextFlightResponse
export type ServerOnInstrumentationRequestError = (
error: unknown,
// The request could be middleware, node server or web server request,
......@@ -212,14 +203,33 @@ export type InitialRSCPayload = {
c: string
/** couldBeIntercepted */
i: boolean
/** initialTree */
t: FlightRouterState
/** initialSeedData */
d: CacheNodeSeedData
/** initialHead */
h: React.ReactNode
/** initialFlightData */
f: FlightDataPath[]
/** missingSlots */
m: Set<string> | undefined
/** GlobalError */
G: React.ComponentType<any>
}
// Response from `createFromFetch` for normal rendering
export type NavigationFlightResponse = {
/** buildId */
b: string
/** flightData */
f: FlightData
}
// Response from `createFromFetch` for server actions. Action's flight data can be null
export type ActionFlightResponse = {
/** actionResult */
a: ActionResult
/** buildId */
b: string
/** flightData */
f: FlightData | null
}
export type RSCPayload =
| InitialRSCPayload
| NavigationFlightResponse
| ActionFlightResponse
......@@ -18,8 +18,8 @@ import {
} from './create-flight-router-state-from-loader-tree'
import type { CreateSegmentPath, AppRenderContext } from './app-render'
import { hasLoadingComponentInTree } from './has-loading-component-in-tree'
import { createComponentTree } from './create-component-tree'
import { DEFAULT_SEGMENT_KEY } from '../../shared/lib/segment'
import { createComponentTree } from './create-component-tree'
/**
* Use router state to decide at what common layout to render the page.
......
......@@ -53,13 +53,13 @@ describe('ppr-unstable-cache', () => {
env: { TEST_DATA_SERVER: `http://localhost:${port}/` },
})
expect(generations).toHaveLength(3)
expect(generations).toHaveLength(2)
const first = await next
.render$('/')
.then(($) => JSON.parse($('#data').text()))
expect(generations).toHaveLength(3)
expect(generations).toHaveLength(2)
expect(first.data.forceCache).toBeOneOf(generations)
expect(first.data.noStore).toBeOneOf(generations)
......@@ -70,7 +70,7 @@ describe('ppr-unstable-cache', () => {
.render$('/')
.then(($) => JSON.parse($('#data').text()))
expect(generations).toHaveLength(3)
expect(generations).toHaveLength(2)
expect(first).toEqual(again)
}
......@@ -85,13 +85,13 @@ describe('ppr-unstable-cache', () => {
// Expect that the `cache: no-store` value has been updated, but not
// the `cache: force-cache` value.
expect(generations).toHaveLength(5)
expect(generations).toHaveLength(3)
// We know now that the generations have been updated, so let's try to
// validate the value. We don't need to do this within the retry.
expect(revalidated.random).not.toEqual(first.random)
expect(revalidated.data.forceCache).toBeOneOf(generations.slice(0, 3))
expect(revalidated.data.noStore).toBeOneOf(generations.slice(3))
expect(revalidated.data.forceCache).toBe(generations[1])
expect(revalidated.data.noStore).toBe(generations[2])
expect(revalidated).not.toEqual(first)
// Ensure that the `force-cache` value has not been updated, and only called
......
......@@ -444,7 +444,11 @@ describe('app dir - rsc basics', () => {
.then(async (response) => {
const result = await resolveStreamResponse(response)
expect(result).toContain('component:index.server')
expect(result).toMatch(isNextDev ? /0:\["development",/ : /0:\[".*?",/)
if (isNextDev) {
expect(result).toContain('"b":"development"')
} else {
expect(result).toMatch(/"b":".*?"/)
}
})
})
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export const revalidate = 1
export default async function Home() {
console.log('home page rendered')
const randomNumber = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random'
).then((res) => res.text())
return (
<div>
<p id="random-number">{randomNumber}</p>
</div>
)
}
import { NextResponse } from 'next/server'
import { revalidatePath } from 'next/cache'
export async function GET() {
revalidatePath('/')
return NextResponse.json({ success: true })
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('ssg-single-pass', () => {
const { next, skipped } = nextTestSetup({
skipDeployment: true,
files: __dirname,
})
if (skipped) {
return
}
it('should only render the page once during build', async () => {
expect(next.cliOutput).toIncludeRepeated('home page rendered', 1)
})
it('should only render the page once during an ISR revalidation', async () => {
let logOccurrences = next.cliOutput.split('home page rendered').length - 1
expect(logOccurrences).toBe(1)
logOccurrences = 0
const browser = await next.browser('/')
const initialRandomNumber = await browser
.elementById('random-number')
.text()
expect(initialRandomNumber).toMatch(/\d+/)
// grab the index of the last log message so we can start
// parsing future logs from there
const outputIndex = next.cliOutput.length
// trigger a revalidation
const response = await (await next.fetch('/revalidate')).json()
expect(response.success).toBe(true)
await retry(async () => {
// Retry until the data has been revalidated
browser.refresh()
const newRandomNumber = await browser.elementById('random-number').text()
expect(newRandomNumber).toMatch(/\d+/)
expect(newRandomNumber).not.toBe(initialRandomNumber)
expect(next.cliOutput.slice(outputIndex)).toIncludeRepeated(
'home page rendered',
1
)
})
})
})
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