Next.js Discord

Discord Forum

Best practise for fetching data in nested dynamic routes

Unanswered
Polar bear posted this in #help-forum
Open in Discord
Polar bearOP
Hi, I have a dynamic route for example: /servers/[serverid]
and there are many nested routes inside it like /servers/[serverid]/channels /servers/[serverid]/members /servers/[serverid]/roles etc..

Now I have to duplicate code of fetching data in every single route

so how can I reduce this code and do the best practise?

7 Replies

abstract the code of fetching the data
@Yi Lon Ma abstract the code of fetching the data
Polar bearOP
didn't get it
@Polar bear it would help if you showed your existing code
Polar bearOP
For example I have to do this for every single route inside dynamic route

  const branchPromise = getBranch(bid);
  const employeesPromise = getEmployees(query, validPage, validPerPage);

  const [branch, employees] = await Promise.all([
    branchPromise,
    employeesPromise,
  ]);


I have to check for branch if it exists or not then get data related to this route and if this route has dynamic route inside it like
/branches/[bid]/employees/[empid]

it gets worse
All of routes inside /branches/[bid] depends on branch data
@Polar bear <@452033040428826644> Here's an example
I mean thats already quite abstracted with your getBranch function,