Schneller, Ramez
This commit is contained in:
37
proxy.ts
Normal file
37
proxy.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { NextResponse, NextRequest } from 'next/server';
|
||||
import { routing } from '@/i18n/routing';
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
"/((?!api|_next|.*\\..*).*)"
|
||||
]
|
||||
};
|
||||
|
||||
// Proxy für geschützte Routen + i18n + Cookies
|
||||
export async function proxy(request: NextRequest) {
|
||||
const locale = routing.locales.find(locale =>
|
||||
request.nextUrl.pathname.startsWith(`/${locale}`)
|
||||
);
|
||||
|
||||
if (!locale) {
|
||||
const newLang =
|
||||
request.headers
|
||||
.get('accept-language')
|
||||
?.split(',')[0]
|
||||
?.split('-')[0]
|
||||
?? 'en';
|
||||
|
||||
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 }
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user