Unverified Commit e2286eeb authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add additional comments to new router (#38986)

Expanded app-render.


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent 8c902b61
...@@ -41,6 +41,7 @@ function fetchFlight( ...@@ -41,6 +41,7 @@ function fetchFlight(
JSON.stringify(flightRouterState) JSON.stringify(flightRouterState)
) )
// TODO-APP: Verify that TransformStream is supported.
const { readable, writable } = new TransformStream() const { readable, writable } = new TransformStream()
fetch(flightUrl.toString()).then((res) => { fetch(flightUrl.toString()).then((res) => {
......
...@@ -12,16 +12,25 @@ import { ...@@ -12,16 +12,25 @@ import {
AppTreeContext, AppTreeContext,
} from '../../shared/lib/app-router-context' } from '../../shared/lib/app-router-context'
/**
* Get the current search params. For example useSearchParams() would return {"foo": "bar"} when ?foo=bar
*/
export function useSearchParams() { export function useSearchParams() {
return useContext(SearchParamsContext) return useContext(SearchParamsContext)
} }
/**
* Get an individual search param. For example useSearchParam("foo") would return "bar" when ?foo=bar
*/
export function useSearchParam(key: string): string | string[] { export function useSearchParam(key: string): string | string[] {
const params = useContext(SearchParamsContext) const params = useContext(SearchParamsContext)
return params[key] return params[key]
} }
// TODO-APP: Move the other router context over to this one // TODO-APP: Move the other router context over to this one
/**
* Get the router methods. For example router.push('/dashboard')
*/
export function useRouter(): import('../../shared/lib/app-router-context').AppRouterInstance { export function useRouter(): import('../../shared/lib/app-router-context').AppRouterInstance {
return useContext(AppRouterContext) return useContext(AppRouterContext)
} }
...@@ -31,6 +40,9 @@ export function useRouter(): import('../../shared/lib/app-router-context').AppRo ...@@ -31,6 +40,9 @@ export function useRouter(): import('../../shared/lib/app-router-context').AppRo
// return useContext(ParamsContext) // return useContext(ParamsContext)
// } // }
/**
* Get the current pathname. For example usePathname() on /dashboard?foo=bar would return "/dashboard"
*/
export function usePathname(): string { export function usePathname(): string {
return useContext(PathnameContext) return useContext(PathnameContext)
} }
...@@ -40,6 +52,10 @@ export function usePathname(): string { ...@@ -40,6 +52,10 @@ export function usePathname(): string {
// return useContext(LayoutSegmentsContext) // return useContext(LayoutSegmentsContext)
// } // }
// TODO-APP: Expand description when the docs are written for it.
/**
* Get the current segment one level down from the layout.
*/
export function useSelectedLayoutSegment( export function useSelectedLayoutSegment(
parallelRouteKey: string = 'children' parallelRouteKey: string = 'children'
): string { ): string {
......
...@@ -41,6 +41,9 @@ export type RenderOptsPartial = { ...@@ -41,6 +41,9 @@ export type RenderOptsPartial = {
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
/**
* Interop between "export default" and "module.exports".
*/
function interopDefault(mod: any) { function interopDefault(mod: any) {
return mod.default || mod return mod.default || mod
} }
...@@ -60,6 +63,9 @@ type Record = { ...@@ -60,6 +63,9 @@ type Record = {
value: any value: any
} }
/**
* Create data fetching record for Promise.
*/
function createRecordFromThenable(thenable: Promise<any>) { function createRecordFromThenable(thenable: Promise<any>) {
const record: Record = { const record: Record = {
status: RecordStatus.Pending, status: RecordStatus.Pending,
...@@ -84,6 +90,9 @@ function createRecordFromThenable(thenable: Promise<any>) { ...@@ -84,6 +90,9 @@ function createRecordFromThenable(thenable: Promise<any>) {
return record return record
} }
/**
* Read record value or throw Promise if it's not resolved yet.
*/
function readRecordValue(record: Record) { function readRecordValue(record: Record) {
if (record.status === RecordStatus.Resolved) { if (record.status === RecordStatus.Resolved) {
return record.value return record.value
...@@ -92,6 +101,10 @@ function readRecordValue(record: Record) { ...@@ -92,6 +101,10 @@ function readRecordValue(record: Record) {
} }
} }
/**
* Preload data fetching record before it is called during React rendering.
* If the record is already in the cache returns that record.
*/
function preloadDataFetchingRecord( function preloadDataFetchingRecord(
map: Map<string, Record>, map: Map<string, Record>,
key: string, key: string,
...@@ -108,6 +121,10 @@ function preloadDataFetchingRecord( ...@@ -108,6 +121,10 @@ function preloadDataFetchingRecord(
return record return record
} }
/**
* Render Flight stream.
* This is only used for renderToHTML, the Flight response does not need additional wrappers.
*/
function useFlightResponse( function useFlightResponse(
writable: WritableStream<Uint8Array>, writable: WritableStream<Uint8Array>,
cachePrefix: string, cachePrefix: string,
...@@ -158,10 +175,18 @@ function useFlightResponse( ...@@ -158,10 +175,18 @@ function useFlightResponse(
return entry return entry
} }
// Create the wrapper component for a Flight stream. /**
* Create a component that renders the Flight stream.
* This is only used for renderToHTML, the Flight response does not need additional wrappers.
*/
function createServerComponentRenderer( function createServerComponentRenderer(
ComponentToRender: React.ComponentType, ComponentToRender: React.ComponentType,
ComponentMod: any, ComponentMod: {
__next_app_webpack_require__?: any
__next_rsc__?: {
__webpack_require__?: any
}
},
{ {
cachePrefix, cachePrefix,
transformStream, transformStream,
...@@ -171,7 +196,9 @@ function createServerComponentRenderer( ...@@ -171,7 +196,9 @@ function createServerComponentRenderer(
cachePrefix: string cachePrefix: string
transformStream: TransformStream<Uint8Array, Uint8Array> transformStream: TransformStream<Uint8Array, Uint8Array>
serverComponentManifest: NonNullable<RenderOpts['serverComponentManifest']> serverComponentManifest: NonNullable<RenderOpts['serverComponentManifest']>
serverContexts: Array<[ServerContextName: string, JSONValue: any]> serverContexts: Array<
[ServerContextName: string, JSONValue: Object | number | string]
>
} }
) { ) {
// We need to expose the `__webpack_require__` API globally for // We need to expose the `__webpack_require__` API globally for
...@@ -180,7 +207,7 @@ function createServerComponentRenderer( ...@@ -180,7 +207,7 @@ function createServerComponentRenderer(
// @ts-ignore // @ts-ignore
globalThis.__next_require__ = globalThis.__next_require__ =
ComponentMod.__next_app_webpack_require__ || ComponentMod.__next_app_webpack_require__ ||
ComponentMod.__next_rsc__.__webpack_require__ ComponentMod.__next_rsc__?.__webpack_require__
// @ts-ignore // @ts-ignore
globalThis.__next_chunk_load__ = () => Promise.resolve() globalThis.__next_chunk_load__ = () => Promise.resolve()
...@@ -278,6 +305,7 @@ export type FlightDataPath = ...@@ -278,6 +305,7 @@ export type FlightDataPath =
parallelRoute: string, parallelRoute: string,
segment: Segment, segment: Segment,
parallelRoute: string, parallelRoute: string,
currentSegment: Segment,
tree: FlightRouterState, tree: FlightRouterState,
subTreeData: React.ReactNode subTreeData: React.ReactNode
] ]
...@@ -288,6 +316,9 @@ export type ChildProp = { ...@@ -288,6 +316,9 @@ export type ChildProp = {
segment: Segment segment: Segment
} }
/**
* Parse dynamic route segment to type of parameter
*/
function getSegmentParam(segment: string): { function getSegmentParam(segment: string): {
param: string param: string
type: DynamicParamTypes type: DynamicParamTypes
...@@ -316,6 +347,9 @@ function getSegmentParam(segment: string): { ...@@ -316,6 +347,9 @@ function getSegmentParam(segment: string): {
return null return null
} }
/**
* Get inline <link> tags based on __next_rsc_css__ manifest. Only used when rendering to HTML.
*/
function getCssInlinedLinkTags( function getCssInlinedLinkTags(
ComponentMod: any, ComponentMod: any,
serverComponentManifest: any serverComponentManifest: any
...@@ -336,7 +370,7 @@ function getCssInlinedLinkTags( ...@@ -336,7 +370,7 @@ function getCssInlinedLinkTags(
) )
} }
export async function renderToHTML( export async function renderToHTMLOrFlight(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
pathname: string, pathname: string,
...@@ -364,6 +398,7 @@ export async function renderToHTML( ...@@ -364,6 +398,7 @@ export async function renderToHTML(
const isFlight = query.__flight__ !== undefined const isFlight = query.__flight__ !== undefined
// Handle client-side navigation to pages directory
if (isFlight && isPagesDir) { if (isFlight && isPagesDir) {
stripInternalQueries(query) stripInternalQueries(query)
const search = stringifyQuery(query) const search = stringifyQuery(query)
...@@ -380,6 +415,9 @@ export async function renderToHTML( ...@@ -380,6 +415,9 @@ export async function renderToHTML(
// TODO-APP: verify the tree is valid // TODO-APP: verify the tree is valid
// TODO-APP: verify query param is single value (not an array) // TODO-APP: verify query param is single value (not an array)
// TODO-APP: verify tree can't grow out of control // TODO-APP: verify tree can't grow out of control
/**
* Router state provided from the client-side router. Used to handle rendering from the common layout down.
*/
const providedFlightRouterState: FlightRouterState = isFlight const providedFlightRouterState: FlightRouterState = isFlight
? query.__flight_router_state_tree__ ? query.__flight_router_state_tree__
? JSON.parse(query.__flight_router_state_tree__ as string) ? JSON.parse(query.__flight_router_state_tree__ as string)
...@@ -388,9 +426,7 @@ export async function renderToHTML( ...@@ -388,9 +426,7 @@ export async function renderToHTML(
stripInternalQueries(query) stripInternalQueries(query)
const hasConcurrentFeatures = !!runtime
const pageIsDynamic = isDynamicRoute(pathname) const pageIsDynamic = isDynamicRoute(pathname)
const LayoutRouter = const LayoutRouter =
ComponentMod.LayoutRouter as typeof import('../client/components/layout-router.client').default ComponentMod.LayoutRouter as typeof import('../client/components/layout-router.client').default
const HotReloader = ComponentMod.HotReloader as const HotReloader = ComponentMod.HotReloader as
...@@ -398,10 +434,14 @@ export async function renderToHTML( ...@@ -398,10 +434,14 @@ export async function renderToHTML(
| null | null
const headers = req.headers const headers = req.headers
// @ts-expect-error TODO-APP: fix type of req // TODO-APP: fix type of req
// @ts-expect-error
const cookies = req.cookies const cookies = req.cookies
const tree: LoaderTree = ComponentMod.tree /**
* The tree created in next-app-loader that holds component segments and modules
*/
const loaderTree: LoaderTree = ComponentMod.tree
// Reads of this are cached on the `req` object, so this should resolve // Reads of this are cached on the `req` object, so this should resolve
// instantly. There's no need to pass this data down from a previous // instantly. There's no need to pass this data down from a previous
...@@ -412,6 +452,11 @@ export async function renderToHTML( ...@@ -412,6 +452,11 @@ export async function renderToHTML(
(renderOpts as any).previewProps (renderOpts as any).previewProps
) )
const isPreview = previewData !== false const isPreview = previewData !== false
/**
* Server Context is specifically only available in Server Components.
* It has to hold values that can't change while rendering from the common layout down.
* An example of this would be that `headers` are available but `searchParams` are not because that'd mean we have to render from the root layout down on all requests.
*/
const serverContexts: Array<[string, any]> = [ const serverContexts: Array<[string, any]> = [
['WORKAROUND', null], // TODO-APP: First value has a bug currently where the value is not set on the second request: https://github.com/facebook/react/issues/24849 ['WORKAROUND', null], // TODO-APP: First value has a bug currently where the value is not set on the second request: https://github.com/facebook/react/issues/24849
['HeadersContext', headers], ['HeadersContext', headers],
...@@ -419,19 +464,28 @@ export async function renderToHTML( ...@@ -419,19 +464,28 @@ export async function renderToHTML(
['PreviewDataContext', previewData], ['PreviewDataContext', previewData],
] ]
/**
* Used to keep track of in-flight / resolved data fetching Promises.
*/
const dataCache = new Map<string, Record>() const dataCache = new Map<string, Record>()
type CreateSegmentPath = (child: FlightSegmentPath) => FlightSegmentPath type CreateSegmentPath = (child: FlightSegmentPath) => FlightSegmentPath
/**
* Dynamic parameters. E.g. when you visit `/dashboard/vercel` which is rendered by `/dashboard/[slug]` the value will be {"slug": "vercel"}.
*/
const pathParams = (renderOpts as any).params as ParsedUrlQuery const pathParams = (renderOpts as any).params as ParsedUrlQuery
/**
* Parse the dynamic segment and return the associated value.
*/
const getDynamicParamFromSegment = ( const getDynamicParamFromSegment = (
// [id] or [slug] // [slug] / [[slug]] / [...slug]
segment: string segment: string
): { ): {
param: string param: string
value: string | string[] | null value: string | string[] | null
treeValue: string treeSegment: Segment
type: DynamicParamTypesShort type: DynamicParamTypesShort
} | null => { } | null => {
const segmentParam = getSegmentParam(segment) const segmentParam = getSegmentParam(segment)
...@@ -443,22 +497,29 @@ export async function renderToHTML( ...@@ -443,22 +497,29 @@ export async function renderToHTML(
const value = pathParams[key] const value = pathParams[key]
if (!value) { if (!value) {
// Handle case where optional catchall does not have a value, e.g. `/dashboard/[...slug]` when requesting `/dashboard`
if (segmentParam.type === 'optional-catchall') { if (segmentParam.type === 'optional-catchall') {
const type = getShortDynamicParamType(segmentParam.type)
return { return {
param: key, param: key,
value: null, value: null,
type: getShortDynamicParamType(segmentParam.type), type: type,
treeValue: '', // This value always has to be a string.
treeSegment: [key, '', type],
} }
} }
return null return null
} }
const type = getShortDynamicParamType(segmentParam.type)
return { return {
param: key, param: key,
// The value that is passed to user code.
value: value, value: value,
treeValue: Array.isArray(value) ? value.join('/') : value, // The value that is rendered in the router tree.
type: getShortDynamicParamType(segmentParam.type), treeSegment: [key, Array.isArray(value) ? value.join('/') : value, type],
type: type,
} }
} }
...@@ -471,9 +532,7 @@ export async function renderToHTML( ...@@ -471,9 +532,7 @@ export async function renderToHTML(
const dynamicParam = getDynamicParamFromSegment(segment) const dynamicParam = getDynamicParamFromSegment(segment)
const segmentTree: FlightRouterState = [ const segmentTree: FlightRouterState = [
dynamicParam dynamicParam ? dynamicParam.treeSegment : segment,
? [dynamicParam.param, dynamicParam.treeValue, dynamicParam.type]
: segment,
{}, {},
] ]
...@@ -495,15 +554,18 @@ export async function renderToHTML( ...@@ -495,15 +554,18 @@ export async function renderToHTML(
return segmentTree return segmentTree
} }
/**
* Use the provided loader tree to create the React Component tree.
*/
const createComponentTree = async ({ const createComponentTree = async ({
createSegmentPath, createSegmentPath,
tree: [segment, parallelRoutes, { layout, loading, page }], loaderTree: [segment, parallelRoutes, { layout, loading, page }],
parentParams, parentParams,
firstItem, firstItem,
rootLayoutIncluded, rootLayoutIncluded,
}: { }: {
createSegmentPath: CreateSegmentPath createSegmentPath: CreateSegmentPath
tree: LoaderTree loaderTree: LoaderTree
parentParams: { [key: string]: any } parentParams: { [key: string]: any }
rootLayoutIncluded?: boolean rootLayoutIncluded?: boolean
firstItem?: boolean firstItem?: boolean
...@@ -516,10 +578,19 @@ export async function renderToHTML( ...@@ -516,10 +578,19 @@ export async function renderToHTML(
: isPage : isPage
? await page() ? await page()
: undefined : undefined
/**
* Checks if the current segment is a root layout.
*/
const rootLayoutAtThisLevel = isLayout && !rootLayoutIncluded const rootLayoutAtThisLevel = isLayout && !rootLayoutIncluded
/**
* Checks if the current segment or any level above it has a root layout.
*/
const rootLayoutIncludedAtThisLevelOrAbove = const rootLayoutIncludedAtThisLevelOrAbove =
rootLayoutIncluded || rootLayoutAtThisLevel rootLayoutIncluded || rootLayoutAtThisLevel
/**
* Check if the current layout/page is a client component
*/
const isClientComponentModule = const isClientComponentModule =
layoutOrPageMod && !layoutOrPageMod.hasOwnProperty('__next_rsc__') layoutOrPageMod && !layoutOrPageMod.hasOwnProperty('__next_rsc__')
...@@ -537,11 +608,18 @@ export async function renderToHTML( ...@@ -537,11 +608,18 @@ export async function renderToHTML(
} }
} }
/**
* The React Component to render.
*/
const Component = layoutOrPageMod const Component = layoutOrPageMod
? interopDefault(layoutOrPageMod) ? interopDefault(layoutOrPageMod)
: undefined : undefined
// Handle dynamic segment params.
const segmentParam = getDynamicParamFromSegment(segment) const segmentParam = getDynamicParamFromSegment(segment)
/**
* Create object holding the parent params and current params, this is passed to getServerSideProps and getStaticProps.
*/
const currentParams = const currentParams =
// Handle null case where dynamic param is optional // Handle null case where dynamic param is optional
segmentParam && segmentParam.value !== null segmentParam && segmentParam.value !== null
...@@ -549,42 +627,39 @@ export async function renderToHTML( ...@@ -549,42 +627,39 @@ export async function renderToHTML(
...parentParams, ...parentParams,
[segmentParam.param]: segmentParam.value, [segmentParam.param]: segmentParam.value,
} }
: parentParams : // Pass through parent params to children
const actualSegment = segmentParam parentParams
? [segmentParam.param, segmentParam.treeValue] // Resolve the segment param
: segment const actualSegment = segmentParam ? segmentParam.treeSegment : segment
// This happens outside of rendering in order to eagerly kick off data fetching for layouts / the page further down // This happens outside of rendering in order to eagerly kick off data fetching for layouts / the page further down
const parallelRouteMap = await Promise.all( const parallelRouteMap = await Promise.all(
Object.keys(parallelRoutes).map( Object.keys(parallelRoutes).map(
async (parallelRouteKey): Promise<[string, React.ReactNode]> => { async (parallelRouteKey): Promise<[string, React.ReactNode]> => {
const currentSegmentPath = firstItem const currentSegmentPath: FlightSegmentPath = firstItem
? [parallelRouteKey] ? [parallelRouteKey]
: [actualSegment, parallelRouteKey] : [actualSegment, parallelRouteKey]
// Create the child component
const { Component: ChildComponent } = await createComponentTree({ const { Component: ChildComponent } = await createComponentTree({
createSegmentPath: (child) => { createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child]) return createSegmentPath([...currentSegmentPath, ...child])
}, },
tree: parallelRoutes[parallelRouteKey], loaderTree: parallelRoutes[parallelRouteKey],
parentParams: currentParams, parentParams: currentParams,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
}) })
const childSegmentParam = getDynamicParamFromSegment( const childSegment = parallelRoutes[parallelRouteKey][0]
parallelRoutes[parallelRouteKey][0] const childSegmentParam = getDynamicParamFromSegment(childSegment)
)
const childProp: ChildProp = { const childProp: ChildProp = {
current: <ChildComponent />, current: <ChildComponent />,
segment: childSegmentParam segment: childSegmentParam
? [ ? childSegmentParam.treeSegment
childSegmentParam.param, : childSegment,
childSegmentParam.treeValue,
childSegmentParam.type,
]
: parallelRoutes[parallelRouteKey][0],
} }
// This is turned back into an object below.
return [ return [
parallelRouteKey, parallelRouteKey,
<LayoutRouter <LayoutRouter
...@@ -599,6 +674,7 @@ export async function renderToHTML( ...@@ -599,6 +674,7 @@ export async function renderToHTML(
) )
) )
// Convert the parallel route map into an object after all promises have been resolved.
const parallelRouteComponents = parallelRouteMap.reduce( const parallelRouteComponents = parallelRouteMap.reduce(
(list, [parallelRouteKey, Comp]) => { (list, [parallelRouteKey, Comp]) => {
list[parallelRouteKey] = Comp list[parallelRouteKey] = Comp
...@@ -607,7 +683,7 @@ export async function renderToHTML( ...@@ -607,7 +683,7 @@ export async function renderToHTML(
{} as { [key: string]: React.ReactNode } {} as { [key: string]: React.ReactNode }
) )
// When the segment does not have a layout/page we still have to add the layout router to ensure the path holds the loading component // When the segment does not have a layout or page we still have to add the layout router to ensure the path holds the loading component
if (!Component) { if (!Component) {
return { return {
Component: () => <>{parallelRouteComponents.children}</>, Component: () => <>{parallelRouteComponents.children}</>,
...@@ -692,9 +768,9 @@ export async function renderToHTML( ...@@ -692,9 +768,9 @@ export async function renderToHTML(
return { return {
Component: () => { Component: () => {
let props let props
if (fetcher) {
// The data fetching was kicked off before rendering (see above) // The data fetching was kicked off before rendering (see above)
// if the data was not resolved yet the layout rendering will be suspended // if the data was not resolved yet the layout rendering will be suspended
if (fetcher) {
const record = preloadDataFetchingRecord( const record = preloadDataFetchingRecord(
dataCache, dataCache,
dataCacheKey, dataCacheKey,
...@@ -726,17 +802,24 @@ export async function renderToHTML( ...@@ -726,17 +802,24 @@ export async function renderToHTML(
} }
} }
// Handle Flight render request. This is only used when client-side navigating. E.g. when you `router.push('/dashboard')` or `router.reload()`.
if (isFlight) { if (isFlight) {
// TODO-APP: throw on invalid flightRouterState // TODO-APP: throw on invalid flightRouterState
/**
* Use router state to decide at what common layout to render the page.
* This can either be the common layout between two pages or a specific place to start rendering from using the "refetch" marker in the tree.
*/
const walkTreeWithFlightRouterState = async ( const walkTreeWithFlightRouterState = async (
treeToFilter: LoaderTree, loaderTreeToFilter: LoaderTree,
parentParams: { [key: string]: string | string[] }, parentParams: { [key: string]: string | string[] },
flightRouterState?: FlightRouterState, flightRouterState?: FlightRouterState,
parentRendered?: boolean parentRendered?: boolean
): Promise<FlightDataPath> => { ): Promise<FlightDataPath> => {
const [segment, parallelRoutes] = treeToFilter const [segment, parallelRoutes] = loaderTreeToFilter
const parallelRoutesKeys = Object.keys(parallelRoutes) const parallelRoutesKeys = Object.keys(parallelRoutes)
// Because this function walks to a deeper point in the tree to start rendering we have to track the dynamic parameters up to the point where rendering starts
// That way even when rendering the subtree getServerSideProps/getStaticProps get the right parameters.
const segmentParam = getDynamicParamFromSegment(segment) const segmentParam = getDynamicParamFromSegment(segment)
const currentParams = const currentParams =
// Handle null case where dynamic param is optional // Handle null case where dynamic param is optional
...@@ -747,11 +830,16 @@ export async function renderToHTML( ...@@ -747,11 +830,16 @@ export async function renderToHTML(
} }
: parentParams : parentParams
const actualSegment: Segment = segmentParam const actualSegment: Segment = segmentParam
? [segmentParam.param, segmentParam.treeValue, segmentParam.type] ? segmentParam.treeSegment
: segment : segment
/**
* Decide if the current segment is where rendering has to start.
*/
const renderComponentsOnThisLevel = const renderComponentsOnThisLevel =
// No further router state available
!flightRouterState || !flightRouterState ||
// Segment in router state does not match current segment
!matchSegment(actualSegment, flightRouterState[0]) || !matchSegment(actualSegment, flightRouterState[0]) ||
// Last item in the tree // Last item in the tree
parallelRoutesKeys.length === 0 || parallelRoutesKeys.length === 0 ||
...@@ -761,14 +849,16 @@ export async function renderToHTML( ...@@ -761,14 +849,16 @@ export async function renderToHTML(
if (!parentRendered && renderComponentsOnThisLevel) { if (!parentRendered && renderComponentsOnThisLevel) {
return [ return [
actualSegment, actualSegment,
createFlightRouterStateFromLoaderTree(treeToFilter), // Create router state using the slice of the loaderTree
createFlightRouterStateFromLoaderTree(loaderTreeToFilter),
// Create component tree using the slice of the loaderTree
React.createElement( React.createElement(
( (
await createComponentTree( await createComponentTree(
// This ensures flightRouterPath is valid and filters down the tree // This ensures flightRouterPath is valid and filters down the tree
{ {
createSegmentPath: (child) => child, createSegmentPath: (child) => child,
tree: treeToFilter, loaderTree: loaderTreeToFilter,
parentParams: currentParams, parentParams: currentParams,
firstItem: true, firstItem: true,
} }
...@@ -778,6 +868,7 @@ export async function renderToHTML( ...@@ -778,6 +868,7 @@ export async function renderToHTML(
] ]
} }
// Walk through all parallel routes.
for (const parallelRouteKey of parallelRoutesKeys) { for (const parallelRouteKey of parallelRoutesKeys) {
const parallelRoute = parallelRoutes[parallelRouteKey] const parallelRoute = parallelRoutes[parallelRouteKey]
const path = await walkTreeWithFlightRouterState( const path = await walkTreeWithFlightRouterState(
...@@ -795,10 +886,16 @@ export async function renderToHTML( ...@@ -795,10 +886,16 @@ export async function renderToHTML(
return [actualSegment] return [actualSegment]
} }
// Flight data that is going to be passed to the browser.
// Currently a single item array but in the future multiple patches might be combined in a single request.
const flightData: FlightData = [ const flightData: FlightData = [
// TODO-APP: change walk to output without '' // TODO-APP: change walk to output without ''
( (
await walkTreeWithFlightRouterState(tree, {}, providedFlightRouterState) await walkTreeWithFlightRouterState(
loaderTree,
{},
providedFlightRouterState
)
).slice(1), ).slice(1),
] ]
...@@ -809,29 +906,40 @@ export async function renderToHTML( ...@@ -809,29 +906,40 @@ export async function renderToHTML(
) )
} }
const search = stringifyQuery(query) // Below this line is handling for rendering to HTML.
// TODO-APP: validate req.url as it gets passed to render.
const initialCanonicalUrl = req.url!
const initialTree = createFlightRouterStateFromLoaderTree(tree)
const initialStylesheets: string[] = getCssInlinedLinkTags(
ComponentMod,
serverComponentManifest
)
// Create full component tree from root to leaf.
const { Component: ComponentTree } = await createComponentTree({ const { Component: ComponentTree } = await createComponentTree({
createSegmentPath: (child) => child, createSegmentPath: (child) => child,
tree, loaderTree: loaderTree,
parentParams: {}, parentParams: {},
firstItem: true, firstItem: true,
}) })
// AppRouter is provided by next-app-loader
const AppRouter = const AppRouter =
ComponentMod.AppRouter as typeof import('../client/components/app-router.client').default ComponentMod.AppRouter as typeof import('../client/components/app-router.client').default
const WrappedComponentTreeWithRouter = () => { let serverComponentsInlinedTransformStream: TransformStream<
Uint8Array,
Uint8Array
> = new TransformStream()
// TODO-APP: validate req.url as it gets passed to render.
const initialCanonicalUrl = req.url!
const initialStylesheets: string[] = getCssInlinedLinkTags(
ComponentMod,
serverComponentManifest
)
/**
* A new React Component that renders the provided React Component
* using Flight which can then be rendered to HTML.
*/
const ServerComponentsRenderer = createServerComponentRenderer(
() => {
const initialTree = createFlightRouterStateFromLoaderTree(loaderTree)
return ( return (
<AppRouter <AppRouter
hotReloader={HotReloader && <HotReloader assetPrefix="" />} hotReloader={HotReloader && <HotReloader assetPrefix="" />}
...@@ -842,42 +950,30 @@ export async function renderToHTML( ...@@ -842,42 +950,30 @@ export async function renderToHTML(
<ComponentTree /> <ComponentTree />
</AppRouter> </AppRouter>
) )
} },
const bootstrapScripts = buildManifest.rootMainFiles.map(
(src) => '/_next/' + src
)
let serverComponentsInlinedTransformStream: TransformStream<
Uint8Array,
Uint8Array
> | null = null
serverComponentsInlinedTransformStream = new TransformStream()
const Component = createServerComponentRenderer(
WrappedComponentTreeWithRouter,
ComponentMod, ComponentMod,
{ {
cachePrefix: pathname + (search ? `?${search}` : ''), cachePrefix: initialCanonicalUrl,
transformStream: serverComponentsInlinedTransformStream, transformStream: serverComponentsInlinedTransformStream,
serverComponentManifest, serverComponentManifest,
serverContexts, serverContexts,
} }
) )
/**
* Style registry for styled-jsx
*/
const jsxStyleRegistry = createStyleRegistry() const jsxStyleRegistry = createStyleRegistry()
const styledJsxFlushEffect = () => { /**
* styled-jsx styles as React Component
*/
const styledJsxFlushEffect = (): React.ReactNode => {
const styles = jsxStyleRegistry.styles() const styles = jsxStyleRegistry.styles()
jsxStyleRegistry.flush() jsxStyleRegistry.flush()
return <>{styles}</> return <>{styles}</>
} }
const AppContainer = ({ children }: { children: JSX.Element }) => (
<StyleRegistry registry={jsxStyleRegistry}>{children}</StyleRegistry>
)
/** /**
* Rules of Static & Dynamic HTML: * Rules of Static & Dynamic HTML:
* *
...@@ -894,16 +990,19 @@ export async function renderToHTML( ...@@ -894,16 +990,19 @@ export async function renderToHTML(
const generateStaticHTML = supportsDynamicHTML !== true const generateStaticHTML = supportsDynamicHTML !== true
const bodyResult = async () => { const bodyResult = async () => {
const content = ( const content = (
<AppContainer> <StyleRegistry registry={jsxStyleRegistry}>
<Component /> <ServerComponentsRenderer />
</AppContainer> </StyleRegistry>
) )
const renderStream = await renderToInitialStream({ const renderStream = await renderToInitialStream({
ReactDOMServer, ReactDOMServer,
element: content, element: content,
streamOptions: { streamOptions: {
bootstrapScripts, // Include hydration scripts in the HTML
bootstrapScripts: buildManifest.rootMainFiles.map(
(src) => '/_next/' + src
),
}, },
}) })
...@@ -912,6 +1011,8 @@ export async function renderToHTML( ...@@ -912,6 +1011,8 @@ export async function renderToHTML(
return flushed return flushed
} }
const hasConcurrentFeatures = !!runtime
return await continueFromInitialStream(renderStream, { return await continueFromInitialStream(renderStream, {
dataStream: serverComponentsInlinedTransformStream?.readable, dataStream: serverComponentsInlinedTransformStream?.readable,
generateStaticHTML: generateStaticHTML || !hasConcurrentFeatures, generateStaticHTML: generateStaticHTML || !hasConcurrentFeatures,
......
...@@ -53,7 +53,7 @@ import { getExtension, serveStatic } from './serve-static' ...@@ -53,7 +53,7 @@ import { getExtension, serveStatic } from './serve-static'
import { ParsedUrlQuery } from 'querystring' import { ParsedUrlQuery } from 'querystring'
import { apiResolver } from './api-utils/node' import { apiResolver } from './api-utils/node'
import { RenderOpts, renderToHTML } from './render' import { RenderOpts, renderToHTML } from './render'
import { renderToHTML as appRenderToHTML } from './app-render' import { renderToHTMLOrFlight as appRenderToHTMLOrFlight } from './app-render'
import { ParsedUrl, parseUrl } from '../shared/lib/router/utils/parse-url' import { ParsedUrl, parseUrl } from '../shared/lib/router/utils/parse-url'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
...@@ -615,7 +615,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -615,7 +615,7 @@ export default class NextNodeServer extends BaseServer {
(renderOpts.isAppPath || query.__flight__) (renderOpts.isAppPath || query.__flight__)
) { ) {
const isPagesDir = !renderOpts.isAppPath const isPagesDir = !renderOpts.isAppPath
return appRenderToHTML( return appRenderToHTMLOrFlight(
req.originalRequest, req.originalRequest,
res.originalResponse, res.originalResponse,
pathname, pathname,
......
...@@ -3,14 +3,13 @@ import type { FocusRef } from '../../client/components/reducer' ...@@ -3,14 +3,13 @@ import type { FocusRef } from '../../client/components/reducer'
import type { FlightRouterState, FlightData } from '../../server/app-render' import type { FlightRouterState, FlightData } from '../../server/app-render'
export type ChildSegmentMap = Map<string, CacheNode> export type ChildSegmentMap = Map<string, CacheNode>
type ParallelRoutesCacheNodes = Map<string, ChildSegmentMap>
export type CacheNode = { export type CacheNode = {
data: ReturnType< data: ReturnType<
typeof import('../../client/components/app-router.client').fetchServerResponse typeof import('../../client/components/app-router.client').fetchServerResponse
> | null > | null
subTreeData: null | React.ReactNode subTreeData: null | React.ReactNode
parallelRoutes: ParallelRoutesCacheNodes parallelRoutes: Map<string, ChildSegmentMap>
} }
export type AppRouterInstance = { export type AppRouterInstance = {
......
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