diff --git a/middleware.ts b/middleware.ts new file mode 100644 index 0000000..814b934 --- /dev/null +++ b/middleware.ts @@ -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) + ) + } +}