This commit is contained in:
2026-03-26 08:09:01 +03:30
parent 5b10807004
commit 333bc0c69e
37 changed files with 940 additions and 575 deletions

View File

@@ -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|.*\\..*).*)"],
};