From f7b1e9bacd978f108f4c27ad2668b61609587bf2 Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Wed, 28 Feb 2024 20:35:50 +0530 Subject: [PATCH 1/2] add middlware to redirect /[contractAddress] requests --- 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..829ab44e --- /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); // Removes empty strings from the result. + + // 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(); +} From 838621318a2a6ac1cd84bd7bec4ba25658f034eb Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Wed, 28 Feb 2024 20:39:49 +0530 Subject: [PATCH 2/2] remove extra Boolean comment --- packages/nextjs/middleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nextjs/middleware.ts b/packages/nextjs/middleware.ts index 829ab44e..7197fd21 100644 --- a/packages/nextjs/middleware.ts +++ b/packages/nextjs/middleware.ts @@ -8,7 +8,7 @@ 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); // Removes empty strings from the result. + 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])) {