From b74f6b6f9e3c87ebf28e1abd160abb1849b48988 Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Fri, 6 Feb 2026 12:35:33 +0100 Subject: [PATCH] fix(middleware): strip port 3000 from reconstructed URL to prevent hydration mismatch --- middleware.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/middleware.ts b/middleware.ts index 6e423e74..6f1bba2e 100644 --- a/middleware.ts +++ b/middleware.ts @@ -24,17 +24,20 @@ export default function middleware(request: NextRequest) { let effectiveRequest = request; if (url.includes('0.0.0.0')) { const proto = headers.get('x-forwarded-proto') || 'https'; - const host = headers.get('host') || 'testing.klz-cables.com'; + const host = headers.get('x-forwarded-host') || headers.get('host') || 'testing.klz-cables.com'; const newUrl = new URL(url); newUrl.protocol = proto; - newUrl.host = host; + // Split host to remove port if present + const [hostname] = host.split(':'); + newUrl.hostname = hostname; + newUrl.port = ''; // Explicitly clear the port to avoid leaking :3000 effectiveRequest = new NextRequest(newUrl, { headers: request.headers, method: request.method, body: request.body, }); console.log( - `Replaced 0.0.0.0 URL with: ${newUrl.toString()} | Original Host: ${headers.get('host')} | Headers: ${JSON.stringify(headerObj)}`, + `Replaced 0.0.0.0 URL with: ${newUrl.toString()} | Original Host: ${headers.get('host')} | Forwarded Host: ${headers.get('x-forwarded-host')}`, ); }