I am building a Next.js app where some of the pages have in-page-routes one them. To accommodate this, I am using a catch-all (i.e. [[...slug]]) and handling the routes on that page itself. My folder structure looks like this:
While these routes work as expected, I am running into an issue where if I include an "as" prop to the Next.js Link component when referencing one of these catch-all pages, the href breaks, and the page refreshes to the "as" url. Here is my code:
import { useRouter } from "next/router";
import Link from "next/link";
export default function Header() {
const router = useRouter();
return (
<header>
<nav>
<ul>
<NavItem link="/">Logo</NavItem>
<NavItem link="/games/crash">Link Text</NavItem>
<NavItem link="/games/double">Link Text</NavItem>
<NavItem link="/games/mines">Link Text</NavItem>
<NavItem link="/profile">
<Profile />
</NavItem>
<NavItem link={router.asPath} as="/deposit">
<RoundPurpleButton />
</NavItem>
<MoreButton />
</ul>
</nav>
</header>
);
}
function NavItem({ children, link, as }) {
return (
<li>
<Link href={link} as={as}>
<a> {children}</a>
</Link>
</li>
);
}
All header elements link to their own page except the Purple Round button. For reasons (modal functionality) it links to the current page but displays a different url. I am using dynamic link building, passing an href and an as prop to the Next.js Link component. This is where I am getting strange behavior.
If I pass just a link everything works fine. You can see from the below picture each header and the link that it routes to (above the header element). In this scenario each header element, when clicked, takes you to the specified link.
The moment I add an "as" prop however, the round purple button displays the correct "link" and "as" attributes but when you click on it you see this message in the console:
GET http://localhost:3001/_next/static/chunks/pages/games/double.js net::ERR_ABORTED 404 (Not Found)
then the page refreshes to the "as" url.
I believe this issue has something to do with the Next.js catch-all links because I do not get this issue on statically typed link like "/", it's only on the catch-all links like "profile/[[...profile]]."
Does anyone have any ideas on how to fix this issue or how to properly structure the routes to allow for this functionality?