Skip to content

Commit fdd1d4f

Browse files
authored
Merge pull request #12954 from ethereum/performance/ignore-routes-middleware
Performance: fix compression issues by adjusting middleware matcher
2 parents 0869936 + f8fc217 commit fdd1d4f

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

netlify.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
[functions]
4242

4343
[functions.___netlify-odb-handler]
44+
external_node_modules = ["sharp"]
4445
included_files = [
4546
"./src/intl/**/*",
4647
"!./public/**/*",
4748
"node_modules/next/dist/server/future/route-modules/pages/vendored/contexts/router-context*",
4849
"node_modules/next/dist/server/future/route-modules/pages/vendored/contexts/amp-context*",
4950
"node_modules/next/dist/server/future/route-modules/pages/vendored/contexts/head-manager-context*",
51+
"node_modules/sharp/**/*",
5052
]
5153

5254
[functions.___netlify-handler]

src/middleware.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,36 @@ function detectLocale(acceptLanguage: string | null) {
2121
return locale
2222
}
2323

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+
2438
// Middleware required to always display the locale prefix in the URL. It
2539
// redirects to the default locale if the locale is not present in the URL
2640
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()
3345
}
3446

35-
if (req.nextUrl.locale === FAKE_LOCALE) {
47+
if (locale === FAKE_LOCALE) {
3648
// Apparently, the built-in `localeDetection`from Next does not work when
3749
// using the faked locale hack. So, we need to detect the locale manually
3850
const localeDetected = detectLocale(req.headers.get("accept-language"))
3951
const locale = localeDetected || DEFAULT_LOCALE
4052

41-
const redirectUrl = new URL(`/${locale}${req.nextUrl.pathname}`, req.url)
53+
const redirectUrl = new URL(`/${locale}${pathname}`, req.url)
4254

4355
// Add trailing slash if it's not present
4456
if (!redirectUrl.pathname.endsWith("/")) {
@@ -47,4 +59,6 @@ export async function middleware(req: NextRequest) {
4759

4860
return NextResponse.redirect(redirectUrl, { status: 301 })
4961
}
62+
63+
return NextResponse.next()
5064
}

0 commit comments

Comments
 (0)