From 5f036c5da08ce722193d4deaddbe270e056d6e80 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Date: Thu, 4 Apr 2024 14:36:57 +0200 Subject: [PATCH] hack: redirect to correct page when 404 --- pages/404.tsx | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/pages/404.tsx b/pages/404.tsx index 6d1b89dd..cf3d2d65 100644 --- a/pages/404.tsx +++ b/pages/404.tsx @@ -1,6 +1,38 @@ import Link from "next/link"; +import { useRouter } from "next/router"; + +/** + * Inject the different path possible under [walletAddress] + */ +export async function getStaticProps() { + const fs = require("fs"); + const walletPaths = fs + .readdirSync("./pages/[walletAddress]/") + .map((page: string) => page.replace(".tsx", "")); + + return { + props: { + walletPaths, + }, + }; +} + +export default function Custom404(props: any) { + const router = useRouter(); + const path = router.asPath; + let [_, address, page] = path.split("/"); + page = page || "index"; // page value can be undefined + // If the page match /KT1..../{dashboard|index|anything under [walletAddress]} we redirect + // This hack only works if the http server redirect to 404.html when the page is not found + // Fortunately it's the case of github-page + if ( + address && + address.startsWith("KT1") && + props.walletPaths.includes(page) + ) { + router.push(path); + } -export default function Custom404() { return (