Skip to content

Commit

Permalink
add redirect for /[contractAddress] to mainnet (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Mar 1, 2024
1 parent 55427e3 commit 4d72834
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/nextjs/middleware.ts
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();
}

0 comments on commit 4d72834

Please sign in to comment.