Ready for production deployment with Dockerfile and i18n support
This commit is contained in:
33
middleware.ts
Normal file
33
middleware.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
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).*)'],
|
||||
};
|
||||
Reference in New Issue
Block a user