1
1
mirror of https://github.com/theoludwig/theoludwig.git synced 2024-11-03 20:11:30 +01:00

fix: try/catch inside middleware when checking locale

This commit is contained in:
Théo LUDWIG 2023-08-24 23:09:33 +02:00
parent 137cceffa1
commit 35211fa279
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B

View File

@ -17,12 +17,16 @@ export const middleware = (request: NextRequest): NextResponse => {
let locale = request.cookies.get('locale')?.value
if (locale == null || !LOCALES.includes(locale as Locale)) {
const headers = {
'accept-language':
request.headers.get('accept-language') ?? DEFAULT_LOCALE
try {
const headers = {
'accept-language':
request.headers.get('accept-language') ?? DEFAULT_LOCALE
}
const languages = new Negotiator({ headers }).languages()
locale = match(languages, LOCALES.slice(), DEFAULT_LOCALE)
} catch {
locale = DEFAULT_LOCALE
}
const languages = new Negotiator({ headers }).languages()
locale = match(languages, LOCALES.slice(), DEFAULT_LOCALE)
response.cookies.set('locale', locale, {
path: '/',
maxAge: COOKIE_MAX_AGE