Skip to content

Commit dc657fb

Browse files
committed
update netlify.toml and middleware.ts
1 parent 4bab112 commit dc657fb

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { NextRequest, NextResponse } from "next/server"
22

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

5+
const PUBLIC_FILE = /\.(.*)$/
6+
57
function detectLocale(acceptLanguage: string | null) {
68
if (!acceptLanguage) {
79
return DEFAULT_LOCALE
@@ -29,20 +31,26 @@ export const config = {
2931
* - favicon.ico (favicon file)
3032
* - .well-known (security files)
3133
*/
32-
"/((?!api|_next/static|_next/image|favicon.ico|.well-known).*)",
34+
"/((?!api|_next/static).*)",
3335
],
3436
}
3537

3638
// Middleware required to always display the locale prefix in the URL. It
3739
// redirects to the default locale if the locale is not present in the URL
3840
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) {
4048
// Apparently, the built-in `localeDetection`from Next does not work when
4149
// using the faked locale hack. So, we need to detect the locale manually
4250
const localeDetected = detectLocale(req.headers.get("accept-language"))
4351
const locale = localeDetected || DEFAULT_LOCALE
4452

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

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

5260
return NextResponse.redirect(redirectUrl, { status: 301 })
5361
}
62+
63+
return NextResponse.next()
5464
}

0 commit comments

Comments
 (0)