-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
37 lines (31 loc) · 1.11 KB
/
Copy pathproxy.ts
File metadata and controls
37 lines (31 loc) · 1.11 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 { NextRequest, NextResponse } from 'next/server';
import { isMarkdownPreferred, rewritePath } from 'fumadocs-core/negotiation';
import { docsContentRoute, docsRoute } from '@/lib/shared';
import {
getSlugFromContentPath,
hasProtectedAccess,
isProtectedSlug,
} from '@/lib/protected';
const { rewrite: rewriteDocs } = rewritePath(
`${docsRoute}{/*path}`,
`${docsContentRoute}{/*path}/content.md`,
);
const { rewrite: rewriteSuffix } = rewritePath(
`${docsRoute}{/*path}.md`,
`${docsContentRoute}{/*path}/content.md`,
);
export default async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
let rewriteTarget = rewriteSuffix(pathname);
if (!rewriteTarget && isMarkdownPreferred(request)) {
rewriteTarget = rewriteDocs(pathname);
}
if (rewriteTarget) {
const slug = getSlugFromContentPath(rewriteTarget);
if (slug && isProtectedSlug(slug) && !(await hasProtectedAccess())) {
return new NextResponse('Unauthorized', { status: 401 });
}
return NextResponse.rewrite(new URL(rewriteTarget, request.nextUrl));
}
return NextResponse.next();
}