28 lines
827 B
TypeScript
28 lines
827 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
const locales = ["tr", "en"];
|
|
const defaultLocale = "tr";
|
|
|
|
export function middleware(request: NextRequest) {
|
|
// Check if there is any supported locale in the pathname
|
|
const { pathname } = request.nextUrl;
|
|
|
|
const pathnameHasLocale = locales.some(
|
|
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
|
);
|
|
|
|
if (pathnameHasLocale) return;
|
|
|
|
// If no locale found, redirect to default locale
|
|
request.nextUrl.pathname = `/${defaultLocale}${pathname}`;
|
|
return NextResponse.redirect(request.nextUrl);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
// Skip all internal paths (_next) and static files
|
|
'/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
|
|
],
|
|
};
|