Next.js Discord

Discord Forum

Routing UX best practices with Next 13+

Answered
Blanc de Hotot posted this in #help-forum
Open in Discord
Blanc de HototOP
:popCat: Hi folks, I'm having trouble getting React Suspense working correctly where if the user begins a route change to a server layout with a lot of fetch requests, I'd like the link to display a loading icon. Can someone point me to an example of this? I can get it to work perfectly using form actions, but unsure how to achieve this with routing, whether it be through Link components or through router.push. Thank you!
Answered by Common Moorhen
use server components
View full answer

59 Replies

Common Moorhen
what do you mean with "if the user begins a route change to a server layout with a lot of fetch requests"?
you mean that the new layout needs to fetch data?
@Blanc de Hotot
@!=tgt why are you fetching in a server component
Blanc de HototOP
this is standard practice afaik??
@Common Moorhen you mean that the new layout needs to fetch data?
Blanc de HototOP
yes, layout/page
@Blanc de Hotot this is standard practice afaik??
how are you fetching
you can directly acccess stuff from a server component
no api fetchr equests
@Blanc de Hotot yes, layout/page
Common Moorhen
use server components
Answer
Common Moorhen
to create loading skeletons for a page, use loading files
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming

to create loading skeletons for a layout, use <Suspense />
Blanc de HototOP
i'm not sure what either of you are saying. layouts are server components by default. 👀
here's the structure i'm using currently.

layout.tsx (server component)
- fetches data and determines which view to render for the user
- imports client components to render fetched data, wrapped in suspense, with a loading skeleton fallback.

navigation.tsx (client component)
- assume i have folders a/layout.tsx, b/layout.tsx, c/[slug]/layout.tsx
- i would have links to a, b, and one of these pages may have a list of c/[slug] links. clicking these would lead to respective layouts.
do you mean i need to have another server component separate from layout.tsx as a connecting component between layout and client??
Common Moorhen
if you fetch data on the server you dont need to use loading things
cos its going to be fast
suspense is for client components that fetch data
wat
@Blanc de Hotot use a loading.tsx
its just suspense wrapping your page
so while the page does its fetching, it shows the loading.tsx file
Common Moorhen
why would you use a skeleton for waiting for a data that takes almost nothing to load?
it doesnt apply to streaming
you can just make a spinner
Common Moorhen
yes
or show a text sying loading
Yea
anything works
Common Moorhen
I havent worked with data streaming
so idk how it would work in that case
its not complicated as it sounds
Common Moorhen
but for showing loading ui while awaiting data, it would make if it were something on the client that takes long because its the client
but the server has a fast connection, so it is fast
and if you are fetching on the client, im guessing you would be doing so in a useEffect
so you can easily setup a state var to manage loading
Common Moorhen
yes
so why would you use the suspense on the server?
@Common Moorhen so why would you use the suspense on the server?
since on the server...you dont have a way to do loading?
Common Moorhen
yes
i mean
a server is not always fast btw
Common Moorhen
not?
like if its something very intensive, can take around 3-4s
Common Moorhen
oh
didnt know that
what could be very intensive? multime requests?
dashboards
alot of stuff can take time
Common Moorhen
ah
youre right
i was thinking of a single component
and some thimes youre going to have multiple async components
mhm
@Arinji so while the page does its fetching, it shows the loading.tsx file
Blanc de HototOP
i'm not using a loading.tsx, just using a custom fallback that's a basic component. i did end up figuring this out, i needed an additional layer of the server component that i use on layout.tsx. thank you!