Unverified Commit 778a2a74 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix app routes on deploy (#45931)

Follow-up to https://github.com/vercel/next.js/pull/45716 this ensures
we correctly construct the initial URL value as it must be a fully
qualified URL. Existing tests caught this failure when running in deploy
mode.
parent 4697b159
...@@ -2051,6 +2051,8 @@ export default class NextNodeServer extends BaseServer { ...@@ -2051,6 +2051,8 @@ export default class NextNodeServer extends BaseServer {
const initUrl = const initUrl =
this.hostname && this.port this.hostname && this.port
? `${protocol}://${this.hostname}:${this.port}${req.url}` ? `${protocol}://${this.hostname}:${this.port}${req.url}`
: (this.nextConfig.experimental as any).trustHostHeader
? `https://${req.headers.host || 'localhost'}${req.url}`
: req.url : req.url
addRequestMeta(req, '__NEXT_INIT_URL', initUrl) addRequestMeta(req, '__NEXT_INIT_URL', initUrl)
......
...@@ -5,6 +5,7 @@ import { ...@@ -5,6 +5,7 @@ import {
cookieWithRequestMeta, cookieWithRequestMeta,
} from './helpers' } from './helpers'
import { Readable } from 'stream' import { Readable } from 'stream'
import { check } from 'next-test-utils'
createNextDescribe( createNextDescribe(
'app-custom-routes', 'app-custom-routes',
...@@ -92,26 +93,29 @@ createNextDescribe( ...@@ -92,26 +93,29 @@ createNextDescribe(
}) })
describe('body', () => { describe('body', () => {
it('can handle handle a streaming request and streaming response', async () => { // we can't stream a body to a function currently only stream response
const body = new Array(10).fill(JSON.stringify({ ping: 'pong' })) if (!(global as any).isNextDeploy) {
let index = 0 it('can handle handle a streaming request and streaming response', async () => {
const stream = new Readable({ const body = new Array(10).fill(JSON.stringify({ ping: 'pong' }))
read() { let index = 0
if (index >= body.length) return this.push(null) const stream = new Readable({
read() {
this.push(body[index] + '\n') if (index >= body.length) return this.push(null)
index++
}, this.push(body[index] + '\n')
}) index++
},
})
const res = await next.fetch('/advanced/body/streaming', { const res = await next.fetch('/advanced/body/streaming', {
method: 'POST', method: 'POST',
body: stream, body: stream,
}) })
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
expect(await res.text()).toEqual(body.join('\n') + '\n') expect(await res.text()).toEqual(body.join('\n') + '\n')
}) })
}
it('can read a JSON encoded body', async () => { it('can read a JSON encoded body', async () => {
const body = { ping: 'pong' } const body = { ping: 'pong' }
...@@ -125,27 +129,30 @@ createNextDescribe( ...@@ -125,27 +129,30 @@ createNextDescribe(
expect(meta.body).toEqual(body) expect(meta.body).toEqual(body)
}) })
it('can read a streamed JSON encoded body', async () => { // we can't stream a body to a function currently only stream response
const body = { ping: 'pong' } if (!(global as any).isNextDeploy) {
const encoded = JSON.stringify(body) it('can read a streamed JSON encoded body', async () => {
let index = 0 const body = { ping: 'pong' }
const stream = new Readable({ const encoded = JSON.stringify(body)
async read() { let index = 0
if (index >= encoded.length) return this.push(null) const stream = new Readable({
async read() {
this.push(encoded[index]) if (index >= encoded.length) return this.push(null)
index++
}, this.push(encoded[index])
}) index++
const res = await next.fetch('/advanced/body/json', { },
method: 'POST', })
body: stream, const res = await next.fetch('/advanced/body/json', {
}) method: 'POST',
body: stream,
})
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
const meta = getRequestMeta(res.headers) const meta = getRequestMeta(res.headers)
expect(meta.body).toEqual(body) expect(meta.body).toEqual(body)
}) })
}
it('can read the text body', async () => { it('can read the text body', async () => {
const body = 'hello, world' const body = 'hello, world'
...@@ -275,7 +282,13 @@ createNextDescribe( ...@@ -275,7 +282,13 @@ createNextDescribe(
expect(res.status).toEqual(500) expect(res.status).toEqual(500)
expect(await res.text()).toBeEmpty() expect(await res.text()).toBeEmpty()
expect(next.cliOutput).toContain(error)
if (!(global as any).isNextDeploy) {
await check(() => {
expect(next.cliOutput).toContain(error)
return 'yes'
}, 'yes')
}
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment