Skip to content

Commit

Permalink
misc: split app-render into smaller functions (#56611)
Browse files Browse the repository at this point in the history
This PR splits apart the function used to render App Router pages into smaller chunks for better readability + testing. A lot of the complexity is tied by the fact that a lot of the code of the function relied on closures so I had to coalesce a lot of the captured variables used into one big context that is then passed around during render.

There are a lot of things to slim down further but I don't have the energy to dig more.
  • Loading branch information
feedthejim committed Oct 10, 2023
1 parent 56fe014 commit d4fcd03
Show file tree
Hide file tree
Showing 12 changed files with 1,324 additions and 1,143 deletions.
6 changes: 3 additions & 3 deletions packages/next/src/lib/metadata/metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export function createMetadataComponents({
pathname,
searchParams,
getDynamicParamFromSegment,
appUsingSizeAdjust,
appUsingSizeAdjustment,
errorType,
}: {
tree: LoaderTree
pathname: string
searchParams: { [key: string]: any }
getDynamicParamFromSegment: GetDynamicParamFromSegment
appUsingSizeAdjust: boolean
appUsingSizeAdjustment: boolean
errorType?: 'not-found' | 'redirect'
}): [React.ComponentType, React.ComponentType] {
const metadataContext = {
Expand Down Expand Up @@ -110,7 +110,7 @@ export function createMetadataComponents({
IconsMetadata({ icons: metadata.icons }),
])

if (appUsingSizeAdjust) elements.push(<meta name="next-size-adjust" />)
if (appUsingSizeAdjustment) elements.push(<meta name="next-size-adjust" />)

return (
<>
Expand Down
17 changes: 7 additions & 10 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
import RenderResult from '../render-result'
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
import { FlightRenderResult } from './flight-render-result'
import type { ActionResult } from './types'
import type { ActionAsyncStorage } from '../../client/components/action-async-storage.external'
import {
filterReqHeaders,
Expand All @@ -37,6 +36,7 @@ import {
NEXT_CACHE_REVALIDATED_TAGS_HEADER,
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER,
} from '../../lib/constants'
import type { AppRenderContext, GenerateFlight } from './app-render'

function nodeToWebReadableStream(nodeReadable: import('stream').Readable) {
if (process.env.NEXT_RUNTIME !== 'edge') {
Expand Down Expand Up @@ -246,21 +246,18 @@ export async function handleAction({
staticGenerationStore,
requestStore,
serverActionsBodySizeLimit,
ctx,
}: {
req: IncomingMessage
res: ServerResponse
ComponentMod: any
page: string
serverActionsManifest: any
generateFlight: (options: {
actionResult: ActionResult
formState?: any
skipFlight: boolean
asNotFound?: boolean
}) => Promise<RenderResult>
generateFlight: GenerateFlight
staticGenerationStore: StaticGenerationStore
requestStore: RequestStore
serverActionsBodySizeLimit?: SizeLimit
ctx: AppRenderContext
}): Promise<
| undefined
| {
Expand Down Expand Up @@ -466,7 +463,7 @@ export async function handleAction({
requestStore,
})

actionResult = await generateFlight({
actionResult = await generateFlight(ctx, {
actionResult: Promise.resolve(returnVal),
// if the page was not revalidated, we can skip the rendering the flight tree
skipFlight: !staticGenerationStore.pathWasRevalidated,
Expand Down Expand Up @@ -533,7 +530,7 @@ export async function handleAction({
} catch {}
return {
type: 'done',
result: await generateFlight({
result: await generateFlight(ctx, {
skipFlight: false,
actionResult: promise,
asNotFound: true,
Expand All @@ -555,7 +552,7 @@ export async function handleAction({

return {
type: 'done',
result: await generateFlight({
result: await generateFlight(ctx, {
actionResult: promise,
// if the page was not revalidated, we can skip the rendering the flight tree
skipFlight: !staticGenerationStore.pathWasRevalidated,
Expand Down

0 comments on commit d4fcd03

Please sign in to comment.