Skip to content

Commit

Permalink
optimize middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Dec 7, 2022
1 parent 454c01c commit 349bc18
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.next
node_modules
node_modules
.vscode
.next
2 changes: 1 addition & 1 deletion app/i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const initI18next = async (lng, ns) => {
.use(initReactI18next)
.use(resourcesToBackend((language, namespace) => import(`./locales/${language}/${namespace}.json`)))
.init({
debug: true,
// debug: true,
supportedLngs: languages,
// preload: languages,
fallbackLng,
Expand Down
7 changes: 4 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { NextRequest, NextResponse } from 'next/server';
import acceptLanguage from 'accept-language';
import { fallbackLng, languages } from './app/i18n'

acceptLanguage.languages(['en', 'de']);
acceptLanguage.languages(languages);

export const config = {
matcher: ['/'],
};

export function middleware(req: NextRequest) {
const lang = acceptLanguage.get(req.headers.get('Accept-Language'));
const lng = acceptLanguage.get(req.headers.get('Accept-Language')) || fallbackLng;

if (req.nextUrl.pathname === '/') {
return NextResponse.rewrite(new URL(`/${lang}`, req.url));
return NextResponse.redirect(new URL(`/${lng}`, req.url));
}

return NextResponse.next();
Expand Down

0 comments on commit 349bc18

Please sign in to comment.