2023-08-08 02:06:41 +00:00
|
|
|
import {AVAILABLE_LOCALES} from "@/app/(i18n)/(locales)"
|
2023-09-29 07:32:42 +00:00
|
|
|
import Negotiator from "negotiator"
|
2023-08-08 02:06:41 +00:00
|
|
|
import {NextRequest, NextResponse} from "next/server"
|
|
|
|
|
2023-09-29 07:32:42 +00:00
|
|
|
|
2023-08-08 02:06:41 +00:00
|
|
|
export function middleware(request: NextRequest) {
|
|
|
|
// https://nextjs.org/docs/app/building-your-application/routing/internationalization
|
|
|
|
const pathname = request.nextUrl.pathname
|
|
|
|
const pathnameIsMissingLocale = AVAILABLE_LOCALES.every(
|
|
|
|
(locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
|
|
|
|
)
|
|
|
|
if(!pathnameIsMissingLocale) {
|
|
|
|
return NextResponse.next()
|
|
|
|
}
|
|
|
|
const negotiator = new Negotiator(request as any)
|
|
|
|
const bestLocale = negotiator.language(AVAILABLE_LOCALES)
|
|
|
|
return NextResponse.rewrite(`${request.nextUrl.protocol}//${request.nextUrl.host}/${bestLocale}${pathname}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
matcher: [
|
2023-08-08 02:30:03 +00:00
|
|
|
'/((?!api|_next|manifest.json|logo-[nw]bg-[0-9]*.png|favicon-[nw]bg.ico).*)',
|
2023-08-08 02:06:41 +00:00
|
|
|
]
|
|
|
|
}
|