From e9e03c217ae4a18af45fb5e9e27b56d17ded894c Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Sun, 12 Apr 2026 11:14:15 +0200 Subject: [PATCH] fix(middleware): avoid manual NextRequest with body for GET/HEAD to resolve failed to pipe response error (160k occurrences) --- middleware.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/middleware.ts b/middleware.ts index 2261faad..62cd5429 100644 --- a/middleware.ts +++ b/middleware.ts @@ -52,16 +52,20 @@ export default async function middleware(request: NextRequest) { urlObj.protocol = proto; - effectiveRequest = new NextRequest(urlObj, { - headers: request.headers, - method: request.method, - body: request.body, - }); + // Only create a new request for GET/HEAD to avoid consuming the body stream on POST/PUT + // This resolves the "failed to pipe response" error (160k occurrences in GlitchTip) + if (['GET', 'HEAD'].includes(request.method)) { + effectiveRequest = new NextRequest(urlObj, { + headers: request.headers, + method: request.method, + // No body for GET/HEAD + }); - if (process.env.NODE_ENV !== 'production' || !process.env.CI) { - console.log( - `🛡️ Proxy: Fixed internal URL leak: ${url} -> ${urlObj.toString()} | Proto: ${proto} | Host: ${hostHeader}`, - ); + if (process.env.NODE_ENV !== 'production' || !process.env.CI) { + console.log( + `🛡️ Proxy: Fixed internal URL leak: ${url} -> ${urlObj.toString()} | Proto: ${proto} | Host: ${hostHeader}`, + ); + } } }