Next.js Discord

Discord Forum

How can I make my routers to be faster?

Unanswered
Muhammed Khal posted this in #help-forum
Open in Discord
Hai guys, I am using the use.router but it's so much slow compared to the link, how can I make it faster or any better replacement for it?


    const handleRoute = (projectId: string) => {
      switch (true) {
        case isClientPortal:
          router.push(`/client-dashboard/projects/${projectId}`);
          break;
        case isClientWorkSpace:
          router.push(
            `/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`
          );
          break;
        case isProject:
          router.push(
            `/dashboard/projects/${projectId}?isProject=${isProject}`
          );
          break;
        default:
          router.push(
            `/dashboard/projects/${projectId}?isDashboard=${isDashboard}`
          );
          break;
      }
    };
              <Card className=' mr-3' onClick={() => handleRoute(project.id)}>
        {/* content */}

</Card>


HOw can I make it to reload faster in the production

80 Replies

why don't you want to use next/link here?
You mean like

<Link href={routerLink}>
<Card>
</Card>
</Link>
But in some cases I can't use like in the table
<TableBody className='cursor-pointer'>
                {table?.getRowModel().rows?.length > 0 &&
                  table.getRowModel().rows.map((row) => (
                    <TableRow
                      key={row.id}
                      data-state={row.getIsSelected() && 'selected'}
                      onClick={() =>
                        router.push(
                          `/dashboard/clients/client-workspace/${row.original.id}`
                        )
                      }
                    >
                      {row.getVisibleCells().map((cell) => (
                        <TableCell key={cell.id} className='table-cell'>
                          {flexRender(
                            cell.column.columnDef.cell,
                            cell.getContext()
                          )}
                        </TableCell>
                      ))}
                    </TableRow>
                  ))}
              </TableBody>


When I add the link to that table, it's breaking the entire layout
  useEffect(() => {
    if (isAgency === false) {
      router.push('/client-dashboard');
    }
  }, [isAgency, router, session]);


And at there I am not sure how can I use the Link!!
anyway the issue is that next/link prefetches the links/pages, and with router, you have to manualy call .prefetch or smth
Can you please gimme the example how can I do prefetch?
I did tried like
router.prefetch('url')

BUt seems it doesn't work
prefetch before the onclick?
No
like put it in a useeffect, so when the page loads, they all load (like next/link would do)
const handleRoute = (projectId: string) => {
      switch (true) {
        case isClientPortal:
          router.push(`/client-dashboard/projects/${projectId}`);
          break;
        case isClientWorkSpace:
          router.push(
            `/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`
          );
          break;
        case isProject:
          router.push(
            `/dashboard/projects/${projectId}?isProject=${isProject}`
          );
          break;
        default:
          router.push(
            `/dashboard/projects/${projectId}?isDashboard=${isDashboard}`
          );
          break;
      }
    };
~


That one with push

and


const handleRoute = (projectId: string) => {
      switch (true) {
        case isClientPortal:
          router.prefetch(`/client-dashboard/projects/${projectId}`);
          break;
        case isClientWorkSpace:
          router.prefetch(
            `/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`
          );
          break;
        case isProject:
          router.prefetch(
            `/dashboard/projects/${projectId}?isProject=${isProject}`
          );
          break;
        default:
          router.prefetch(
            `/dashboard/projects/${projectId}?isDashboard=${isDashboard}`
          );
          break;
      }
    };

With prefetch
.prefetch is called ealier... and then you .push
you mean like
router.prefetch.push(url)
no...
you run the prefetch sometime.. and then later, you run .push and it would be already in cache, so very quick
Yes I understand that point but I am not sure how to implement it
I did like the code I sent you but that doesn't even working'
like i said... make a useEffect that runs prefetch on all your urls...
useEffect(() => {
router.prefetch(url)
},[router])

const handleRoute = (projectId: string) => {
      switch (true) {
        case isClientPortal:
          router.push(`/client-dashboard/projects/${projectId}`);
          break;
        case isClientWorkSpace:
          router.push(
            `/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`
          );
          break;
        case isProject:
          router.push(
            `/dashboard/projects/${projectId}?isProject=${isProject}`
          );
          break;
        default:
          router.push(
            `/dashboard/projects/${projectId}?isDashboard=${isDashboard}`
          );
          break;
      }
    };
Like that
try it...
Yes
and does it work better?
I am trying hehe
gimme a min
yeah, you have to use built version of nextjs to see it work (as dev doesn't do prefetch)
Ahh erk
  const handleRoute = (projectId: string) => {
        switch (true) {
    case isClientPortal:
      router.prefetch(`/client-dashboard/projects/${projectId}`);
      router.push(`/client-dashboard/projects/${projectId}`);
      break;
    case isClientWorkSpace:
      router.prefetch(`/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`);
      router.push(`/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`);
      break;
    case isProject:
      router.prefetch(`/dashboard/projects/${projectId}?isProject=${isProject}`);
      router.push(`/dashboard/projects/${projectId}?isProject=${isProject}`);
      break;
    default:
      router.prefetch(`/dashboard/projects/${projectId}?isDashboard=${isDashboard}`);
      router.push(`/dashboard/projects/${projectId}?isDashboard=${isDashboard}`);
      break;
  }
};
`

I did kinda liek that
Is it works right?
this is useless now...
Ah okay
It needs to be in useEffect
it can be anywhere, but just not before the .push as that is useless
Okay
idk if you can do it on root of component, so i just said useEffect to be safe
@Clown bro are u seriously doing === false?
Because it's always getting as undfined bro at first as it's coming from the API
I tried !isAgency but doesn't seems working
useEffect(() => {
      switch (true) {
        case isClientPortal:
          router.prefetch(`/client-dashboard/projects/${projectId}`);
          break;
        case isClientWorkSpace:
          router.prefetch(
            `/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`
          );
          break;
        case isProject:
          router.prefetch(
            `/dashboard/projects/${projectId}?isProject=${isProject}`
          );
          break;
        default:
          router.prefetch(
            `/dashboard/projects/${projectId}?isDashboard=${isDashboard}`
          );
          break;
      }
      console.log('projectId in the use effect:', projectId);
    }, [
      isClientPortal,
      isClientWorkSpace,
      isDashboard,
      isProject,
      projectId,
      router,
    ]);

    const handleRoute = (projectId: string) => {
      switch (true) {
        case isClientPortal:
          router.push(`/client-dashboard/projects/${projectId}`);
          break;
        case isClientWorkSpace:
          router.push(
            `/dashboard/projects/${projectId}?isClientWorkSpace=${isClientWorkSpace}`
          );
          break;
        case isProject:
          router.push(
            `/dashboard/projects/${projectId}?isProject=${isProject}`
          );
          break;
        default:
          router.push(
            `/dashboard/projects/${projectId}?isDashboard=${isDashboard}`
          );
          break;
      }
    };

<Card
                className=' mr-3'
                onClick={() => {
                  handleRoute(project.id);
                  setProjectId(project.id);
                }}
              >



I did like that but seems not working
@riský
you have to get the list of all projectId combinations on the page... and then prefetch them...
or... you could just make a size 0 next/link element (idk if this would fully work tho)
The projectid's gonna be so many right sometimes more then 100 and thousand
If I do then it's gonna slow down the application
then don't have prefetch...
you could have prefetch on hover...
but idk
Hmm this seems might useful
@riský or... you could just make a size 0 next/link element (idk if this would fully work tho)
hmmm if this works... then you would have the benifit of next/link prefetching (only when visible) and still manualy work
onMouseOver then prefetch
you can do that (but if they do click very fast... then not real use)
Link is really faster compared to the router
There's some places I can't able to use
the Link
that is because it fetches whenever the link is in viewport... so not all at once
yeah
Did you used the prefetch in your application?
so, you either have to implment it youself of use next/link and remove the default styling that breaks things
and I have one question
router prefetch does show what I am going to fetch like
router prefetch does what .push does but not actually change page (ie just download the page)
Like that, at left bottom of the screen?
I mean if i use a link and i am able to see what my link is going it, if I implement the prefect can I able to see like that like in the above ss?
if you use the useRouter, you don't have that ui as it is not a link element...
anyway... i would suggest reading the docs, as i am getting nowhere here...
Okay gotit
Thanks
@riský could you please share me the exact doc link so I can go through that
Saint Bernard
I find this to be very problematic and concerning as well. forces me to style links as buttons sometimes although i just want to stick to semantic structuring
How did you handled this situation @Saint Bernard ?
Did you used the prefetch of the router?
Saint Bernard
I just use Links
@Saint Bernard I just use Links
thats what i feel like is the best.. just overide the styles so it doesn't break everything... and then it should work well
Okay
@riský are you available here bro?
router.events.on


Seems that event is not available on next/navigation right
I am trying to implement the loader bar attached to the header but seems it''s not working
Do you know how can I do it?