forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
52 lines (39 loc) · 2.07 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { availableLocales } from './next.locales.mjs';
// This Middleware is responsible for handling automatic language detection from a user's Browser
// This middleware should only run on "/" requests coming to the Website
export const middleware = async (request: NextRequest) => {
const { pathname, search } = request.nextUrl;
// This function allows us to redirect with a Locale Code
const redirectWithLocale = (_locale: string) => {
const redirectUrl = `/${_locale}${pathname}${search}`;
return NextResponse.redirect(new URL(redirectUrl, request.url));
};
const localeCookie = request.cookies.get('NEXT_LOCALE');
// If we already have a NEXT_LOCALE Cookie, then Redirect to the stored Locale Code
if (localeCookie?.value && localeCookie.value !== 'default') {
return redirectWithLocale(localeCookie.value);
}
// If not, we try to check if the Browser is sending `Accept-Language` Header
const acceptedLanguagesRaw = request.headers.get('Accept-Language') || 'en';
// If present, we try to split the format into ['code', 'q=order', ...]
// Where q= is the precedence of the Accepted Language
// We then filter those out as we don't want them
const acceptedLanguages = acceptedLanguagesRaw
.split(';')
.map(collection => collection.split(','))
.flat()
.filter(locale => !locale.startsWith('q='));
// We check if we have any matching Language in the order of preference given
// And if yes, we return that Locale Code
const matchedLocaleCode = acceptedLanguages.find(acceptedLocale =>
availableLocales.some(locale => locale.code === acceptedLocale)
);
// We create a new Response Object containing the Locale Match or the default Language
const responseWithCookie = redirectWithLocale(matchedLocaleCode || 'en');
// Then we set a Cookie to avoid this calculation from happening on every / hit
responseWithCookie.cookies.set('NEXT_LOCALE', matchedLocaleCode || 'en');
return responseWithCookie;
};
export const config = { matcher: '/' };