From 4d7283427b2e6cda64901625f0be416537173523 Mon Sep 17 00:00:00 2001 From: "Shiv Bhonde | shivbhonde.eth" Date: Fri, 1 Mar 2024 20:15:04 +0530 Subject: [PATCH] add redirect for /[contractAddress] to mainnet (#69) --- packages/nextjs/middleware.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/nextjs/middleware.ts diff --git a/packages/nextjs/middleware.ts b/packages/nextjs/middleware.ts new file mode 100644 index 00000000..7197fd21 --- /dev/null +++ b/packages/nextjs/middleware.ts @@ -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(); +}