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 = {}; 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('host') || 'testing.klz-cables.com'; const newUrl = new URL(url); newUrl.protocol = proto; newUrl.host = host; 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)}`, ); } 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*'], };