Skip to content

Commit

Permalink
Listen to req close
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Apr 23, 2021
1 parent 85a2031 commit 9f05d7c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 42 deletions.
20 changes: 16 additions & 4 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,15 +924,27 @@ export default class Server {
target,
changeOrigin: true,
ignorePath: true,
proxyTimeout: 30_000, // limit proxying to 30 seconds
})

await new Promise((proxyResolve, proxyReject) => {
proxy.web(req, res, undefined, (err) => {
if (err) {
return proxyResolve(err)
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)
}
proxyReject(true)
})
proxy.web(req, res)
})

return {
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
51 changes: 13 additions & 38 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 Expand Up @@ -2024,42 +2037,4 @@ describe('Custom routes', () => {
runSoloTests()
})
})

it('should not hang when proxy rewrite fails', async () => {
try {
await fs.rename(
join(appDir, 'next.config.js'),
join(appDir, 'next.config.js.bak')
)
await fs.writeFile(
join(appDir, 'next.config.js'),
`
module.exports = {
rewrites() {
return [
{
source: '/to-nowhere',
destination: 'http://localhost:1234'
}
]
}
}
`
)
appPort = await findPort()
app = await launchApp(appDir, appPort)

const res = await fetchViaHTTP(appPort, '/to-nowhere', undefined, {
timeout: 5000,
})

expect(res.status).toBe(500)
} finally {
await fs.remove(join(appDir, 'next.config.js'))
await fs.rename(
join(appDir, 'next.config.js.bak'),
join(appDir, 'next.config.js')
)
}
})
})

0 comments on commit 9f05d7c

Please sign in to comment.