Skip to content

Commit d0fe7c2

Browse files
committed
fix: add try catch on locale helper on middleware
1 parent 0463ca7 commit d0fe7c2

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

pwa/middleware.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@ import { match as matchLocale } from "@formatjs/intl-localematcher";
44
import Negotiator from "negotiator";
55

66
function getLocale(request: NextRequest): string | undefined {
7-
// Negotiator expects plain object so we need to transform headers
8-
const negotiatorHeaders: Record<string, string> = {};
9-
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
7+
try {
8+
// Negotiator expects plain object so we need to transform headers
9+
const negotiatorHeaders: Record<string, string> = {};
10+
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
1011

11-
// Use negotiator and intl-localematcher to get best locale
12-
const languages = new Negotiator({ headers: negotiatorHeaders }).languages();
13-
const locales = [...i18n.locales];
14-
const locale = matchLocale(languages, locales, i18n.defaultLocale);
15-
return ([...i18n.locales] as string[]).includes(locale)
16-
? locale
17-
: i18n.defaultLocale;
12+
// Use negotiator and intl-localematcher to get best locale
13+
const languages = new Negotiator({
14+
headers: negotiatorHeaders,
15+
}).languages();
16+
const locales = [...i18n.locales];
17+
const locale = matchLocale(languages, locales, i18n.defaultLocale);
18+
return ([...i18n.locales] as string[]).includes(locale)
19+
? locale
20+
: i18n.defaultLocale;
21+
} catch (e) {
22+
console.error(e);
23+
return i18n.defaultLocale;
24+
}
1825
}
1926

2027
export async function middleware(request: NextRequest) {

0 commit comments

Comments
 (0)