|
1 | 1 | import { NextResponse } from 'next/server'; |
2 | | -import localeConfig from './i18n/config.json'; |
3 | | -import type { NextRequest } from 'next/server'; |
| 2 | +import createMiddleware from './next.middleware'; |
4 | 3 |
|
5 | | -// As set of available and enabled locales for the website |
6 | | -// This is used for allowing us to redirect the user to any |
7 | | -// of the available locales that we have enabled on the website |
8 | | -const localeCodes = localeConfig |
9 | | - .filter(locale => locale.enabled) |
10 | | - .map(locale => locale.code); |
| 4 | +const nextMiddleware = createMiddleware(NextResponse); |
11 | 5 |
|
12 | | -export async function middleware(req: NextRequest) { |
13 | | - const { pathname, search } = req.nextUrl; |
| 6 | +const { middleware, matcher } = nextMiddleware([ |
| 7 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 8 | + require('./middlewares/detectLanguage').default, |
| 9 | +]); |
14 | 10 |
|
15 | | - // This function allows us to redirect with a Locale Code |
16 | | - const redirectWithLocale = (locale: string) => { |
17 | | - const redirectUrl = `/${locale}${pathname}${search}`; |
18 | | - |
19 | | - return NextResponse.redirect(new URL(redirectUrl, req.url)); |
20 | | - }; |
21 | | - |
22 | | - const localeCookie = req.cookies.get('NEXT_LOCALE'); |
23 | | - |
24 | | - // If we already have a NEXT_LOCALE Cookie, then Redirect to the stored Locale Code |
25 | | - if (localeCookie && localeCookie.value) { |
26 | | - return redirectWithLocale(localeCookie.value); |
27 | | - } |
28 | | - |
29 | | - // If not, we try to check if the Browser is sending `Accept-Language` Header |
30 | | - const acceptedLanguagesRaw = req.headers.get('Accept-Language') || 'en'; |
31 | | - |
32 | | - // If present, we try to split the format into ['code', 'q=order', ...] |
33 | | - // Where q= is the precedence of the Accepted Language |
34 | | - // We then filter those out as we don't want them |
35 | | - const acceptedLanguages = acceptedLanguagesRaw |
36 | | - .split(';') |
37 | | - .map(collection => collection.split(',')) |
38 | | - .flat() |
39 | | - .filter(locale => !locale.startsWith('q=')); |
40 | | - |
41 | | - // We check if we have any matching Language in the order of preference given |
42 | | - // And if yes, we return that Locale Code |
43 | | - const matchedLocaleCode = acceptedLanguages.find(acceptedLocale => |
44 | | - localeCodes.some(supportedLocale => supportedLocale === acceptedLocale) |
45 | | - ); |
46 | | - |
47 | | - // We create a new Response Object containing the Locale Match or the default Language |
48 | | - const responseWithCookie = redirectWithLocale(matchedLocaleCode || 'en'); |
49 | | - |
50 | | - // Then we set a Cookie to avoid this calculation from happening on every / hit |
51 | | - responseWithCookie.cookies.set('NEXT_LOCALE', matchedLocaleCode || 'en'); |
52 | | - |
53 | | - return responseWithCookie; |
54 | | -} |
55 | | - |
56 | | -export const config = { matcher: '/' }; |
| 11 | +export { middleware, matcher }; |
0 commit comments