diff --git a/packages/next/src/server/app-render/encryption.ts b/packages/next/src/server/app-render/encryption.ts index a1cc98d2787a0..9324d881517ec 100644 --- a/packages/next/src/server/app-render/encryption.ts +++ b/packages/next/src/server/app-render/encryption.ts @@ -205,8 +205,22 @@ export async function decryptActionBoundArgs( new ReadableStream({ start(controller) { controller.enqueue(textEncoder.encode(decrypted)) - // Explicitly don't close the stream here so that hanging promises are - // not rejected. + + if (workUnitStore?.type === 'prerender') { + // Explicitly don't close the stream here (until prerendering is + // complete) so that hanging promises are not rejected. + if (workUnitStore.renderSignal.aborted) { + controller.close() + } else { + workUnitStore.renderSignal.addEventListener( + 'abort', + () => controller.close(), + { once: true } + ) + } + } else { + controller.close() + } }, }), {