-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathmiddleware.ts
More file actions
46 lines (38 loc) · 1.59 KB
/
middleware.ts
File metadata and controls
46 lines (38 loc) · 1.59 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
46
import { createI18nMiddleware } from 'fumadocs-core/i18n';
import type { NextFetchEvent, NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { i18n } from '@/lib/i18n';
const fumadocsMiddleware = createI18nMiddleware(i18n);
export default function middleware(request: NextRequest, event: NextFetchEvent) {
const { pathname } = request.nextUrl;
const segments = pathname.split('/').filter(Boolean);
const currentLocale = i18n.languages.includes(segments[0]) ? segments[0] : null;
// Get the referer to detect if user is coming from a localized page
const referer = request.headers.get('referer');
if (referer && !currentLocale) {
try {
const refererUrl = new URL(referer);
const refererSegments = refererUrl.pathname.split('/').filter(Boolean);
const refererLocale = i18n.languages.includes(refererSegments[0]) ? refererSegments[0] : null;
// If coming from a non-default locale page to a non-prefixed URL
if (refererLocale && refererLocale !== i18n.defaultLanguage) {
if (pathname.match(/^\/(tools|apis|resources|reference)\//)) {
return NextResponse.redirect(new URL(`/${refererLocale}${pathname}`, request.url));
}
}
} catch {
// Ignore invalid referer URLs
}
}
const result = fumadocsMiddleware(request, event);
return result;
}
export const config = {
matcher: [
// Match .md files
'/(.*).md',
'/docs/(.*).md',
// Match all paths except Next.js internals, API routes, and static files
'/((?!_next|api/).*)', // This excludes /api/ but includes /apis/
],
};