Files
gsh-website/proxy.ts
BuildTools f765e9de71 Jetzt aber
2025-12-20 00:08:20 +01:00

42 lines
953 B
TypeScript

import { NextResponse, NextRequest } from 'next/server';
import { routing } from '@/i18n/routing';
export const config = {
matcher: [
"/((?!api|_next|.*\\..*).*)"
]
};
export async function proxy(request: NextRequest) {
const locale = routing.locales.find(locale =>
request.nextUrl.pathname.startsWith(`/${locale}`)
);
if (!locale) {
let newLang =
request.headers
.get('accept-language')
?.split(',')[0]
?.split('-')[0];
if (!newLang)
newLang = routing.defaultLocale;
else {
const langAvailable = (routing.locales as readonly string[]).includes(newLang);
if (!langAvailable) newLang = routing.defaultLocale;
}
const redirect = `/${newLang}`
+ request.nextUrl.pathname
+ request.nextUrl.search;
return NextResponse.redirect(new URL(redirect, request.url));
}
const headers = new Headers();
headers.set('X-NEXT-INTL-LOCALE', locale);
return NextResponse.next({
request: { headers }
});
}