Files
klz-cables.com/middleware.ts
Marc Mintel b74f6b6f9e
All checks were successful
Build & Deploy KLZ Cables / 🔍 Prepare Environment (push) Successful in 7s
Build & Deploy KLZ Cables / 🧪 Quality Assurance (push) Successful in 1m30s
Build & Deploy KLZ Cables / 🏗️ Build Gatekeeper (push) Successful in 21s
Build & Deploy KLZ Cables / 🏗️ Build App (push) Successful in 4m38s
Build & Deploy KLZ Cables / 🚀 Deploy (push) Successful in 50s
Build & Deploy KLZ Cables / ⚡ PageSpeed (push) Successful in 7m12s
Build & Deploy KLZ Cables / 🔔 Notifications (push) Successful in 2s
fix(middleware): strip port 3000 from reconstructed URL to prevent hydration mismatch
2026-02-06 12:35:33 +01:00

61 lines
1.9 KiB
TypeScript

import createMiddleware from 'next-intl/middleware';
import { NextResponse, NextRequest } from 'next/server';
// Create the internationalization middleware
const intlMiddleware = createMiddleware({
// A list of all locales that are supported
locales: ['en', 'de'],
// Used when no locale matches
defaultLocale: 'en',
});
export default function middleware(request: NextRequest) {
const { method, url, headers } = request;
// Build header object for logging
const headerObj: Record<string, string> = {};
headers.forEach((value, key) => {
headerObj[key] = value;
});
// Defensive URL correction
// If the URL contains 0.0.0.0 (internal IP), we rebuild it using the Host header
let effectiveRequest = request;
if (url.includes('0.0.0.0')) {
const proto = headers.get('x-forwarded-proto') || 'https';
const host = headers.get('x-forwarded-host') || headers.get('host') || 'testing.klz-cables.com';
const newUrl = new URL(url);
newUrl.protocol = proto;
// 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')} | Forwarded Host: ${headers.get('x-forwarded-host')}`,
);
}
try {
// Apply internationalization middleware
const response = intlMiddleware(effectiveRequest);
return response;
} catch (error) {
console.error(
`Request failed: method=${method} url=${url} headers=${JSON.stringify(headerObj)}`,
error,
);
throw error;
}
}
export const config = {
// Match only internationalized pathnames
matcher: ['/((?!api|_next|_vercel|health|.*\\..*).*)', '/', '/(de|en)/:path*'],
};