-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
37 lines (29 loc) · 1.06 KB
/
Copy pathmiddleware.js
File metadata and controls
37 lines (29 loc) · 1.06 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
import { NextResponse } from "next/server";
import { match } from '@formatjs/intl-localematcher';
import Negotiator from 'negotiator';
const locales = ['en-US', 'de-ES'];
function getLocale(request) {
const headers = { 'accept-language': request.headers.get('accept-language') };
const languages = new Negotiator({ headers }).languages();
const defaultLocale = 'en-US';
return match(languages, locales, defaultLocale);
}
export function middleware(request) {
const { pathname } = request.nextUrl;
// Exclude static files (e.g., Whitepaper.pdf) and Next.js internals (_next)
if (pathname.startsWith('/_next') || pathname === '/Whitepaper.pdf') {
return;
}
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
);
if (pathnameHasLocale) return;
const locale = getLocale(request);
request.nextUrl.pathname = `/${locale}${pathname}`;
return NextResponse.redirect(request.nextUrl);
}
export const config = {
matcher: [
'/((?!_next).*)', // Apply to all routes except _next
],
};