mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-28 17:56:37 +02:00
🐛 Fix matcher throwing error
This commit is contained in:
parent
5bf579a62f
commit
153062f91c
3 changed files with 12 additions and 6 deletions
|
@ -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,
|
||||||
|
|
|
@ -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}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue