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();
|
if (hasLocale || PUBLIC_FILE.test(pathname)) return NextResponse.next();
|
||||||
|
|
||||||
// Relative Location keeps the public host; NextURL.origin is the PM2 bind address.
|
// nextUrl.origin is the PM2 bind address (localhost:3024). Use the public Host.
|
||||||
const location = `/${defaultLocale}${pathname === "/" ? "" : pathname}${request.nextUrl.search}`;
|
const host =
|
||||||
return new NextResponse(null, {
|
request.headers.get("x-forwarded-host")?.split(",")[0]?.trim() ||
|
||||||
status: 307,
|
request.headers.get("host");
|
||||||
headers: { Location: location },
|
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 = {
|
export const config = {
|
||||||
|
|||||||
Reference in New Issue
Block a user