Does a single loading.tsx on the root directory of `app` catch all the way down to all routes?
Answered
Elite posted this in #help-forum
EliteOP
Does a single loading.tsx on the root directory of
app catch all the way down to all routes?Answered by B33fb0n3
It should, yes. Because the nearest suspense boundary will be triggered
22 Replies
@Elite Does a single loading.tsx on the root directory of `app` catch all the way down to all routes?
It should, yes. Because the nearest suspense boundary will be triggered
Answer
IIRC yes but i think there was a edge case that if your layout is loaded but the actual page content has dynamic stuff that needs further loading then the same suspense boundary won't be triggered again. In that case you'll need another loading.tsx for that page.
@Clown IIRC yes but i think there was a edge case that if your layout is loaded but the actual page content has dynamic stuff that needs further loading then the same suspense boundary won't be triggered again. In that case you'll need another loading.tsx for that page.
EliteOP
so how do you recommend i go about making my loading pages? should I do it wherever I fetch content from an API or something on the server side? just add a loading.tsx there?
is it like better practice to do so?
and then for my client side fetches i have built in loading skeletons with useEffects on them
is it like better practice to do so?
and then for my client side fetches i have built in loading skeletons with useEffects on them
@Elite so how do you recommend i go about making my loading pages? should I do it wherever I fetch content from an API or something on the server side? just add a loading.tsx there?
is it like better practice to do so?
and then for my client side fetches i have built in loading skeletons with useEffects on them
for serverside fetching use loading.tsx and for clientside fetching use libraries like react query or swr. They have build in mechanisms to keep track of the loading state
EliteOP
ah so i shouldnt do it myself
@B33fb0n3 for serverside fetching use loading.tsx and for clientside fetching use libraries like react query or swr. They have build in mechanisms to keep track of the loading state
EliteOP
in one spot im polling data as I query data on redirect, but there is no data until a few seconds after, so I poll it.
do u recommend react query for that?
do u recommend react query for that?
and it would handle the polling and loading state right
and for server components i can just do a normal fetch + loading.tsx
@Elite in one spot im polling data as I query data on redirect, but there is no data until a few seconds after, so I poll it.
do u recommend react query for that?
If you getting redirected, the browser itself shows a loading state. If you don’t want this, mutate (I guess) first (show a loading animation) and then redirect
@B33fb0n3 If you getting redirected, the browser itself shows a loading state. If you don’t want this, mutate (I guess) first (show a loading animation) and then redirect
EliteOP
no like when they login, they tget redirected to a page
and on that page i fetch user data
but i need to poll the data as it doesnt appear fast
EliteOP
So I wanna expose some api endpoints publicly in the future with auth so my mobile app can use. Would I just make all my backend logic as functions in the util folder, and then just instantiate them and return their outputs in route handlers? That way my website technically doesn’t call any APIs but the code is the same if I make a mobile app.
Nah, you way should be to exclude your functionality in different functions and call these functions inside your server components. Then you can also use the loading.tsx to show a loading page
@B33fb0n3 Nah, you way should be to exclude your functionality in different functions and call these functions inside your server components. Then you can also use the loading.tsx to show a loading page
EliteOP
Yes but what if I want to make this same logic available for like my mobile app? Wouldn’t I need a route handler? But I can’t call them in server functions
@Elite Yes but what if I want to make this same logic available for like my mobile app? Wouldn’t I need a route handler? But I can’t call them in server functions
If you make an mobile app, there are more things to consider to apply loading states, when you write them in a different language
EliteOP
Well I’m not saying the loading states
@B33fb0n3 If you make an mobile app, there are more things to consider to apply loading states, when you write them in a different language
EliteOP
What I mean is like the core logic that are in each of these functions
So if I have the source code of the backed in these functions, then I’d just make a route handler that instantiates this function and returns its output as JSON or whatever. But in my next project I don’t use the route handlers if I’m on the server side
Just the raw functions right
ah yea. You can make your backend accessable via route handlers. That's fine
EliteOP
Alr