-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
52 lines (42 loc) · 1.56 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { i18n, parseLocaleOnServer } from '@/lib/i18n';
export function middleware(request: NextRequest) {
const url = request.nextUrl.clone(); // https://nextjs.org/docs/messages/middleware-relative-urls
const headers = new Headers(request.headers);
// if (process.env.NODE_ENV === 'development') {
// headers.forEach((value, key) => {
// console.log(`[middleware] ${key}: ${value}`);
// });
// }
headers.set(`x-origin`, url.origin);
// Check if there is any supported locale in the pathname
const pathnameIsMissingLocale = i18n.locales.every(
(locale) =>
!url.pathname.startsWith(`/${locale}/`) && url.pathname !== `/${locale}`
);
// Redirect if there is no locale
if (pathnameIsMissingLocale) {
const locale = parseLocaleOnServer(request);
// e.g. If incoming request is '/weekly', the new URL is now '/ko/weekly'
url.pathname = `/${locale}${url.pathname.startsWith('/') ? '' : '/'}${
url.pathname
}`;
return NextResponse.redirect(url, { headers });
}
return NextResponse.next({ request: { headers } });
}
export const config = {
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image responses)
* - icon, apple-icon, favicon (icon files)
* - robots (generated robots)
* - sitemap (generated sitemaps)
*/
matcher: [
'/((?!api|_next/static|_next/image|sitemap|robots|icon|apple-icon|favicon).*)',
],
};