Weird middleware issues with pages loading dev env

This commit is contained in:
2026-05-02 21:12:17 +03:00
parent 8c2abab821
commit 4369102a75

View File

@@ -1,20 +1,43 @@
import createMiddleware from 'next-intl/middleware'; import createMiddleware from "next-intl/middleware";
import {routing} from './i18n/routing'; 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 = { export const config = {
// Match only internationalized pathnames // Match only internationalized pathnames
matcher: [ matcher: [
// Enable a redirect to a matching locale at the root // Enable a redirect to a matching locale at the root
'/', "/",
// Set a cookie to remember the previous locale for // Set a cookie to remember the previous locale for
// all requests that have a locale prefix // all requests that have a locale prefix
'/(et|en)/:path*', "/(et|en)/:path*",
// Enable redirects that add missing locales // Enable redirects that add missing locales
// (e.g. `/pathnames` -> `/en/pathnames`) // (e.g. `/pathnames` -> `/en/pathnames`)
'/((?!_next|_vercel|.*\\..*).*)' "/((?!_next|_vercel|.*\\..*).*)",
] ],
}; };