feat: add i18n middleware for cloudflare compatibility

This commit is contained in:
dyzulk
2026-01-09 08:48:05 +07:00
parent 9ce5d01315
commit 44d578f033

21
middleware.ts Normal file
View File

@@ -0,0 +1,21 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
const PUBLIC_FILE = /\.(.*)$/
export function middleware(req: NextRequest) {
if (
req.nextUrl.pathname.startsWith('/_next') ||
req.nextUrl.pathname.includes('/api/') ||
PUBLIC_FILE.test(req.nextUrl.pathname)
) {
return
}
if (req.nextUrl.locale === 'default') {
const locale = req.cookies.get('NEXT_LOCALE')?.value || 'en'
return NextResponse.redirect(
new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url)
)
}
}