From 03925a952d750bd9d3457a4e4bcc5f2e9057dcb3 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Wed, 21 Jun 2023 17:04:33 +0900 Subject: [PATCH] fix(proxy): forward SSE close event --- packages/vite/src/node/server/middlewares/proxy.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/vite/src/node/server/middlewares/proxy.ts b/packages/vite/src/node/server/middlewares/proxy.ts index ae289a8f8e9776..6b282e0afea05b 100644 --- a/packages/vite/src/node/server/middlewares/proxy.ts +++ b/packages/vite/src/node/server/middlewares/proxy.ts @@ -91,6 +91,17 @@ export function proxyMiddleware( }) }) + // https://github.com/http-party/node-http-proxy/issues/1520#issue-877626125 + // https://github.com/chimurai/http-proxy-middleware/blob/cd58f962aec22c925b7df5140502978da8f87d5f/src/plugins/default/debug-proxy-errors-plugin.ts#L25-L37 + proxy.on('proxyRes', (proxyRes, req, res) => { + res.on('close', () => { + if (!res.writableEnded) { + debug?.('destroying proxyRes in proxyRes close event') + proxyRes.destroy() + } + }) + }) + // clone before saving because http-proxy mutates the options proxies[context] = [proxy, { ...opts }] })