Skip to content

Commit

Permalink
hack: redirect to correct page when 404
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilou97 committed Apr 4, 2024
1 parent 7a16642 commit 38e78ef
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion pages/404.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<section
className="flex flex-col items-center justify-center text-center text-white"
Expand Down

0 comments on commit 38e78ef

Please sign in to comment.