forked from thirdweb-dev/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
119 lines (101 loc) · 3.3 KB
/
middleware.ts
File metadata and controls
119 lines (101 loc) · 3.3 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// middleware.ts
import { NextRequest, NextResponse } from "next/server";
import { getAllChainRecords } from "utils/allChainsRecords";
// ignore assets, api - only intercept page routes
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api|_next/static|_next/image|favicon.ico).*)",
],
};
// used for resolving chainId to network slug with constant time lookup
const { chainIdToChain, slugToChain } = getAllChainRecords();
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// remove '/' in front and then split by '/'
const paths = pathname.slice(1).split("/");
// we're in chain mode, rewrite to `/chain/<slug>`
if (paths.length === 1) {
// redirect numbers to strings
if (paths[0] in chainIdToChain) {
const chainId = Number(paths[0]);
return redirect(request, `/${chainIdToChain[chainId].slug}`);
}
if (paths[0] in slugToChain) {
return rewrite(request, `/chain/${slugToChain[paths[0]].slug}`);
}
}
// end chain mode
// ignore paths that don't have at least 2 parts
if (paths.length < 2) {
return;
}
const [networkOrAddress, ...catchAll] = paths;
// legacy
const legacyRedirect = handleLegacyRedirects(
request,
networkOrAddress,
catchAll,
);
if (legacyRedirect) {
return legacyRedirect;
}
// solana contract page
// if (isSupportedSOLNetwork(networkOrAddress)) {
// const solNetwork = getSolNetworkFromNetworkPath(networkOrAddress);
// if (!solNetwork) {
// return redirect(request, "/404");
// } else {
// return rewrite(request, `/sol${pathname}`);
// }
// }
// evm contract page
// /<network>/... or /<chainId>/...
if (isPossibleEVMAddress(catchAll[0])) {
// /<chainId>/... => /evm/<network>/...
if (networkOrAddress in chainIdToChain) {
const networkInfo = chainIdToChain[Number(networkOrAddress)];
// can not use rewrite here because slug is required client side for resolving the network
return redirect(request, `/${networkInfo.slug}/${catchAll.join("/")}`);
}
// /<network>/... => /evm/<network>/...
return rewrite(request, `/evm${pathname}`);
}
if (isPossibleEVMAddress(networkOrAddress)) {
return rewrite(request, `/publish${pathname}`);
}
}
function isPossibleEVMAddress(address: string) {
return address?.startsWith("0x") || address?.endsWith(".eth");
}
// utils for rewriting and redirecting with relative paths
function rewrite(request: NextRequest, relativePath: string) {
const url = request.nextUrl.clone();
url.pathname = relativePath;
return NextResponse.rewrite(url);
}
function redirect(
request: NextRequest,
relativePath: string,
permanent = false,
) {
const url = request.nextUrl.clone();
url.pathname = relativePath;
return NextResponse.redirect(url, permanent ? 308 : undefined);
}
function handleLegacyRedirects(
request: NextRequest,
networkOrAddress: string,
catchAll: string[],
) {
// handle deployer.thirdweb.eth urls
if (networkOrAddress === "deployer.thirdweb.eth") {
return redirect(request, `/thirdweb.eth/${catchAll.join("/")}`, true);
}
}