@@ -21,24 +21,36 @@ function detectLocale(acceptLanguage: string | null) {
21
21
return locale
22
22
}
23
23
24
+ export const config = {
25
+ matcher : [
26
+ /*
27
+ * Match all request paths except for the ones starting with:
28
+ * - api (API routes)
29
+ * - _next/static (static files)
30
+ * - _next/image (image optimization files)
31
+ * - favicon.ico (favicon file)
32
+ * - .well-known (security files)
33
+ */
34
+ "/((?!api|_next/static).*)" ,
35
+ ] ,
36
+ }
37
+
24
38
// Middleware required to always display the locale prefix in the URL. It
25
39
// redirects to the default locale if the locale is not present in the URL
26
40
export async function middleware ( req : NextRequest ) {
27
- if (
28
- req . nextUrl . pathname . startsWith ( "/_next" ) ||
29
- req . nextUrl . pathname . includes ( "/api/" ) ||
30
- PUBLIC_FILE . test ( req . nextUrl . pathname )
31
- ) {
32
- return
41
+ const { pathname, locale } = req . nextUrl
42
+
43
+ if ( pathname . startsWith ( "/_next" ) || PUBLIC_FILE . test ( pathname ) ) {
44
+ return NextResponse . next ( )
33
45
}
34
46
35
- if ( req . nextUrl . locale === FAKE_LOCALE ) {
47
+ if ( locale === FAKE_LOCALE ) {
36
48
// Apparently, the built-in `localeDetection`from Next does not work when
37
49
// using the faked locale hack. So, we need to detect the locale manually
38
50
const localeDetected = detectLocale ( req . headers . get ( "accept-language" ) )
39
51
const locale = localeDetected || DEFAULT_LOCALE
40
52
41
- const redirectUrl = new URL ( `/${ locale } ${ req . nextUrl . pathname } ` , req . url )
53
+ const redirectUrl = new URL ( `/${ locale } ${ pathname } ` , req . url )
42
54
43
55
// Add trailing slash if it's not present
44
56
if ( ! redirectUrl . pathname . endsWith ( "/" ) ) {
@@ -47,4 +59,6 @@ export async function middleware(req: NextRequest) {
47
59
48
60
return NextResponse . redirect ( redirectUrl , { status : 301 } )
49
61
}
62
+
63
+ return NextResponse . next ( )
50
64
}
0 commit comments