Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-forward headers to external rewrites #17557

Merged
merged 4 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export async function imageOptimizer(
mockRes.setHeader = (name: string, value: string | string[]) =>
(mockHeaders[name.toLowerCase()] = value)
mockRes._implicitHeader = () => {}
mockRes.connection = res.connection
mockRes.finished = false
mockRes.statusCode = 200

Expand All @@ -258,6 +259,7 @@ export async function imageOptimizer(
mockReq.headers = req.headers
mockReq.method = req.method
mockReq.url = href
mockReq.connection = req.connection

await server.getRequestHandler()(
mockReq,
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ export default class Server {
target,
changeOrigin: true,
ignorePath: true,
xfwd: true,
proxyTimeout: 30_000, // limit proxying to 30 seconds
})

Expand Down
8 changes: 6 additions & 2 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,9 @@ const runTests = (isDev = false) => {
},
},
])
expect(await res.text()).toContain('hi from external')
const nextHost = `localhost:${appPort}`
const externalHost = `localhost:${externalServerPort}`
expect(await res.text()).toContain(`hi ${nextHost} from ${externalHost}`)
})

it('should support unnamed parameters correctly', async () => {
Expand Down Expand Up @@ -1911,7 +1913,9 @@ describe('Custom routes', () => {
externalServerPort = await findPort()
externalServer = http.createServer((req, res) => {
externalServerHits.add(req.url)
res.end('hi from external')
const nextHost = req.headers['x-forwarded-host']
const externalHost = req.headers['host']
res.end(`hi ${nextHost} from ${externalHost}`)
})
await new Promise((resolve, reject) => {
externalServer.listen(externalServerPort, (error) => {
Expand Down