Skip to content

Commit

Permalink
Fix build restart log (vercel#56543)
Browse files Browse the repository at this point in the history
### Observed Issue
```
⚠ Restarted collecting page data for [object Object] because it took more than 60 seconds
```

### Fix
The original issue is caused because the path is assigned to the `argument` array itself. Passing the argument type to the he worker, so in restart callback we're type safe, can the value will be correct.
  • Loading branch information
huozhi authored Oct 7, 2023
1 parent c60ecfc commit dbf35a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
13 changes: 9 additions & 4 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import type { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared'
import type { MiddlewareManifest } from './webpack/plugins/middleware-plugin'
import type { ActionManifest } from './webpack/plugins/flight-client-entry-plugin'
import type { ExportAppOptions, ExportAppWorker } from '../export/types'
import type {
ExportAppOptions,
ExportAppWorker,
ExportPageInput,
} from '../export/types'

import '../lib/setup-exception-listeners'

Expand Down Expand Up @@ -1211,12 +1215,13 @@ export default async function build(
| 'isPageStatic'
| 'getDefinedNamedExports'
| 'exportPage'
>
>,
[ExportPageInput]
>(staticWorkerPath, {
timeout: timeout * 1000,
onRestart: (method, [arg], attempts) => {
if (method === 'exportPage') {
const { path: pagePath } = arg
const pagePath = arg.path
if (attempts >= 3) {
throw new Error(
`Static page generation for ${pagePath} is still timing out after 3 attempts. See more info here https://nextjs.org/docs/messages/static-page-generation-timeout`
Expand All @@ -1226,7 +1231,7 @@ export default async function build(
`Restarted static page generation for ${pagePath} because it took more than ${timeout} seconds`
)
} else {
const pagePath = arg
const pagePath = arg.path
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
3 changes: 2 additions & 1 deletion packages/next/src/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
ExportAppOptions,
ExportWorker,
WorkerRenderOptsPartial,
ExportPageInput,
} from './types'
import type { PrerenderManifest } from '../build'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
Expand Down Expand Up @@ -173,7 +174,7 @@ function setupWorkers(

let infoPrinted = false

const worker = Worker.create<typeof import('./worker')>(
const worker = Worker.create<typeof import('./worker'), [ExportPageInput]>(
require.resolve('./worker'),
{
timeout: timeout * 1000,
Expand Down
21 changes: 12 additions & 9 deletions packages/next/src/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,31 @@ const cleanupWorkers = (worker: JestWorker) => {
}
}

type Options<T extends object = object> = FarmOptions & {
type Options<
T extends object = object,
Args extends any[] = any[]
> = FarmOptions & {
timeout?: number
onRestart?: (method: string, args: any[], attempts: number) => void
onRestart?: (method: string, args: Args, attempts: number) => void
exposedMethods: ReadonlyArray<keyof T>
enableWorkerThreads?: boolean
}

export class Worker<T extends object = object> {
export class Worker<T extends object = object, Args extends any[] = any[]> {
private _worker?: JestWorker

/**
* Creates a new worker with the correct typings associated with the selected
* methods.
*/
public static create<T extends object>(
public static create<T extends object, Args extends any[] = any[]>(
workerPath: string,
options: Options<T>
): Worker<T> & T {
return new Worker(workerPath, options) as Worker<T> & T
options: Options<T, Args>
): Worker<T, Args> & T {
return new Worker(workerPath, options) as Worker<T, Args> & T
}

constructor(workerPath: string, options: Options<T>) {
constructor(workerPath: string, options: Options<T, Args>) {
let { timeout, onRestart, ...farmOptions } = options

let restartPromise: Promise<typeof RESTARTED>
Expand Down Expand Up @@ -133,7 +136,7 @@ export class Worker<T extends object = object> {
<M extends (...args: unknown[]) => Promise<unknown> | unknown>(
method: M
) =>
async (...args: Parameters<M>) => {
async (...args: Args) => {
activeTasks++

try {
Expand Down

0 comments on commit dbf35a7

Please sign in to comment.