From 81db46b7133780d6a153bcac2318b4cc53aa9441 Mon Sep 17 00:00:00 2001 From: Janka Uryga Date: Tue, 11 Jun 2024 13:39:26 +0200 Subject: [PATCH] fix: propagate waitUntil from edge runtime sandbox --- packages/next/src/server/base-server.ts | 2 +- packages/next/src/server/next-server.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index f948f311ce8e6..a5a5fc4749387 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -1669,7 +1669,7 @@ export default abstract class Server< ) } - private getWaitUntil(): WaitUntil | undefined { + protected getWaitUntil(): WaitUntil | undefined { const builtinRequestContext = getBuiltinRequestContext() if (builtinRequestContext) { // the platform provided a request context. diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index 37fe46ff7a14a..c51758cf61fbc 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -980,7 +980,12 @@ export default class NextNodeServer extends BaseServer< }) // If we handled the request, we can return early. - if (handled) return true + if (handled) { + const waitUntil = this.getWaitUntil() + waitUntil?.(handled.waitUntil) + + return true + } } // If the route was detected as being a Pages API route, then handle @@ -1934,6 +1939,11 @@ export default class NextNodeServer extends BaseServer< } }) + const waitUntil = this.getWaitUntil() + if (waitUntil) { + waitUntil(result.waitUntil) + } + const { originalResponse } = params.res if (result.response.body) { await pipeToNodeResponse(result.response.body, originalResponse)