-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
45 lines (39 loc) · 1.21 KB
/
middleware.ts
File metadata and controls
45 lines (39 loc) · 1.21 KB
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
39
40
41
42
43
44
45
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import {
defaultLocale,
getLocaleFromPathname,
normalizeLocaleFromAcceptLanguage,
} from '@/i18n/config';
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api') ||
pathname.startsWith('/admin') ||
pathname.startsWith('/logos') ||
pathname.startsWith('/projects') ||
pathname === '/favicon.ico' ||
pathname === '/robots.txt' ||
pathname === '/sitemap.xml'
) {
return NextResponse.next();
}
if (pathname === '/') {
const preferred = normalizeLocaleFromAcceptLanguage(request.headers.get('accept-language'));
const url = request.nextUrl.clone();
url.pathname = `/${preferred}`;
return NextResponse.redirect(url);
}
const localeFromPath = getLocaleFromPathname(pathname) ?? defaultLocale;
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-locale', localeFromPath);
return NextResponse.next({
request: {
headers: requestHeaders,
},
});
}
export const config = {
matcher: ['/((?!_next/static|_next/image|.*\\..*).*)'],
};