update
This commit is contained in:
@@ -1,32 +1,31 @@
|
||||
import {NextResponse} from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const locales = ["en", "fa","ar"];
|
||||
const DEFAULT_LOCALE = "fa"; // fallback امن
|
||||
|
||||
export function middleware(request) {
|
||||
const {pathname} = request.nextUrl;
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
if (
|
||||
pathname.startsWith("/_next") ||
|
||||
pathname.startsWith("/favicon.ico") ||
|
||||
pathname.startsWith("/robots.txt") ||
|
||||
pathname.match(/^\/.*\.(png|jpg|jpeg|gif|svg|webp|ico)$/)
|
||||
pathname.startsWith("/api") ||
|
||||
pathname.match(/\.(.*)$/)
|
||||
) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const pathnameHasLocale = locales.some(
|
||||
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
||||
);
|
||||
if (pathnameHasLocale) return NextResponse.next();
|
||||
const segments = pathname.split("/");
|
||||
const maybeLang = segments[1];
|
||||
|
||||
const locale = "fa";
|
||||
const url = request.nextUrl.clone();
|
||||
url.pathname = `/${locale}${pathname}`;
|
||||
return NextResponse.redirect(url);
|
||||
// فقط چک کن آیا prefix داره یا نه
|
||||
if (!maybeLang) {
|
||||
return NextResponse.redirect(
|
||||
new URL(`/${DEFAULT_LOCALE}${pathname}`, request.url)
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// export const config = {
|
||||
// matcher: [
|
||||
// "/((?!_next|favicon.ico|robots.txt|.*\\.(png|jpg|jpeg|gif|svg|webp|ico)).*)",
|
||||
// ],
|
||||
// };
|
||||
export const config = {
|
||||
matcher: ["/((?!_next|.*\\..*).*)"],
|
||||
};
|
||||
Reference in New Issue
Block a user