Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: node_env as global env #5613

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
import { NextResponse } from 'next/server';
import createMiddleware from './next.middleware';
import detectLanguage from './middlewares/detectLanguage';
import { availableLocales } from './next.locales.mjs';
import type { NextRequest } from 'next/server';

const nextMiddleware = createMiddleware(NextResponse);
// 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;

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

export { middleware, matcher };
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: '/' };
51 changes: 0 additions & 51 deletions middlewares/detectLanguage.ts

This file was deleted.

39 changes: 0 additions & 39 deletions next.middleware.ts

This file was deleted.

1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["NODE_ENV"],
"pipeline": {
"serve": {
"cache": false,
Expand Down
1 change: 0 additions & 1 deletion types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ export * from './layouts';
export * from './navigation';
export * from './prevNextLink';
export * from './releases';
export * from './middlewares';
export * from './dynamic';
24 changes: 0 additions & 24 deletions types/middlewares.ts

This file was deleted.

Loading