forked from shopstr-eng/shopstr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (36 loc) · 1.21 KB
/
Copy pathmiddleware.ts
File metadata and controls
41 lines (36 loc) · 1.21 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";
import { nip19 } from "nostr-tools";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Handle npub redirects, but ignore if already in marketplace page route
if (
pathname.match(/^\/npub[a-zA-Z0-9]+$/) &&
!pathname.startsWith("/marketplace/")
) {
const url = new URL(`/marketplace${pathname}`, request.url);
return NextResponse.redirect(url);
}
// Handle naddr redirects, but ignore if already in listing page route
if (
pathname.match(/^\/naddr[a-zA-Z0-9]+$/) &&
!pathname.startsWith("/listing/")
) {
const url = new URL(`/listing${pathname}`, request.url);
return NextResponse.redirect(url);
}
// Handle community naddr redirects
if (pathname.startsWith("/naddr") && !pathname.startsWith("/communities/")) {
try {
const decoded = nip19.decode(pathname.substring(1));
if (decoded.type === "naddr" && decoded.data.kind === 34550) {
return NextResponse.redirect(
new URL(`/communities${pathname}`, request.url)
);
}
} catch (e) {
/* ignore */
}
}
return NextResponse.next();
}