Files
moygroup/proxy.ts
T
2026-06-03 16:13:15 +03:00

38 lines
1.0 KiB
TypeScript

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { i18n } from './app/dictionaries';
function getLocale(request: NextRequest): string {
// Always return default locale for now (can be improved to read headers)
return i18n.defaultLocale;
}
export function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
// Skip public files and _next requests
if (
pathname.startsWith('/_next') ||
pathname.includes('.') ||
pathname.startsWith('/api')
) {
return;
}
// Check if there is any supported locale in the pathname
const pathnameIsMissingLocale = i18n.locales.every(
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
);
if (pathnameIsMissingLocale) {
const locale = getLocale(request);
return NextResponse.redirect(
new URL(`/${locale}${pathname === '/' ? '' : pathname}`, request.url)
);
}
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};