33 lines
828 B
TypeScript
33 lines
828 B
TypeScript
import { NextResponse } from 'next/server'
|
|
import type { NextRequest } from 'next/server'
|
|
import { locales, defaultLocale } from '@/lib/i18n/locales'
|
|
|
|
function hasLocale(pathname: string) {
|
|
const first = pathname.split('/')[1]
|
|
return (locales as readonly string[]).includes(first)
|
|
}
|
|
|
|
export default function proxy(request: NextRequest) {
|
|
const { pathname } = request.nextUrl
|
|
|
|
// Ignore next internals and assets
|
|
if (
|
|
pathname.startsWith('/_next') ||
|
|
pathname.startsWith('/api') ||
|
|
pathname.includes('.') // static files
|
|
) {
|
|
return
|
|
}
|
|
|
|
// Add default locale if missing
|
|
if (!hasLocale(pathname)) {
|
|
const url = request.nextUrl.clone()
|
|
url.pathname = `/${defaultLocale}${pathname}`
|
|
return NextResponse.redirect(url)
|
|
}
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/((?!_next|.*\..*).*)'],
|
|
}
|