Questions on prefetching in NextJS
I have this productPage in my products/[id]/page.tsx file
and this util function
I have a Next Link which redirects to this product detail page.
I'm doing my tests on the production build. When the link appears in my viewport, I can see in the network tab of my browser that it has prefetched the product page.
However, when I click on it, there is still a delay of 2 seconds before the page gets displayed. Why is that ?
I fail to understand what prefetching actually does & what are the real uses-cases of prefetching which can be clearly visible ?
6 Replies
You have setTimeout set to 2 seconds, so that sounds like the answer. When you go to the page, it is calling get product, which is doing a 2 second delay prior to returning some data.
i think what op is asking is how can prefetching fit in with this component?
i’m not a nextjs person so idk
njs only prefetches the JavaScript/React components needed to render that page when you prefetch a route
it doesnt run your data fetching logic (
getProduct() in your case)
The 2s delay happens because getProduct() runs when you actually navigate to the page, not during prefetch
njs downloads the JavaScript bundle for /products/[id] to your browser on prefetch
Your Server Component executes only on navigation to that path
getProduct() gets called, waits 2 seconds, rendersFunctions: generateStaticParams | Next.js
API reference for the generateStaticParams function.
so a better test would be to conditionally render the component after the page loads?
Yes, that's basically what makes prefetching good imo