@@ -2,6 +2,8 @@ import { NextRequest, NextResponse } from "next/server"
2
2
3
3
import { DEFAULT_LOCALE , FAKE_LOCALE , LOCALES_CODES } from "./lib/constants"
4
4
5
+ const PUBLIC_FILE = / \. ( .* ) $ /
6
+
5
7
function detectLocale ( acceptLanguage : string | null ) {
6
8
if ( ! acceptLanguage ) {
7
9
return DEFAULT_LOCALE
@@ -29,20 +31,26 @@ export const config = {
29
31
* - favicon.ico (favicon file)
30
32
* - .well-known (security files)
31
33
*/
32
- "/((?!api|_next/static|_next/image|favicon.ico|.well-known ).*)" ,
34
+ "/((?!api|_next/static).*)" ,
33
35
] ,
34
36
}
35
37
36
38
// Middleware required to always display the locale prefix in the URL. It
37
39
// redirects to the default locale if the locale is not present in the URL
38
40
export async function middleware ( req : NextRequest ) {
39
- if ( req . nextUrl . locale === FAKE_LOCALE ) {
41
+ const { pathname, locale } = req . nextUrl
42
+
43
+ if ( PUBLIC_FILE . test ( pathname ) ) {
44
+ return NextResponse . next ( )
45
+ }
46
+
47
+ if ( locale === FAKE_LOCALE ) {
40
48
// Apparently, the built-in `localeDetection`from Next does not work when
41
49
// using the faked locale hack. So, we need to detect the locale manually
42
50
const localeDetected = detectLocale ( req . headers . get ( "accept-language" ) )
43
51
const locale = localeDetected || DEFAULT_LOCALE
44
52
45
- const redirectUrl = new URL ( `/${ locale } ${ req . nextUrl . pathname } ` , req . url )
53
+ const redirectUrl = new URL ( `/${ locale } ${ pathname } ` , req . url )
46
54
47
55
// Add trailing slash if it's not present
48
56
if ( ! redirectUrl . pathname . endsWith ( "/" ) ) {
@@ -51,4 +59,6 @@ export async function middleware(req: NextRequest) {
51
59
52
60
return NextResponse . redirect ( redirectUrl , { status : 301 } )
53
61
}
62
+
63
+ return NextResponse . next ( )
54
64
}
0 commit comments