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

Move Edge SSR event waitUntil into the handler #56404

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
}
const nextFontManifest = maybeJSONParse(self.__NEXT_FONT_MANIFEST)

globalThis.__next_private_global_wait_until__ = []

const render = getRender({
pagesType,
dev: ${dev},
Expand Down Expand Up @@ -257,22 +255,12 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +17,15 @@ 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] =
// @ts-ignore
globalThis[NEXT_PRIVATE_GLOBAL_WAIT_UNTIL] || []

export function getRender({
dev,
page,
Expand Down Expand Up @@ -143,13 +153,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
Expand Down