Redirect locale using the public Host header, not NextURL.origin.
A relative Location is invalid in Next.js 15 middleware; the Host header is the public site while nextUrl stays on the PM2 bind address. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
7034deda89
commit
63f1aef752
+13
-6
@@ -16,12 +16,19 @@ export function middleware(request: NextRequest) {
|
||||
);
|
||||
if (hasLocale || PUBLIC_FILE.test(pathname)) return NextResponse.next();
|
||||
|
||||
// Relative Location keeps the public host; NextURL.origin is the PM2 bind address.
|
||||
const location = `/${defaultLocale}${pathname === "/" ? "" : pathname}${request.nextUrl.search}`;
|
||||
return new NextResponse(null, {
|
||||
status: 307,
|
||||
headers: { Location: location },
|
||||
});
|
||||
// nextUrl.origin is the PM2 bind address (localhost:3024). Use the public Host.
|
||||
const host =
|
||||
request.headers.get("x-forwarded-host")?.split(",")[0]?.trim() ||
|
||||
request.headers.get("host");
|
||||
const proto =
|
||||
request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim() || "https";
|
||||
const origin = host ? `${proto}://${host}` : request.nextUrl.origin;
|
||||
const dest = new URL(
|
||||
`/${defaultLocale}${pathname === "/" ? "" : pathname}`,
|
||||
origin,
|
||||
);
|
||||
dest.search = request.nextUrl.search;
|
||||
return NextResponse.redirect(dest);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
|
||||
Reference in New Issue
Block a user