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

Destructure loadComponents where possible (#82986)

## What?

More preparation work for removing manifest loading from loadComponents. No changes to how things function, only making it clearer what is used from loadComponents in each place it is used.
parent e0cf4fec
import type { LoadComponentsReturnType } from '../../../server/load-components'
import type { Params } from '../../../server/request/params' import type { Params } from '../../../server/request/params'
import type { import type { AppPageRouteModule } from '../../../server/route-modules/app-page/module.compiled'
AppPageRouteModule, import type { AppRouteRouteModule } from '../../../server/route-modules/app-route/module.compiled'
AppPageModule,
} from '../../../server/route-modules/app-page/module.compiled'
import type {
AppRouteRouteModule,
AppRouteModule,
} from '../../../server/route-modules/app-route/module.compiled'
import { import {
type AppSegmentConfig, type AppSegmentConfig,
parseAppSegmentConfig, parseAppSegmentConfig,
...@@ -25,6 +18,7 @@ import { ...@@ -25,6 +18,7 @@ import {
type LoaderTree, type LoaderTree,
} from '../../../server/lib/app-dir-module' } from '../../../server/lib/app-dir-module'
import { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment' import { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment'
import type { RouteModule } from '../../../server/route-modules/route-module'
type GenerateStaticParams = (options: { params?: Params }) => Promise<Params[]> type GenerateStaticParams = (options: { params?: Params }) => Promise<Params[]>
...@@ -187,11 +181,9 @@ function collectAppRouteSegments( ...@@ -187,11 +181,9 @@ function collectAppRouteSegments(
* @param components the loaded components * @param components the loaded components
* @returns the segments for the route module * @returns the segments for the route module
*/ */
export function collectSegments({ export function collectSegments(
routeModule, routeModule: RouteModule
}: LoadComponentsReturnType<AppPageModule | AppRouteModule>): ): Promise<AppSegment[]> | AppSegment[] {
| Promise<AppSegment[]>
| AppSegment[] {
if (isAppRouteRouteModule(routeModule)) { if (isAppRouteRouteModule(routeModule)) {
return collectAppRouteSegments(routeModule) return collectAppRouteSegments(routeModule)
} }
......
import { getSegmentParam } from '../../../server/app-render/get-segment-param' import { getSegmentParam } from '../../../server/app-render/get-segment-param'
import type { LoadComponentsReturnType } from '../../../server/load-components'
import type { AppPageModule } from '../../../server/route-modules/app-page/module'
import type AppPageRouteModule from '../../../server/route-modules/app-page/module' import type AppPageRouteModule from '../../../server/route-modules/app-page/module'
import type { AppRouteModule } from '../../../server/route-modules/app-route/module'
import { import {
isAppPageRouteModule, isAppPageRouteModule,
isAppRouteRouteModule, isAppRouteRouteModule,
} from '../../../server/route-modules/checks' } from '../../../server/route-modules/checks'
import type { RouteModule } from '../../../server/route-modules/route-module'
import { InvariantError } from '../../../shared/lib/invariant-error' import { InvariantError } from '../../../shared/lib/invariant-error'
function collectAppPageRootParamKeys( function collectAppPageRootParamKeys(
...@@ -46,11 +44,9 @@ function collectAppPageRootParamKeys( ...@@ -46,11 +44,9 @@ function collectAppPageRootParamKeys(
* @param components the loaded components * @param components the loaded components
* @returns the segments for the route module * @returns the segments for the route module
*/ */
export function collectRootParamKeys({ export function collectRootParamKeys(
routeModule, routeModule: RouteModule
}: LoadComponentsReturnType< ): readonly string[] {
AppPageModule | AppRouteModule
>): readonly string[] {
if (isAppRouteRouteModule(routeModule)) { if (isAppRouteRouteModule(routeModule)) {
return [] return []
} }
......
...@@ -16,7 +16,6 @@ import type { ...@@ -16,7 +16,6 @@ import type {
} from './webpack/plugins/middleware-plugin' } from './webpack/plugins/middleware-plugin'
import type { WebpackLayerName } from '../lib/constants' import type { WebpackLayerName } from '../lib/constants'
import type { AppPageModule } from '../server/route-modules/app-page/module' import type { AppPageModule } from '../server/route-modules/app-page/module'
import type { RouteModule } from '../server/route-modules/route-module'
import type { NextComponentType } from '../shared/lib/utils' import type { NextComponentType } from '../shared/lib/utils'
import '../server/require-hook' import '../server/require-hook'
...@@ -1163,9 +1162,10 @@ export async function isPageStatic({ ...@@ -1163,9 +1162,10 @@ export async function isPageStatic({
sriEnabled, sriEnabled,
}) })
} }
const Comp = componentsResult.Component as NextComponentType | undefined
const routeModule: RouteModule = componentsResult.routeModule const { Component, routeModule } = componentsResult
const Comp = Component as NextComponentType | undefined
let isRoutePPREnabled: boolean = false let isRoutePPREnabled: boolean = false
...@@ -1176,7 +1176,7 @@ export async function isPageStatic({ ...@@ -1176,7 +1176,7 @@ export async function isPageStatic({
let segments let segments
try { try {
segments = await collectSegments(componentsResult) segments = await collectSegments(routeModule)
} catch (err) { } catch (err) {
throw new Error(`Failed to collect configuration for ${page}`, { throw new Error(`Failed to collect configuration for ${page}`, {
cause: err, cause: err,
...@@ -1194,7 +1194,7 @@ export async function isPageStatic({ ...@@ -1194,7 +1194,7 @@ export async function isPageStatic({
) )
} }
rootParamKeys = collectRootParamKeys(componentsResult) rootParamKeys = collectRootParamKeys(routeModule)
// A page supports partial prerendering if it is an app page and either // A page supports partial prerendering if it is an app page and either
// the whole app has PPR enabled or this page has PPR enabled when we're // the whole app has PPR enabled or this page has PPR enabled when we're
...@@ -1422,14 +1422,14 @@ export async function hasCustomGetInitialProps({ ...@@ -1422,14 +1422,14 @@ export async function hasCustomGetInitialProps({
require('../shared/lib/runtime-config.external') as typeof import('../shared/lib/runtime-config.external') require('../shared/lib/runtime-config.external') as typeof import('../shared/lib/runtime-config.external')
).setConfig(runtimeEnvConfig) ).setConfig(runtimeEnvConfig)
const components = await loadComponents({ const { ComponentMod } = await loadComponents({
distDir, distDir,
page: page, page: page,
isAppPath: false, isAppPath: false,
isDev: false, isDev: false,
sriEnabled, sriEnabled,
}) })
let mod = components.ComponentMod let mod = ComponentMod
if (checkingApp) { if (checkingApp) {
mod = (await mod._app) || mod.default || mod mod = (await mod._app) || mod.default || mod
...@@ -1454,7 +1454,7 @@ export async function getDefinedNamedExports({ ...@@ -1454,7 +1454,7 @@ export async function getDefinedNamedExports({
;( ;(
require('../shared/lib/runtime-config.external') as typeof import('../shared/lib/runtime-config.external') require('../shared/lib/runtime-config.external') as typeof import('../shared/lib/runtime-config.external')
).setConfig(runtimeEnvConfig) ).setConfig(runtimeEnvConfig)
const components = await loadComponents({ const { ComponentMod } = await loadComponents({
distDir, distDir,
page: page, page: page,
isAppPath: false, isAppPath: false,
...@@ -1462,8 +1462,8 @@ export async function getDefinedNamedExports({ ...@@ -1462,8 +1462,8 @@ export async function getDefinedNamedExports({
sriEnabled, sriEnabled,
}) })
return Object.keys(components.ComponentMod).filter((key) => { return Object.keys(ComponentMod).filter((key) => {
return typeof components.ComponentMod[key] !== 'undefined' return typeof ComponentMod[key] !== 'undefined'
}) })
} }
......
...@@ -107,13 +107,14 @@ export async function loadStaticPaths({ ...@@ -107,13 +107,14 @@ export async function loadStaticPaths({
}) })
if (isAppPath) { if (isAppPath) {
const segments = await collectSegments(components) const routeModule = components.routeModule
const segments = await collectSegments(routeModule)
const isRoutePPREnabled = const isRoutePPREnabled =
isAppPageRouteModule(components.routeModule) && isAppPageRouteModule(routeModule) &&
checkIsRoutePPREnabled(config.pprConfig, reduceAppConfig(segments)) checkIsRoutePPREnabled(config.pprConfig, reduceAppConfig(segments))
const rootParamKeys = collectRootParamKeys(components) const rootParamKeys = collectRootParamKeys(routeModule)
return buildAppStaticPaths({ return buildAppStaticPaths({
dir, dir,
......
...@@ -361,24 +361,28 @@ export default class NextNodeServer extends BaseServer< ...@@ -361,24 +361,28 @@ export default class NextNodeServer extends BaseServer<
await this.loadCustomCacheHandlers() await this.loadCustomCacheHandlers()
for (const page of Object.keys(pagesManifest || {})) { for (const page of Object.keys(pagesManifest || {})) {
try {
await loadComponents({ await loadComponents({
distDir: this.distDir, distDir: this.distDir,
page, page,
isAppPath: false, isAppPath: false,
isDev: this.isDev, isDev: this.isDev,
sriEnabled: this.sriEnabled, sriEnabled: this.sriEnabled,
}).catch(() => {}) })
} catch (_err) {
// Intentionally ignored because this is a preload step.
}
} }
for (const page of Object.keys(appPathsManifest || {})) { for (const page of Object.keys(appPathsManifest || {})) {
await loadComponents({ try {
const { ComponentMod } = await loadComponents({
distDir: this.distDir, distDir: this.distDir,
page, page,
isAppPath: true, isAppPath: true,
isDev: this.isDev, isDev: this.isDev,
sriEnabled: this.sriEnabled, sriEnabled: this.sriEnabled,
}) })
.then(async ({ ComponentMod }) => {
// we need to ensure fetch is patched before we require the page, // we need to ensure fetch is patched before we require the page,
// otherwise if the fetch is patched by user code, we will be patching it // otherwise if the fetch is patched by user code, we will be patching it
// too late and there won't be any caching behaviors // too late and there won't be any caching behaviors
...@@ -390,8 +394,9 @@ export default class NextNodeServer extends BaseServer< ...@@ -390,8 +394,9 @@ export default class NextNodeServer extends BaseServer<
await webpackRequire(id) await webpackRequire(id)
} }
} }
}) } catch (_err) {
.catch(() => {}) // Intentionally ignored because this is a preload step.
}
} }
} }
......
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