refactor: apply prettier conf and cleanup

This commit is contained in:
Sonny
2023-11-26 02:04:09 +01:00
parent c0af440302
commit 9a5f661df4
120 changed files with 1229 additions and 9101 deletions

View File

@@ -1,40 +1,40 @@
import acceptLanguage from "accept-language";
import { NextResponse } from "next/server";
import { i18n } from "./next-i18next.config";
import acceptLanguage from 'accept-language';
import { NextResponse } from 'next/server';
import { i18n } from './next-i18next.config';
acceptLanguage.languages(i18n.locales);
export const config = {
matcher: ["/((?!api|_next/static|_next/image|assets|favicon.ico|sw.js).*)"],
matcher: ['/((?!api|_next/static|_next/image|assets|favicon.ico|sw.js).*)'],
};
const cookieName = "i18next";
const cookieName = 'i18next';
// Source : https://github.com/i18next/next-app-dir-i18next-example/blob/3d653a46ae33f46abc011b6186f7a4595b84129f/middleware.js
export function middleware(req) {
if (
req.nextUrl.pathname.indexOf("icon") > -1 ||
req.nextUrl.pathname.indexOf("chrome") > -1
req.nextUrl.pathname.indexOf('icon') > -1 ||
req.nextUrl.pathname.indexOf('chrome') > -1
)
return NextResponse.next();
let lng;
if (req.cookies.has(cookieName))
lng = acceptLanguage.get(req.cookies.get(cookieName).value);
if (!lng) lng = acceptLanguage.get(req.headers.get("Accept-Language"));
if (!lng) lng = acceptLanguage.get(req.headers.get('Accept-Language'));
if (!lng) lng = i18n.defaultLocale;
// Redirect if lng in path is not supported
if (
!i18n.locales.some((loc) => req.nextUrl.pathname.startsWith(`/${loc}`)) &&
!req.nextUrl.pathname.startsWith("/_next")
!req.nextUrl.pathname.startsWith('/_next')
) {
return NextResponse.redirect(
new URL(`/${lng}${req.nextUrl.pathname}`, req.url),
);
}
if (req.headers.has("referer")) {
const refererUrl = new URL(req.headers.get("referer"));
if (req.headers.has('referer')) {
const refererUrl = new URL(req.headers.get('referer'));
const lngInReferer = i18n.locales.find((l) =>
refererUrl.pathname.startsWith(`/${l}`),
);