import createMiddleware from "next-intl/middleware"; import { NextRequest, NextResponse } from "next/server"; import { locales } from "@/config"; export default async function middleware(request: NextRequest) { const { pathname } = request.nextUrl; const defaultLocale = "in"; // Check if the pathname already has a locale const pathnameHasLocale = locales.some( (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}` ); // If pathname doesn't have a locale, redirect to default locale if (!pathnameHasLocale) { // Skip redirect for API routes, static files, and Next.js internals if ( pathname.startsWith('/api/') || pathname.startsWith('/_next/') || pathname.startsWith('/favicon') || pathname.includes('.') || pathname.startsWith('/public/') ) { return NextResponse.next(); } // Redirect to default locale const newUrl = new URL(`/${defaultLocale}${pathname}`, request.url); return NextResponse.redirect(newUrl); } // Use next-intl middleware for internationalized routes const handleI18nRouting = createMiddleware({ locales: locales, defaultLocale: defaultLocale, }); return handleI18nRouting(request); } export const config = { // Match all pathnames except for // - API routes // - _next (Next.js internals) // - _static (inside /public) // - all root files inside /public (e.g. favicon.ico) matcher: ['/((?!api|_next|_static|.*\\..*).*)'] };