Untitled
Anonymous
plain_text
07/23/2024 2:02 PM
1.0 KB
19
Indexable
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname
const pathnameIsMissingLocale = ['en', 'fr', 'es'].every(
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
)
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
const locale = request.headers.get('accept-language')?.split(',')[0].split('-')[0] || 'en'
// If the locale is 'en', don't add it to the URL
if (locale === 'en') {
return NextResponse.next()
}
return NextResponse.redirect(new URL(`/${locale}${pathname}`, request.url))
}
}
export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next).*)',
// Optional: only run on root (/) URL
// '/'
],
}Editor is loading...
Leave a Comment