Next.js Discord

Discord Forum

I don't understand what Preload does

Answered
harshcut posted this in #help-forum
Open in Discord
https://nextjs.org/docs/app/building-your-application/data-fetching/patterns#preloading-data
I don't quite understand what the preload function does in this case? Why is it to be used? I there some other resource that I can look into?
Answered by joulev
if you don't preload:
1. checkIsAvailable is run first
2. if isAvailable is truthy, <Item /> is run
3. getItem is then run to render <Item />
=> checkIsAvailable and getItem are run sequentially

if you do preload:
1. getItem is initiated
2. while getItem is still running, checkIsAvailable is run
3. if isAvailable is truthy, <Item /> is run
4. the output of getItem that we already computed in step 1 is used to render <Item />
=> getItem do not have to wait for checkIsAvailable to finish before starting => faster
5. if isAvailable is falsy, the computed value of getItem is simply dropped and unused
View full answer

3 Replies

@harshcut https://nextjs.org/docs/app/building-your-application/data-fetching/patterns#preloading-data I don't quite understand what the preload function does in this case? Why is it to be used? I there some other resource that I can look into?
if you don't preload:
1. checkIsAvailable is run first
2. if isAvailable is truthy, <Item /> is run
3. getItem is then run to render <Item />
=> checkIsAvailable and getItem are run sequentially

if you do preload:
1. getItem is initiated
2. while getItem is still running, checkIsAvailable is run
3. if isAvailable is truthy, <Item /> is run
4. the output of getItem that we already computed in step 1 is used to render <Item />
=> getItem do not have to wait for checkIsAvailable to finish before starting => faster
5. if isAvailable is falsy, the computed value of getItem is simply dropped and unused
Answer
basically, the preload patterns speed up calculation by initiating expensive function calls before it's determined if they will be necessary to render the page
great explanation! thank for this