From 8f7a6ca7d21a97bc9f7a1bbe10427b5ad74b9085 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 27 Feb 2024 09:17:50 -0800 Subject: [PATCH] Improve redirection handling (#62561) Currently to redirect to another RSC route of the same app, we initiate another request to that route. This PR changes that request to directly use the internal hostname of the current server handler, rather than relying on other headers. Closes NEXT-2614 --- packages/next/src/server/app-render/action-handler.ts | 6 +++++- packages/next/src/server/lib/start-server.ts | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/next/src/server/app-render/action-handler.ts b/packages/next/src/server/app-render/action-handler.ts index 7bf38dfdc24a3..c1f0dadf950bb 100644 --- a/packages/next/src/server/app-render/action-handler.ts +++ b/packages/next/src/server/app-render/action-handler.ts @@ -179,9 +179,13 @@ async function createRedirectRenderResult( const forwardedHeaders = getForwardedHeaders(req, res) forwardedHeaders.set(RSC_HEADER, '1') - const host = originalHost.value const proto = staticGenerationStore.incrementalCache?.requestProtocol || 'https' + + // For standalone or the serverful mode, use the internal hostname directly + // other than the headers from the request. + const host = process.env.__NEXT_PRIVATE_HOST || originalHost.value + const fetchUrl = new URL( `${proto}://${host}${basePath}${parsedRedirectUrl.pathname}` ) diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 018f0da49c66f..1f5734e900ec8 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -246,6 +246,7 @@ export async function startServer( // expose the main port to render workers process.env.PORT = port + '' + process.env.__NEXT_PRIVATE_HOST = `${actualHostname}:${port}` // Only load env and config in dev to for logging purposes let envInfo: string[] | undefined