generated from scaffold-eth/scaffold-eth
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add redirect for /[contractAddress] to mainnet (#69)
- Loading branch information
1 parent
55427e3
commit 4d72834
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NextResponse } from "next/server"; | ||
import type { NextRequest } from "next/server"; | ||
|
||
// Viem's address regex | ||
const addressRegex = /^0x[a-fA-F0-9]{40}$/; | ||
|
||
export function middleware(request: NextRequest) { | ||
const { pathname } = request.nextUrl; | ||
|
||
// Extract the first path segment after the initial slash, if any. | ||
const pathSegments = pathname.split("/").filter(Boolean); | ||
|
||
// Check if there is exactly one path segment and if it matches the address regex. | ||
if (pathSegments.length === 1 && addressRegex.test(pathSegments[0])) { | ||
const newURL = new URL(`/${pathSegments[0]}/1`, request.url); | ||
return NextResponse.redirect(newURL); | ||
} | ||
|
||
// For all other requests, proceed with normal handling. | ||
return NextResponse.next(); | ||
} |