Next.js Discord

Discord Forum

Huge routes.ts file - is there a standard way of simplifying it?

Unanswered
Braconid wasp posted this in #help-forum
Open in Discord
Braconid waspOP
I've got a huge routes array in the routes.ts files and I've been thinking about simiplying it like this:

// routes/Home.ts
const HomeRoute = {
  name: "Home",
  path: "/",
};

// routes/Admin.ts
const AdminRoutes = {
  name: "Admin",
  path: "/admin",
  children: [
    {
      name: "super-admin",
    }
  ],
};

// This pattern continues for other sections...

// Central configuration file (routes/index.ts)
import { HomeRoute } from './Home';
import { AdminRoutes } from './Admin';

const Routes = [
  HomeRoute,
  AdminRoutes,
  // Further route modules would be added here
];

export { Routes };


but I'm just not sure if that's standard and something that other developers would be used to? There's some benefits to having a huge array as well, such as you don't have to jump all over different files to see what's happening and you can easily see how everything relates but at the same time, if you want to work only on one route, it might make it difficult to navigate. But, given that you don't often makes changes to the Routes array, it might be beneficial to keep it all in one place. Did I just answer my own question?

0 Replies