From d56c86546f54c58effd70496bf36cf9fb3a63c6a Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 4 Sep 2023 10:25:16 +0200 Subject: [PATCH 1/6] Add constant --- packages/next/src/server/dev/hot-reloader-types.ts | 1 + packages/next/src/server/dev/on-demand-entry-handler.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/next/src/server/dev/hot-reloader-types.ts b/packages/next/src/server/dev/hot-reloader-types.ts index 5f9f3ae3eda3d..855d120239265 100644 --- a/packages/next/src/server/dev/hot-reloader-types.ts +++ b/packages/next/src/server/dev/hot-reloader-types.ts @@ -18,6 +18,7 @@ export const enum HMR_ACTIONS_SENT_TO_BROWSER { PONG = 'pong', DEV_PAGES_MANIFEST_UPDATE = 'devPagesManifestUpdate', TURBOPACK_MESSAGE = 'turbopack-message', + SERVER_ERROR = 'serverError', } interface TurboPackMessageAction { diff --git a/packages/next/src/server/dev/on-demand-entry-handler.ts b/packages/next/src/server/dev/on-demand-entry-handler.ts index ce32c66e4c730..f77d4e09936a6 100644 --- a/packages/next/src/server/dev/on-demand-entry-handler.ts +++ b/packages/next/src/server/dev/on-demand-entry-handler.ts @@ -35,6 +35,7 @@ import { } from '../../shared/lib/constants' import { RouteMatch } from '../future/route-matches/route-match' import { isAppPageRouteMatch } from '../future/route-matches/app-page-route-match' +import { HMR_ACTIONS_SENT_TO_BROWSER } from './hot-reloader-types' const debug = origDebug('next:on-demand-entry-handler') @@ -949,7 +950,7 @@ export function onDemandEntryHandler({ if (!bufferedHmrServerError && error) { client.send( JSON.stringify({ - action: 'serverError', + action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR, errorJSON: stringifyError(error), }) ) From 6c49ca8eee3f13e2a3471a165261040e58a17af5 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 4 Sep 2023 10:36:16 +0200 Subject: [PATCH 2/6] Add ServerErrorAction interface --- .../next/src/server/dev/hot-reloader-types.ts | 5 +++++ .../src/server/dev/on-demand-entry-handler.ts | 16 +++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/next/src/server/dev/hot-reloader-types.ts b/packages/next/src/server/dev/hot-reloader-types.ts index 855d120239265..53f1f88db8614 100644 --- a/packages/next/src/server/dev/hot-reloader-types.ts +++ b/packages/next/src/server/dev/hot-reloader-types.ts @@ -21,6 +21,11 @@ export const enum HMR_ACTIONS_SENT_TO_BROWSER { SERVER_ERROR = 'serverError', } +export interface ServerErrorAction { + action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR + errorJSON: string +} + interface TurboPackMessageAction { type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE data: TurbopackUpdate diff --git a/packages/next/src/server/dev/on-demand-entry-handler.ts b/packages/next/src/server/dev/on-demand-entry-handler.ts index f77d4e09936a6..a489c1bfbc731 100644 --- a/packages/next/src/server/dev/on-demand-entry-handler.ts +++ b/packages/next/src/server/dev/on-demand-entry-handler.ts @@ -35,7 +35,10 @@ import { } from '../../shared/lib/constants' import { RouteMatch } from '../future/route-matches/route-match' import { isAppPageRouteMatch } from '../future/route-matches/app-page-route-match' -import { HMR_ACTIONS_SENT_TO_BROWSER } from './hot-reloader-types' +import { + HMR_ACTIONS_SENT_TO_BROWSER, + ServerErrorAction, +} from './hot-reloader-types' const debug = origDebug('next:on-demand-entry-handler') @@ -948,12 +951,11 @@ export function onDemandEntryHandler({ // New error occurred: buffered error is flushed and new error occurred if (!bufferedHmrServerError && error) { - client.send( - JSON.stringify({ - action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR, - errorJSON: stringifyError(error), - }) - ) + const serverErrorAction: ServerErrorAction = { + action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR, + errorJSON: stringifyError(error), + } + client.send(JSON.stringify(serverErrorAction)) bufferedHmrServerError = null } From 780d49d13f424922aa708bc97b0c1760eaab8b35 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 4 Sep 2023 10:37:03 +0200 Subject: [PATCH 3/6] Add to HMR_ACTION_TYPES --- packages/next/src/server/dev/hot-reloader-types.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/next/src/server/dev/hot-reloader-types.ts b/packages/next/src/server/dev/hot-reloader-types.ts index 53f1f88db8614..1324580d4586e 100644 --- a/packages/next/src/server/dev/hot-reloader-types.ts +++ b/packages/next/src/server/dev/hot-reloader-types.ts @@ -102,6 +102,7 @@ export type HMR_ACTION_TYPES = | MiddlewareChangesAction | ServerOnlyChangesAction | DevPagesManifestUpdateAction + | ServerErrorAction export interface NextJsHotReloaderInterface { activeWebpackConfigs?: Array>> From d5721884f3e32b47864662eef28f95329060bbae Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 4 Sep 2023 10:37:55 +0200 Subject: [PATCH 4/6] Use HMR_ACTION_TYPES, remove export of individual action --- packages/next/src/server/dev/hot-reloader-types.ts | 2 +- packages/next/src/server/dev/on-demand-entry-handler.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/next/src/server/dev/hot-reloader-types.ts b/packages/next/src/server/dev/hot-reloader-types.ts index 1324580d4586e..18f27a50c4567 100644 --- a/packages/next/src/server/dev/hot-reloader-types.ts +++ b/packages/next/src/server/dev/hot-reloader-types.ts @@ -21,7 +21,7 @@ export const enum HMR_ACTIONS_SENT_TO_BROWSER { SERVER_ERROR = 'serverError', } -export interface ServerErrorAction { +interface ServerErrorAction { action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR errorJSON: string } diff --git a/packages/next/src/server/dev/on-demand-entry-handler.ts b/packages/next/src/server/dev/on-demand-entry-handler.ts index a489c1bfbc731..7592551573c77 100644 --- a/packages/next/src/server/dev/on-demand-entry-handler.ts +++ b/packages/next/src/server/dev/on-demand-entry-handler.ts @@ -37,6 +37,7 @@ import { RouteMatch } from '../future/route-matches/route-match' import { isAppPageRouteMatch } from '../future/route-matches/app-page-route-match' import { HMR_ACTIONS_SENT_TO_BROWSER, + HMR_ACTION_TYPES, ServerErrorAction, } from './hot-reloader-types' @@ -951,7 +952,7 @@ export function onDemandEntryHandler({ // New error occurred: buffered error is flushed and new error occurred if (!bufferedHmrServerError && error) { - const serverErrorAction: ServerErrorAction = { + const serverErrorAction: HMR_ACTION_TYPES = { action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR, errorJSON: stringifyError(error), } From cdded63ca231251f2d5aaf78d422e6297f3fd4d8 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 4 Sep 2023 11:12:37 +0200 Subject: [PATCH 5/6] Use hotReloader.send instead of manually creating event object --- .../next/src/server/dev/hot-reloader-webpack.ts | 1 + .../next/src/server/dev/on-demand-entry-handler.ts | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/next/src/server/dev/hot-reloader-webpack.ts b/packages/next/src/server/dev/hot-reloader-webpack.ts index df536185baedf..bc413bc153b16 100644 --- a/packages/next/src/server/dev/hot-reloader-webpack.ts +++ b/packages/next/src/server/dev/hot-reloader-webpack.ts @@ -1385,6 +1385,7 @@ export default class HotReloader implements NextJsHotReloaderInterface { }) this.onDemandEntries = onDemandEntryHandler({ + hotReloader: this, multiCompiler: this.multiCompiler, pagesDir: this.pagesDir, appDir: this.appDir, diff --git a/packages/next/src/server/dev/on-demand-entry-handler.ts b/packages/next/src/server/dev/on-demand-entry-handler.ts index 7592551573c77..da7372ac73ae8 100644 --- a/packages/next/src/server/dev/on-demand-entry-handler.ts +++ b/packages/next/src/server/dev/on-demand-entry-handler.ts @@ -35,11 +35,8 @@ import { } from '../../shared/lib/constants' import { RouteMatch } from '../future/route-matches/route-match' import { isAppPageRouteMatch } from '../future/route-matches/app-page-route-match' -import { - HMR_ACTIONS_SENT_TO_BROWSER, - HMR_ACTION_TYPES, - ServerErrorAction, -} from './hot-reloader-types' +import { HMR_ACTIONS_SENT_TO_BROWSER } from './hot-reloader-types' +import HotReloader from './hot-reloader-webpack' const debug = origDebug('next:on-demand-entry-handler') @@ -503,6 +500,7 @@ async function findRoutePathData( } export function onDemandEntryHandler({ + hotReloader, maxInactiveAge, multiCompiler, nextConfig, @@ -511,6 +509,7 @@ export function onDemandEntryHandler({ rootDir, appDir, }: { + hotReloader: HotReloader maxInactiveAge: number multiCompiler: webpack.MultiCompiler nextConfig: NextConfigComplete @@ -952,11 +951,10 @@ export function onDemandEntryHandler({ // New error occurred: buffered error is flushed and new error occurred if (!bufferedHmrServerError && error) { - const serverErrorAction: HMR_ACTION_TYPES = { + hotReloader.send({ action: HMR_ACTIONS_SENT_TO_BROWSER.SERVER_ERROR, errorJSON: stringifyError(error), - } - client.send(JSON.stringify(serverErrorAction)) + }) bufferedHmrServerError = null } From 3360bc5dfca06c1a88728dc473b70c66806cca46 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 4 Sep 2023 11:13:06 +0200 Subject: [PATCH 6/6] Remove check for payload.errorJSON as it's always included --- packages/next/src/client/page-bootstrap.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/client/page-bootstrap.ts b/packages/next/src/client/page-bootstrap.ts index fd209eaf8a11c..43a44d19ba810 100644 --- a/packages/next/src/client/page-bootstrap.ts +++ b/packages/next/src/client/page-bootstrap.ts @@ -17,7 +17,7 @@ export function pageBootrap(assetPrefix: string) { let buildIndicatorHandler: (obj: Record) => void = () => {} function devPagesHmrListener(payload: any) { - if (payload.action === 'serverError' && payload.errorJSON) { + if (payload.action === 'serverError') { const { stack, message } = JSON.parse(payload.errorJSON) const error = new Error(message) error.stack = stack