From 5bd0347ac30a3c0cfa34e99ac50006d34f4c1070 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 4 Oct 2023 11:34:55 +0200 Subject: [PATCH 1/2] move event waitUntil into the handler --- .../loaders/next-edge-ssr-loader/index.ts | 16 ++-------------- .../loaders/next-edge-ssr-loader/render.ts | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts index 706f7f27571af..36ff0a9bf61a6 100644 --- a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts +++ b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts @@ -225,8 +225,6 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction = } const nextFontManifest = maybeJSONParse(self.__NEXT_FONT_MANIFEST) - globalThis.__next_private_global_wait_until__ = [] - const render = getRender({ pagesType, dev: ${dev}, @@ -257,22 +255,12 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction = export const ComponentMod = pageMod - export default async function nHandler (opts, event) { - const res = await adapter({ + export default function nHandler (opts, event) { + return adapter({ ...opts, IncrementalCache, handler: render }) - - if (event && event.waitUntil) { - event.waitUntil( - Promise.all( - [res?.waitUntil, ...globalThis.__next_private_global_wait_until__] - ) - ) - } - - return res }` return transformed diff --git a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts index eacd767be52e9..05241c728cfd2 100644 --- a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts +++ b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts @@ -5,6 +5,7 @@ import type { BuildManifest } from '../../../../server/get-page-files' import type { ReactLoadableManifest } from '../../../../server/load-components' import type { ClientReferenceManifest } from '../../plugins/flight-manifest-plugin' import type { NextFontManifest } from '../../plugins/next-font-manifest-plugin' +import type { NextFetchEvent } from '../../../../server/web/spec-extension/fetch-event' import WebServer from '../../../../server/web-server' import { @@ -16,6 +17,11 @@ import { PrerenderManifest } from '../../..' import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths' import { SizeLimit } from '../../../../../types' +// @ts-ignore +globalThis.__next_private_global_wait_until__ = + // @ts-ignore + globalThis.__next_private_global_wait_until__ || [] + export function getRender({ dev, page, @@ -143,13 +149,20 @@ export function getRender({ const handler = server.getRequestHandler() - return async function render(request: Request) { + return async function render(request: Request, event: NextFetchEvent) { const extendedReq = new WebNextRequest(request) const extendedRes = new WebNextResponse() handler(extendedReq, extendedRes) const result = await extendedRes.toResponse() + if (event && event.waitUntil) { + event.waitUntil( + // @ts-ignore + Promise.all([...globalThis.__next_private_global_wait_until__]) + ) + } + // fetchMetrics is attached to the web request that going through the server, // wait for the handler result is ready and attach it back to the original request. ;(request as any).fetchMetrics = extendedReq.fetchMetrics From 4409a9c6fb06a9bd60163c4a5102023b722e3be3 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Wed, 4 Oct 2023 15:02:10 +0200 Subject: [PATCH 2/2] use symbol --- .../webpack/loaders/next-edge-ssr-loader/render.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts index 05241c728cfd2..d4c4aa11138b0 100644 --- a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts +++ b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/render.ts @@ -17,10 +17,14 @@ import { PrerenderManifest } from '../../..' import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths' import { SizeLimit } from '../../../../../types' +const NEXT_PRIVATE_GLOBAL_WAIT_UNTIL = Symbol.for( + '__next_private_global_wait_until__' +) + // @ts-ignore -globalThis.__next_private_global_wait_until__ = +globalThis[NEXT_PRIVATE_GLOBAL_WAIT_UNTIL] = // @ts-ignore - globalThis.__next_private_global_wait_until__ || [] + globalThis[NEXT_PRIVATE_GLOBAL_WAIT_UNTIL] || [] export function getRender({ dev, @@ -159,7 +163,7 @@ export function getRender({ if (event && event.waitUntil) { event.waitUntil( // @ts-ignore - Promise.all([...globalThis.__next_private_global_wait_until__]) + Promise.all([...globalThis[NEXT_PRIVATE_GLOBAL_WAIT_UNTIL]]) ) }