forked from SkateHive/skatehive3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (33 loc) · 1.16 KB
/
Copy pathmiddleware.ts
File metadata and controls
41 lines (33 loc) · 1.16 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
/**
* SkateHive Middleware
*
* Purpose:
* 1. Handle snap URL rewrites
*
* What we DON'T do:
* - Bot detection via user-agent (ineffective, blocks legitimate crawlers)
* - Use Vercel Firewall for IP-based blocking if needed
*/
export function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
// Handle snap URLs: /user/username/snap/permlink
const snapMatch = url.pathname.match(/^\/user\/([^\/]+)\/snap\/([^\/]+)$/);
if (snapMatch) {
const [, username, permlink] = snapMatch;
// Rewrite to the profile page with snaps view
// The client-side will detect the URL structure and open the modal
url.pathname = `/user/${username}`;
url.searchParams.set('view', 'snaps');
return NextResponse.rewrite(url);
}
return NextResponse.next();
}
export const config = {
matcher: [
// Keep middleware scoped to the only app route that needs a rewrite.
// The www -> apex redirect is configured at the Vercel domain level.
'/user/:username/snap/:permlink',
],
};