-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware.ts
31 lines (27 loc) · 1.04 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Middleware allows you to run code before a request is completed. Then, based on the
* incoming request, you can modify the response by rewriting, redirecting, modifying
* the request or response headers, or responding directly.
* @see https://nextjs.org/docs/app/building-your-application/routing/middleware
*/
import createIntlMiddleware from "next-intl/middleware";
import { NextRequest } from "next/server";
import { defaultLocale, locales } from "./i18n/config";
// Don't run middleware on API routes or Next.js build output
export const config = {
matcher: ["/((?!api|_next|.*\\..*).*)"],
};
/**
* Detect the user's preferred language and redirect to a localized route
* if the preferred language isn't the current locale.
*/
const i18nMiddleware = createIntlMiddleware({
locales,
defaultLocale,
// Don't prefix the URL with the locale when the locale is the default locale (i.e. "en-US")
localePrefix: "as-needed",
});
export default function middleware(req: NextRequest) {
const response = i18nMiddleware(req);
return response;
}