Files
moygroup/proxy.ts
T

36 lines
986 B
TypeScript

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { i18n } from './app/dictionaries';
function getLocale(): 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, _next requests, api and admin routes
if (
pathname.startsWith('/_next') ||
pathname.includes('.') ||
pathname.startsWith('/api') ||
pathname.startsWith('/admin')
) {
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();
return NextResponse.redirect(
new URL(`/${locale}${pathname === '/' ? '' : pathname}`, request.url)
);
}
}