34 lines
911 B
TypeScript
34 lines
911 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) {
|
|
const { pathname } = request.nextUrl;
|
|
|
|
// Exclude static files, public folder items, API routes, next internals
|
|
if (
|
|
pathname.startsWith('/_next') ||
|
|
pathname.startsWith('/api') ||
|
|
pathname.includes('.') ||
|
|
pathname === '/favicon.ico'
|
|
) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const pathnameHasLocale = locales.some(
|
|
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
|
|
);
|
|
|
|
if (pathnameHasLocale) return NextResponse.next();
|
|
|
|
// Redirect if there is no locale
|
|
request.nextUrl.pathname = `/${defaultLocale}${pathname}`;
|
|
return NextResponse.redirect(request.nextUrl);
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/((?!_next|api|favicon.ico).*)'],
|
|
};
|