Skip to content

Commit 4bab112

Browse files
committed
export middleware custom route matcher
1 parent 8b3b0e3 commit 4bab112

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/middleware.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { NextRequest, NextResponse } from "next/server"
22

33
import { DEFAULT_LOCALE, FAKE_LOCALE, LOCALES_CODES } from "./lib/constants"
44

5-
const PUBLIC_FILE = /\.(.*)$/
6-
75
function detectLocale(acceptLanguage: string | null) {
86
if (!acceptLanguage) {
97
return DEFAULT_LOCALE
@@ -21,17 +19,23 @@ function detectLocale(acceptLanguage: string | null) {
2119
return locale
2220
}
2321

22+
export const config = {
23+
matcher: [
24+
/*
25+
* Match all request paths except for the ones starting with:
26+
* - api (API routes)
27+
* - _next/static (static files)
28+
* - _next/image (image optimization files)
29+
* - favicon.ico (favicon file)
30+
* - .well-known (security files)
31+
*/
32+
"/((?!api|_next/static|_next/image|favicon.ico|.well-known).*)",
33+
],
34+
}
35+
2436
// Middleware required to always display the locale prefix in the URL. It
2537
// redirects to the default locale if the locale is not present in the URL
2638
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
33-
}
34-
3539
if (req.nextUrl.locale === FAKE_LOCALE) {
3640
// Apparently, the built-in `localeDetection`from Next does not work when
3741
// using the faked locale hack. So, we need to detect the locale manually

0 commit comments

Comments
 (0)