Link navigation is taking forever In App router
Unanswered
Sun bear posted this in #help-forum
Sun bearOP
I using app router in next 14 and the link navigation is taking like 5 sec to navigate and I even add loading page but it is not showing the loading....
45 Replies
Could you provide us with more infos/minimal reprodtuction repository?
https://vercel.com/guides/creating-a-minimal-reproducible-example
https://vercel.com/guides/creating-a-minimal-reproducible-example
Sun bearOP
I could share on dynamic route because it is big project, I'm migrating the page to app router which I successful did but link navigation is taking too long
I even add prefetch in link
yeah
but it would be helpful to see that route code as well
it looks like there is an expensive calc/query running before a page render
Sun bearOP
// imports
export default async function SubscribersHome() {
const session = await getServerSession(authOptions);
const user = await prisma.team.findFirst({
where: {
userRoles: {
every: {
userId: session?.user?.id ?? "",
},
},
},
});
return (
<Layout
pageName="Home"
subtitle="Your page for critical information and summaries"
>
<Suspense fallback={<span>Loading...........</span>}>
<div className="flex h-full w-full justify-center md:items-center">
<Link
href={`http://${user?.slug}.localhost:3001`}
target="_blank"
rel="noreferrer"
className="truncate rounded-md bg-slate-100 p-2 text-sm font-medium text-stone-900 transition-colors hover:bg-slate-200 dark:bg-stone-800 dark:text-stone-400 dark:hover:bg-stone-700"
>
{`http://${user?.slug}.localhost:3001`} ↗
</Link>
</div>
</div>
</div>
<Link href={DOCS_LINK} target="_blank" rel="noopener noreferrer">
<Button>
<ArrowRight className="mr-2 w-4" /> Go to Docs
</Button>
</Link>
</div>
</div>
<PlanUpgradeBanner />
</Suspense>
</Layout>
);
}is this route segment dynamic? and if yes - does it need to be like that?
Sun bearOP
I removed lots of code from this
I see already
it's propbably prisma query 😉
try to prerender that segment with SSG or ISR
which auth lib are you using? if next-auth you can join your roles already in token 😄
Sun bearOP
How to prerender ?? like I'm also using tRPC
@Z4NR34L which auth lib are you using? if next-auth you can join your roles already in token 😄
Sun bearOP
yeah I'm using nextauth
first thigs first - please make your own DB Adapter for netx-auth where you will join all data that you are using, like permissions, roles etc into token and session
that will prevent from making queries all time for checking perks or role
Sun bearOP
can you suggestion solution because I don't get what you mean
Here you will find materials about making db adapter for next-auth
https://authjs.dev/guides/adapters/creating-a-database-adapter
https://authjs.dev/guides/adapters/creating-a-database-adapter
also there is already a list of ready-to-use ones, but in your case you will need to add roles to session and token
https://authjs.dev/getting-started/adapters
https://authjs.dev/getting-started/adapters
Sun bearOP
yeah my database query run too much even if I added index in prisma table
you are running this query on every render, that's possible reason why routing is taking that long
Sun bearOP
Okay one big question, why the loading.tsx is not running on navigation ????
I can't answer you this question as I don't have an insight into your project's code fully, you will need to share at least project's structure with me
Sun bearOP
BTW I have prisma adapter I don't think the above links are not valid in my case
about which loading.tsx we are talking about?
Sun bearOP
all of loading in dynamic is not working
@Sun bear BTW I have prisma adapter I don't think the above links are not valid in my case
yeah, but I bet you not added joins to roles in it to store this data in user token/session 🙂
there is no loading.tsx in
[team] segmentSun bearOP
I put loading on layout via suspense and its working
it's in [team]/account and [team]/analytics
Sun bearOP
like in account, content and other pages
add loader in root of [team] segment, and try again
or even add global one on project's root
@Z4NR34L there is no loading.tsx in `[team]` segment
Sun bearOP
I don't get it
I think adding the loading.tsx in root or [team] won't work, because the link navigation is taking too long
BTW do the page.tsx need to be server component to loading.tsx to work ?
Let me search that for you 🙂
@Z4NR34L there is no loading.tsx in `[team]` segment
Sun bearOP
maybe link for this ?
here you go
if you will follow nextjs docs, everything should be just working, if not please make an minimal reproduction repository as it's not possible to debug your case in such way as we were trying.
https://vercel.com/guides/creating-a-minimal-reproducible-example
https://vercel.com/guides/creating-a-minimal-reproducible-example
Sun bearOP
do you link for "yeah, but I bet you not added joins to roles in it to store this data in user token/session "
I've already linked you next-auth docs, where you can find all needed informations to solve this case on your own