-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
38 lines (30 loc) · 911 Bytes
/
middleware.js
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
32
33
34
35
36
37
38
const locales = ['br', 'en'];
// Get the preferred locale, similar to the above or using a library
function getLocale(request) {
if (request && request.language) {
return request.language;
}
return 'br';
}
export function middleware(request) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl;
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
if (pathnameHasLocale) return;
// Redirect if there is no locale
const locale = getLocale(request);
request.nextUrl.pathname = `/${locale}${pathname}`;
// eslint-disable-next-line consistent-return
return Response.redirect(request.nextUrl);
}
export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next).*)',
'/'
// Optional: only run on root (/) URL
// '/'
],
};