-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
38 lines (30 loc) · 1.04 KB
/
Copy pathproxy.ts
File metadata and controls
38 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
const LOCALES = ['fr', 'en']
const DEFAULT_LOCALE = 'fr'
/** Routes blocked from direct access (without locale prefix) */
const BLOCKED_ROUTES: string[] = []
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl
// Block direct access to protected routes (e.g. /uses → 404)
const isBlocked = BLOCKED_ROUTES.some(
route => pathname === route || pathname.startsWith(`${route}/`)
)
if (isBlocked) {
return NextResponse.rewrite(new URL('/404', request.url))
}
// Redirect to default locale if no locale prefix
const hasLocale = LOCALES.some(
l => pathname.startsWith(`/${l}/`) || pathname === `/${l}`
)
if (!hasLocale) {
const localizedPath = pathname === "/"
? `/${DEFAULT_LOCALE}`
: `/${DEFAULT_LOCALE}${pathname}`
return NextResponse.redirect(new URL(localizedPath, request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/((?!api|_next|_vercel|.*\\..*).*)']
}