Files
klz-cables.com/middleware.ts
2025-12-28 23:28:31 +01:00

42 lines
1.1 KiB
TypeScript

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// List of supported locales
const locales = ['en', 'de']
const defaultLocale = 'en'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Skip middleware for static files and API routes
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api/') ||
pathname.startsWith('/media/') ||
pathname.includes('.') // Skip files with extensions
) {
return NextResponse.next()
}
// Check if pathname already has a locale
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
)
if (pathnameHasLocale) {
// Already has locale, continue
return NextResponse.next()
}
// Add default locale to URL
request.nextUrl.pathname = `/${defaultLocale}${pathname}`
return NextResponse.redirect(request.nextUrl)
}
export const config = {
matcher: [
// Match all paths except static files and API routes
'/((?!_next|api|media|favicon.ico|robots.txt|sitemap.xml).*)',
],
}