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

Ensure proxy rewrite does not hang on error #24394

Merged
merged 5 commits into from
Apr 26, 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
23 changes: 20 additions & 3 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,12 +924,29 @@ export default class Server {
target,
changeOrigin: true,
ignorePath: true,
proxyTimeout: 30_000, // limit proxying to 30 seconds
})
proxy.web(req, res)

proxy.on('error', (err: Error) => {
console.error(`Error occurred proxying ${target}`, err)
await new Promise((proxyResolve, proxyReject) => {
let finished = false

proxy.on('proxyReq', (proxyReq) => {
proxyReq.on('close', () => {
if (!finished) {
finished = true
proxyResolve(true)
}
})
})
proxy.on('error', (err) => {
if (!finished) {
finished = true
proxyReject(err)
}
})
proxy.web(req, res)
})

return {
finished: true,
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/custom-routes/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module.exports = {
},
]
: []),
{
source: '/to-nowhere',
destination: 'http://localhost:12233',
},
{
source: '/rewriting-to-auto-export',
destination: '/auto-export/hello?rewrite=1',
Expand Down
13 changes: 13 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ let appPort
let app

const runTests = (isDev = false) => {
it('should not hang when proxy rewrite fails', async () => {
const res = await fetchViaHTTP(appPort, '/to-nowhere', undefined, {
timeout: 5000,
})

expect(res.status).toBe(500)
})

it('should parse params correctly for rewrite to auto-export dynamic page', async () => {
const browser = await webdriver(appPort, '/rewriting-to-auto-export')
const text = await browser.eval(() => document.documentElement.innerHTML)
Expand Down Expand Up @@ -1395,6 +1403,11 @@ const runTests = (isDev = false) => {
},
],
afterFiles: [
{
destination: 'http://localhost:12233',
regex: normalizeRegEx('^\\/to-nowhere$'),
source: '/to-nowhere',
},
{
destination: '/auto-export/hello?rewrite=1',
regex: normalizeRegEx('^\\/rewriting-to-auto-export$'),
Expand Down