From 9f05d7c1ccd35e5747633b0fe4e9c1224f64c3ef Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Fri, 23 Apr 2021 16:25:58 -0500 Subject: [PATCH] Listen to req close --- .../next/next-server/server/next-server.ts | 20 ++++++-- test/integration/custom-routes/next.config.js | 4 ++ .../custom-routes/test/index.test.js | 51 +++++-------------- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index dd84869b5688a..7c5d7b559df9c 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -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 { diff --git a/test/integration/custom-routes/next.config.js b/test/integration/custom-routes/next.config.js index c5ac688266816..7ba28f11ce1f2 100644 --- a/test/integration/custom-routes/next.config.js +++ b/test/integration/custom-routes/next.config.js @@ -11,6 +11,10 @@ module.exports = { }, ] : []), + { + source: '/to-nowhere', + destination: 'http://localhost:12233', + }, { source: '/rewriting-to-auto-export', destination: '/auto-export/hello?rewrite=1', diff --git a/test/integration/custom-routes/test/index.test.js b/test/integration/custom-routes/test/index.test.js index 8535584b92677..cd8f2161c5937 100644 --- a/test/integration/custom-routes/test/index.test.js +++ b/test/integration/custom-routes/test/index.test.js @@ -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) @@ -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$'), @@ -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') - ) - } - }) })