Unverified Commit 5752d4a6 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Ensure module require is awaited in app-render (#38860)

Fixes a bug where `import { nanoid } from 'nanoid'` broke the tests. 



## 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 adf40a6f
...@@ -538,7 +538,7 @@ export async function renderToHTML( ...@@ -538,7 +538,7 @@ export async function renderToHTML(
return segmentTree return segmentTree
} }
const createComponentTree = ({ const createComponentTree = async ({
createSegmentPath, createSegmentPath,
tree: [segment, parallelRoutes, { layout, loading, page }], tree: [segment, parallelRoutes, { layout, loading, page }],
parentParams, parentParams,
...@@ -550,11 +550,15 @@ export async function renderToHTML( ...@@ -550,11 +550,15 @@ export async function renderToHTML(
parentParams: { [key: string]: any } parentParams: { [key: string]: any }
rootLayoutIncluded?: boolean rootLayoutIncluded?: boolean
firstItem?: boolean firstItem?: boolean
}): { Component: React.ComponentType } => { }): Promise<{ Component: React.ComponentType }> => {
const Loading = loading ? interopDefault(loading()) : undefined const Loading = loading ? await interopDefault(loading()) : undefined
const isLayout = typeof layout !== 'undefined' const isLayout = typeof layout !== 'undefined'
const isPage = typeof page !== 'undefined' const isPage = typeof page !== 'undefined'
const layoutOrPageMod = isLayout ? layout() : isPage ? page() : undefined const layoutOrPageMod = isLayout
? await layout()
: isPage
? await page()
: undefined
const rootLayoutAtThisLevel = isLayout && !rootLayoutIncluded const rootLayoutAtThisLevel = isLayout && !rootLayoutIncluded
const rootLayoutIncludedAtThisLevelOrAbove = const rootLayoutIncludedAtThisLevelOrAbove =
rootLayoutIncluded || rootLayoutAtThisLevel rootLayoutIncluded || rootLayoutAtThisLevel
...@@ -594,45 +598,53 @@ export async function renderToHTML( ...@@ -594,45 +598,53 @@ export async function renderToHTML(
: segment : 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 parallelRouteComponents = Object.keys(parallelRoutes).reduce( const parallelRouteMap = await Promise.all(
(list, currentValue) => { Object.keys(parallelRoutes).map(
const currentSegmentPath = firstItem async (parallelRouteKey): Promise<[string, React.ReactNode]> => {
? [currentValue] const currentSegmentPath = firstItem
: [actualSegment, currentValue] ? [parallelRouteKey]
: [actualSegment, parallelRouteKey]
const { Component: ChildComponent } = createComponentTree({
createSegmentPath: (child) => { const { Component: ChildComponent } = await createComponentTree({
return createSegmentPath([...currentSegmentPath, ...child]) createSegmentPath: (child) => {
}, return createSegmentPath([...currentSegmentPath, ...child])
tree: parallelRoutes[currentValue], },
parentParams: currentParams, tree: parallelRoutes[parallelRouteKey],
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, parentParams: currentParams,
}) rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
})
const childSegmentParam = getDynamicParamFromSegment(
parallelRoutes[currentValue][0] const childSegmentParam = getDynamicParamFromSegment(
) parallelRoutes[parallelRouteKey][0]
const childProp: ChildProp = { )
current: <ChildComponent />, const childProp: ChildProp = {
segment: childSegmentParam current: <ChildComponent />,
? [ segment: childSegmentParam
childSegmentParam.param, ? [
childSegmentParam.treeValue, childSegmentParam.param,
childSegmentParam.type, childSegmentParam.treeValue,
] childSegmentParam.type,
: parallelRoutes[currentValue][0], ]
} : parallelRoutes[parallelRouteKey][0],
}
list[currentValue] = ( return [
<LayoutRouter parallelRouteKey,
parallelRouterKey={currentValue} <LayoutRouter
segmentPath={createSegmentPath(currentSegmentPath)} parallelRouterKey={parallelRouteKey}
loading={Loading ? <Loading /> : undefined} segmentPath={createSegmentPath(currentSegmentPath)}
childProp={childProp} loading={Loading ? <Loading /> : undefined}
rootLayoutIncluded={rootLayoutIncludedAtThisLevelOrAbove} childProp={childProp}
/> rootLayoutIncluded={rootLayoutIncludedAtThisLevelOrAbove}
) />,
]
}
)
)
const parallelRouteComponents = parallelRouteMap.reduce(
(list, [parallelRouteKey, Comp]) => {
list[parallelRouteKey] = Comp
return list return list
}, },
{} as { [key: string]: React.ReactNode } {} as { [key: string]: React.ReactNode }
...@@ -759,12 +771,12 @@ export async function renderToHTML( ...@@ -759,12 +771,12 @@ export async function renderToHTML(
if (isFlight) { if (isFlight) {
// TODO-APP: throw on invalid flightRouterState // TODO-APP: throw on invalid flightRouterState
const walkTreeWithFlightRouterState = ( const walkTreeWithFlightRouterState = async (
treeToFilter: LoaderTree, treeToFilter: LoaderTree,
parentParams: { [key: string]: string | string[] }, parentParams: { [key: string]: string | string[] },
flightRouterState?: FlightRouterState, flightRouterState?: FlightRouterState,
parentRendered?: boolean parentRendered?: boolean
): FlightDataPath => { ): Promise<FlightDataPath> => {
const [segment, parallelRoutes] = treeToFilter const [segment, parallelRoutes] = treeToFilter
const parallelRoutesKeys = Object.keys(parallelRoutes) const parallelRoutesKeys = Object.keys(parallelRoutes)
...@@ -794,14 +806,16 @@ export async function renderToHTML( ...@@ -794,14 +806,16 @@ export async function renderToHTML(
actualSegment, actualSegment,
createFlightRouterStateFromLoaderTree(treeToFilter), createFlightRouterStateFromLoaderTree(treeToFilter),
React.createElement( React.createElement(
createComponentTree( (
// This ensures flightRouterPath is valid and filters down the tree await createComponentTree(
{ // This ensures flightRouterPath is valid and filters down the tree
createSegmentPath: (child) => child, {
tree: treeToFilter, createSegmentPath: (child) => child,
parentParams: currentParams, tree: treeToFilter,
firstItem: true, parentParams: currentParams,
} firstItem: true,
}
)
).Component ).Component
), ),
] ]
...@@ -809,7 +823,7 @@ export async function renderToHTML( ...@@ -809,7 +823,7 @@ export async function renderToHTML(
for (const parallelRouteKey of parallelRoutesKeys) { for (const parallelRouteKey of parallelRoutesKeys) {
const parallelRoute = parallelRoutes[parallelRouteKey] const parallelRoute = parallelRoutes[parallelRouteKey]
const path = walkTreeWithFlightRouterState( const path = await walkTreeWithFlightRouterState(
parallelRoute, parallelRoute,
currentParams, currentParams,
flightRouterState && flightRouterState[1][parallelRouteKey], flightRouterState && flightRouterState[1][parallelRouteKey],
...@@ -831,9 +845,9 @@ export async function renderToHTML( ...@@ -831,9 +845,9 @@ export async function renderToHTML(
) )
const flightData: FlightData = [ const flightData: FlightData = [
// TODO-APP: change walk to output without '' // TODO-APP: change walk to output without ''
walkTreeWithFlightRouterState(tree, {}, providedFlightRouterState).slice( (
1 await walkTreeWithFlightRouterState(tree, {}, providedFlightRouterState)
), ).slice(1),
] ]
return new RenderResult( return new RenderResult(
...@@ -858,7 +872,7 @@ export async function renderToHTML( ...@@ -858,7 +872,7 @@ export async function renderToHTML(
dev dev
) )
const { Component: ComponentTree } = createComponentTree({ const { Component: ComponentTree } = await createComponentTree({
createSegmentPath: (child) => child, createSegmentPath: (child) => child,
tree, tree,
parentParams: {}, parentParams: {},
......
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