Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build worker callback arg missing correct page path #61347

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/build/handle-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
NODE_ESM_RESOLVE_OPTIONS,
NODE_RESOLVE_OPTIONS,
} from './webpack-config'
import { isWebpackAppLayer, isWebpackServerLayer } from './worker'
import { isWebpackAppLayer, isWebpackServerLayer } from './utils'
import type { NextConfigComplete } from '../server/config-shared'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
const reactPackagesRegex = /^(react|react-dom|react-server-dom-webpack)($|\/)/
Expand Down
37 changes: 23 additions & 14 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ function getNumberOfWorkers(config: NextConfigComplete) {
}

const staticWorkerPath = require.resolve('./worker')
type StaticWorker = typeof import('./worker')

function createStaticWorker(
config: NextConfigComplete,
incrementalCacheIpcPort?: number,
Expand All @@ -570,8 +572,9 @@ function createStaticWorker(
return new Worker(staticWorkerPath, {
timeout: timeout * 1000,
logger: Log,
onRestart: (method, [arg], attempts) => {
onRestart: (method, args_, attempts) => {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
if (method === 'exportPage') {
const [arg] = args_ as Parameters<StaticWorker['exportPage']>
const pagePath = arg.path
if (attempts >= 3) {
throw new Error(
Expand All @@ -582,7 +585,13 @@ function createStaticWorker(
`Restarted static page generation for ${pagePath} because it took more than ${timeout} seconds`
)
} else {
const pagePath = arg.path
const [arg] = args_ as Parameters<
| StaticWorker['getDefinedNamedExports']
| StaticWorker['hasCustomGetInitialProps']
| StaticWorker['isPageStatic']
>

const pagePath = arg.page
if (attempts >= 2) {
throw new Error(
`Collecting page data for ${pagePath} is still timing out after 2 attempts. See more info here https://nextjs.org/docs/messages/page-data-collection-timeout`
Expand Down Expand Up @@ -1602,12 +1611,12 @@ export default async function build(
nonStaticErrorPageSpan.traceAsyncFn(
async () =>
hasCustomErrorPage &&
(await pagesStaticWorkers.hasCustomGetInitialProps(
'/_error',
(await pagesStaticWorkers.hasCustomGetInitialProps({
page: '/_error',
distDir,
runtimeEnvConfig,
false
))
checkingApp: false,
}))
)

const errorPageStaticResult = nonStaticErrorPageSpan.traceAsyncFn(
Expand All @@ -1630,18 +1639,18 @@ export default async function build(
const appPageToCheck = '/_app'

const customAppGetInitialPropsPromise =
pagesStaticWorkers.hasCustomGetInitialProps(
appPageToCheck,
pagesStaticWorkers.hasCustomGetInitialProps({
page: appPageToCheck,
distDir,
runtimeEnvConfig,
true
)
checkingApp: true,
})

const namedExportsPromise = pagesStaticWorkers.getDefinedNamedExports(
appPageToCheck,
const namedExportsPromise = pagesStaticWorkers.getDefinedNamedExports({
page: appPageToCheck,
distDir,
runtimeEnvConfig
)
runtimeEnvConfig,
})

// eslint-disable-next-line @typescript-eslint/no-shadow
let isNextImageImported: boolean | undefined
Expand Down
27 changes: 18 additions & 9 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1788,12 +1788,17 @@ export async function isPageStatic({
})
}

export async function hasCustomGetInitialProps(
page: string,
distDir: string,
runtimeEnvConfig: any,
export async function hasCustomGetInitialProps({
page,
distDir,
runtimeEnvConfig,
checkingApp,
}: {
page: string
distDir: string
runtimeEnvConfig: any
checkingApp: boolean
): Promise<boolean> {
}): Promise<boolean> {
require('../shared/lib/runtime-config.external').setConfig(runtimeEnvConfig)

const components = await loadComponents({
Expand All @@ -1812,11 +1817,15 @@ export async function hasCustomGetInitialProps(
return mod.getInitialProps !== mod.origGetInitialProps
}

export async function getDefinedNamedExports(
page: string,
distDir: string,
export async function getDefinedNamedExports({
page,
distDir,
runtimeEnvConfig,
}: {
page: string
distDir: string
runtimeEnvConfig: any
): Promise<ReadonlyArray<string>> {
}): Promise<ReadonlyArray<string>> {
require('../shared/lib/runtime-config.external').setConfig(runtimeEnvConfig)
const components = await loadComponents({
distDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { RouteKind } from '../../../../server/future/route-kind'
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path'
import { decodeFromBase64, encodeToBase64 } from '../utils'
import { isInstrumentationHookFile } from '../../../worker'
import { isInstrumentationHookFile } from '../../../utils'
import { loadEntrypoint } from '../../../load-entrypoint'
import type { MappedPages } from '../../../build-context'

Expand Down
6 changes: 5 additions & 1 deletion packages/next/src/build/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import '../server/require-hook'

export * from './utils'
export {
getDefinedNamedExports,
hasCustomGetInitialProps,
isPageStatic,
} from './utils'
import exportPage from '../export/worker'
export { exportPage }
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import {
isInstrumentationHookFile,
getPossibleMiddlewareFilenames,
getPossibleInstrumentationHookFilenames,
} from '../../../build/worker'
} from '../../../build/utils'
import {
createOriginalStackFrame,
getErrorSource,
Expand Down
Loading