Skip to content

Commit 562ed48

Browse files
ovflowdstyfle
andauthored
chore: node_env as global env (#5613)
Co-authored-by: Steven <[email protected]>
1 parent 26a3975 commit 562ed48

File tree

6 files changed

+48
-120
lines changed

6 files changed

+48
-120
lines changed

middleware.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
import { NextResponse } from 'next/server';
2-
import createMiddleware from './next.middleware';
3-
import detectLanguage from './middlewares/detectLanguage';
2+
import { availableLocales } from './next.locales.mjs';
3+
import type { NextRequest } from 'next/server';
44

5-
const nextMiddleware = createMiddleware(NextResponse);
5+
// This Middleware is responsible for handling automatic language detection from a user's Browser
6+
// This middleware should only run on "/" requests coming to the Website
7+
export const middleware = async (request: NextRequest) => {
8+
const { pathname, search } = request.nextUrl;
69

7-
const { middleware, matcher } = nextMiddleware([detectLanguage]);
10+
// This function allows us to redirect with a Locale Code
11+
const redirectWithLocale = (_locale: string) => {
12+
const redirectUrl = `/${_locale}${pathname}${search}`;
813

9-
export { middleware, matcher };
14+
return NextResponse.redirect(new URL(redirectUrl, request.url));
15+
};
16+
17+
const localeCookie = request.cookies.get('NEXT_LOCALE');
18+
19+
// If we already have a NEXT_LOCALE Cookie, then Redirect to the stored Locale Code
20+
if (localeCookie?.value && localeCookie.value !== 'default') {
21+
return redirectWithLocale(localeCookie.value);
22+
}
23+
24+
// If not, we try to check if the Browser is sending `Accept-Language` Header
25+
const acceptedLanguagesRaw = request.headers.get('Accept-Language') || 'en';
26+
27+
// If present, we try to split the format into ['code', 'q=order', ...]
28+
// Where q= is the precedence of the Accepted Language
29+
// We then filter those out as we don't want them
30+
const acceptedLanguages = acceptedLanguagesRaw
31+
.split(';')
32+
.map(collection => collection.split(','))
33+
.flat()
34+
.filter(locale => !locale.startsWith('q='));
35+
36+
// We check if we have any matching Language in the order of preference given
37+
// And if yes, we return that Locale Code
38+
const matchedLocaleCode = acceptedLanguages.find(acceptedLocale =>
39+
availableLocales.some(locale => locale.code === acceptedLocale)
40+
);
41+
42+
// We create a new Response Object containing the Locale Match or the default Language
43+
const responseWithCookie = redirectWithLocale(matchedLocaleCode || 'en');
44+
45+
// Then we set a Cookie to avoid this calculation from happening on every / hit
46+
responseWithCookie.cookies.set('NEXT_LOCALE', matchedLocaleCode || 'en');
47+
48+
return responseWithCookie;
49+
};
50+
51+
export const config = { matcher: '/' };

middlewares/detectLanguage.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

next.middleware.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

turbo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"$schema": "https://turbo.build/schema.json",
3+
"globalEnv": ["NODE_ENV"],
34
"pipeline": {
45
"serve": {
56
"cache": false,

types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ export * from './layouts';
99
export * from './navigation';
1010
export * from './prevNextLink';
1111
export * from './releases';
12-
export * from './middlewares';
1312
export * from './dynamic';

types/middlewares.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)