Files
klz-cables.com/middleware.ts
Marc Mintel 24eea9a2fe
Some checks failed
Build & Deploy KLZ Cables / 🔍 Prepare Environment (push) Successful in 7s
Build & Deploy KLZ Cables / 🧪 Quality Assurance (push) Successful in 1m29s
Build & Deploy KLZ Cables / 🏗️ Build Gatekeeper (push) Successful in 20s
Build & Deploy KLZ Cables / 🏗️ Build App (push) Successful in 4m32s
Build & Deploy KLZ Cables / 🚀 Deploy (push) Successful in 48s
Build & Deploy KLZ Cables / 🔔 Notifications (push) Has been cancelled
Build & Deploy KLZ Cables / ⚡ PageSpeed (push) Has been cancelled
fix(middleware): resolve 0.0.0.0 hydration issues and add header logging
2026-02-06 12:22:32 +01:00

58 lines
1.7 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('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*'],
};