Skip to content

Commit 8d5eeae

Browse files
committed
Set correct origin for internal redirects in custom server
vercel/next-js-mirror#136
1 parent 4f451f6 commit 8d5eeae

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

packages/next/errors.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,5 +1442,8 @@
14421442
"1441": "DevValidationScheduler requires at least one active validation",
14431443
"1442": "The Server Reference ID did not match the expected format. Received %s.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
14441444
"1443": "Unsupported body type: %s",
1445-
"1444": "Invalid target hostname \"%s\" for proxy request, expected \"%s\""
1445+
"1444": "Invalid target hostname \"%s\" for proxy request, expected \"%s\"",
1446+
"1445": "Invariant: Missing `__NEXT_PRIVATE_ORIGIN` environment variable.",
1447+
"1446": "Missing initURL",
1448+
"1447": "Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server."
14461449
}

packages/next/src/server/app-render/action-handler.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,24 @@ async function createForwardedActionResponse(
227227
// with the response from the forwarded worker
228228
forwardedHeaders.set('x-action-forwarded', '1')
229229

230-
const proto =
231-
getRequestMeta(req, 'initProtocol')?.replace(/:+$/, '') || 'https'
232-
233-
// For standalone or the serverful mode, use the internal origin directly
234-
// other than the host headers from the request.
235-
const origin = process.env.__NEXT_PRIVATE_ORIGIN || `${proto}://${host.value}`
230+
// TODO: Remove __NEXT_PRIVATE_ORIGIN
231+
let origin: string | undefined = process.env.__NEXT_PRIVATE_ORIGIN
232+
if (origin === undefined) {
233+
const initUrl = getRequestMeta(req, 'initURL')
234+
if (initUrl !== undefined) {
235+
try {
236+
const parsedUrl = new URL(initUrl)
237+
origin = parsedUrl.origin
238+
} catch (error) {
239+
throw new Error(
240+
'Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.',
241+
{ cause: error }
242+
)
243+
}
244+
} else {
245+
throw new InvariantError('Missing initURL')
246+
}
247+
}
236248

237249
const fetchUrl = new URL(`${origin}${basePath}${workerPathname}`)
238250

@@ -385,13 +397,25 @@ async function createRedirectRenderResult(
385397
const forwardedHeaders = getForwardedHeaders(req, res)
386398
forwardedHeaders.set(RSC_HEADER, '1')
387399

388-
const proto =
389-
getRequestMeta(req, 'initProtocol')?.replace(/:+$/, '') || 'https'
390-
391-
// For standalone or the serverful mode, use the internal origin directly
392-
// other than the host headers from the request.
393-
const origin =
394-
process.env.__NEXT_PRIVATE_ORIGIN || `${proto}://${originalHost.value}`
400+
// TODO: Remove __NEXT_PRIVATE_ORIGIN
401+
let origin: string | undefined = process.env.__NEXT_PRIVATE_ORIGIN
402+
if (origin === undefined) {
403+
const initUrl = getRequestMeta(req, 'initURL')
404+
if (initUrl !== undefined) {
405+
try {
406+
const parsedUrl = new URL(initUrl)
407+
408+
origin = parsedUrl.origin
409+
} catch (error) {
410+
throw new Error(
411+
'Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.',
412+
{ cause: error }
413+
)
414+
}
415+
} else {
416+
throw new InvariantError('Missing initURL')
417+
}
418+
}
395419

396420
const fetchUrl = new URL(
397421
`${origin}${appRelativeRedirectUrl.pathname}${appRelativeRedirectUrl.search}`

0 commit comments

Comments
 (0)