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,23 +598,24 @@ export async function renderToHTML( ...@@ -594,23 +598,24 @@ 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(
async (parallelRouteKey): Promise<[string, React.ReactNode]> => {
const currentSegmentPath = firstItem const currentSegmentPath = firstItem
? [currentValue] ? [parallelRouteKey]
: [actualSegment, currentValue] : [actualSegment, parallelRouteKey]
const { Component: ChildComponent } = createComponentTree({ const { Component: ChildComponent } = await createComponentTree({
createSegmentPath: (child) => { createSegmentPath: (child) => {
return createSegmentPath([...currentSegmentPath, ...child]) return createSegmentPath([...currentSegmentPath, ...child])
}, },
tree: parallelRoutes[currentValue], tree: parallelRoutes[parallelRouteKey],
parentParams: currentParams, parentParams: currentParams,
rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove, rootLayoutIncluded: rootLayoutIncludedAtThisLevelOrAbove,
}) })
const childSegmentParam = getDynamicParamFromSegment( const childSegmentParam = getDynamicParamFromSegment(
parallelRoutes[currentValue][0] parallelRoutes[parallelRouteKey][0]
) )
const childProp: ChildProp = { const childProp: ChildProp = {
current: <ChildComponent />, current: <ChildComponent />,
...@@ -620,19 +625,26 @@ export async function renderToHTML( ...@@ -620,19 +625,26 @@ export async function renderToHTML(
childSegmentParam.treeValue, childSegmentParam.treeValue,
childSegmentParam.type, childSegmentParam.type,
] ]
: parallelRoutes[currentValue][0], : parallelRoutes[parallelRouteKey][0],
} }
list[currentValue] = ( return [
parallelRouteKey,
<LayoutRouter <LayoutRouter
parallelRouterKey={currentValue} parallelRouterKey={parallelRouteKey}
segmentPath={createSegmentPath(currentSegmentPath)} segmentPath={createSegmentPath(currentSegmentPath)}
loading={Loading ? <Loading /> : undefined} loading={Loading ? <Loading /> : undefined}
childProp={childProp} childProp={childProp}
rootLayoutIncluded={rootLayoutIncludedAtThisLevelOrAbove} 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,7 +806,8 @@ export async function renderToHTML( ...@@ -794,7 +806,8 @@ export async function renderToHTML(
actualSegment, actualSegment,
createFlightRouterStateFromLoaderTree(treeToFilter), createFlightRouterStateFromLoaderTree(treeToFilter),
React.createElement( React.createElement(
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,
...@@ -802,6 +815,7 @@ export async function renderToHTML( ...@@ -802,6 +815,7 @@ export async function renderToHTML(
parentParams: currentParams, parentParams: currentParams,
firstItem: true, 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