I create a routes file with all my hard-coded routes and use the const
throughout my app. I use trailing slash, so they need to be appended to all routes.
This setup works well for me, and I've used it on numerous projects with hundreds of routes. It saved a lot of time over the years when inevitably, I needed to change a route's href
or name.
My routes file is similar to this:
export const ROUTE_HOME = {
href: "/",
name: "Home",
};
export const ROUTE_ABOUT = {
href: "/about/",
name: "About",
};
I use them like
import Link from 'next/link';
import {ROUTE_HOME, ROUTE_ABOUT} from 'your/path/to/routes';
<Link href={ROUTE_HOME}>{ROUTE_HOME.name}</Link>
<Link href={ROUTE_ABOUT}>{ROUTE_ABOUT.name}</Link>
On larger projects, I break the routes out into separate files.
src > const > routes.ts // top-level routes
src > modules > auth > const > routes.ts // only auth routes