#Best practise for fetching data in nested dynamic routes

1 messages · Page 1 of 1 (latest)

ornate matrix
#

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?

upbeat furnaceBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

sour spade
#

abstract the code of fetching the data

ornate matrix
fossil flume
#

@ornate matrix it would help if you showed your existing code

ornate matrix
#

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

ornate matrix