🐛 Fix matcher throwing error

This commit is contained in:
Luke Vella 2025-03-14 13:30:45 +00:00
parent 5bf579a62f
commit 153062f91c
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
3 changed files with 12 additions and 6 deletions

View file

@ -19,7 +19,7 @@ const handler = async (req: NextRequest) => {
req, req,
router: appRouter, router: appRouter,
createContext: async () => { createContext: async () => {
const locale = await getPreferredLocale(req); const locale = getPreferredLocale(req);
const user = session?.user const user = session?.user
? { ? {
id: session.user.id, id: session.user.id,

View file

@ -25,7 +25,7 @@ export const middleware = withAuth(async (req) => {
newUrl.pathname = `/${locale}${newUrl.pathname}`; newUrl.pathname = `/${locale}${newUrl.pathname}`;
} else { } else {
// Check if locale is specified in header // Check if locale is specified in header
locale = await getPreferredLocale(req); locale = getPreferredLocale(req);
newUrl.pathname = `/${locale}${newUrl.pathname}`; newUrl.pathname = `/${locale}${newUrl.pathname}`;
} }

View file

@ -5,13 +5,19 @@ import type { NextRequest } from "next/server";
const locales = Object.keys(languages); const locales = Object.keys(languages);
export async function getPreferredLocale(req: NextRequest) { export function getPreferredLocale(req: NextRequest) {
const preferredLanguages = new Negotiator({ const preferredLanguages = new Negotiator({
headers: { headers: {
"accept-language": req.headers.get("accept-language") ?? "", "accept-language": req.headers.get("accept-language") ?? "",
}, },
}).languages(); })
.languages()
.filter((lang) => lang !== "*");
const locale = match(preferredLanguages, locales, defaultLocale); try {
return locale; return match(preferredLanguages, locales, defaultLocale);
} catch (e) {
console.warn("Failed to match locale", e);
return defaultLocale;
}
} }