From 4369102a754a55417aff1dd34b8338eea1c16e7d Mon Sep 17 00:00:00 2001 From: v4ltages Date: Sat, 2 May 2026 21:12:17 +0300 Subject: [PATCH] Weird middleware issues with pages loading dev env --- src/middleware.ts | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index c7fbd95..3a5b4ca 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,20 +1,43 @@ -import createMiddleware from 'next-intl/middleware'; -import {routing} from './i18n/routing'; +import createMiddleware from "next-intl/middleware"; +import { NextRequest } from "next/server"; +import { routing } from "./i18n/routing"; -export default createMiddleware(routing); +const handleI18nRouting = createMiddleware(routing); + +export default function middleware(request: NextRequest) { + const response = handleI18nRouting(request); + + const location = response.headers.get("location"); + + if (location) { + const url = new URL(location); + + // Keep localhost development redirects intact, but drop leaked :3000 + // on public domains when upstream proxy forwards an internal port. + const isLocalhost = + url.hostname === "localhost" || url.hostname === "127.0.0.1"; + + if (!isLocalhost && url.port === "3000") { + url.port = ""; + response.headers.set("location", url.toString()); + } + } + + return response; +} export const config = { // Match only internationalized pathnames matcher: [ // Enable a redirect to a matching locale at the root - '/', + "/", // Set a cookie to remember the previous locale for // all requests that have a locale prefix - '/(et|en)/:path*', + "/(et|en)/:path*", // Enable redirects that add missing locales // (e.g. `/pathnames` -> `/en/pathnames`) - '/((?!_next|_vercel|.*\\..*).*)' - ] + "/((?!_next|_vercel|.*\\..*).*)", + ], };