fix(middleware): resolve 0.0.0.0 hydration issues and add header logging
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
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
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import createMiddleware from 'next-intl/middleware';
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
import { NextResponse, NextRequest } from 'next/server';
|
||||
|
||||
// Create the internationalization middleware
|
||||
const intlMiddleware = createMiddleware({
|
||||
@@ -11,23 +10,43 @@ const intlMiddleware = createMiddleware({
|
||||
defaultLocale: 'en',
|
||||
});
|
||||
|
||||
// Main middleware that logs all requests
|
||||
export default function middleware(request: NextRequest) {
|
||||
const startTime = Date.now();
|
||||
const { method, url, headers } = request;
|
||||
const userAgent = headers.get('user-agent') || 'unknown';
|
||||
const referer = headers.get('referer') || 'none';
|
||||
const ip = headers.get('x-forwarded-for') || headers.get('x-real-ip') || 'unknown';
|
||||
|
||||
// Log incoming request
|
||||
console.log(`Incoming request: method=${method} host=${headers.get('host')} url=${url}`);
|
||||
// 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(request);
|
||||
const response = intlMiddleware(effectiveRequest);
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(`Request failed: method=${method} url=${url}`, error);
|
||||
console.error(
|
||||
`Request failed: method=${method} url=${url} headers=${JSON.stringify(headerObj)}`,
|
||||
error,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user