#How do i show loading screen while server component is fetching data?

59 messages · Page 1 of 1 (latest)

solar canyon
#

I would like to show loading screen, or some sort of loading for each item when the server component is fetching data.

timber pythonBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

solar canyon
#

Its because when i redirect to another link/route, the layout refetches the data and the redirecting takes a while

#

i would like to redirect instantly, but load after redirecting

barren umbra
#

Create a file with the name loading.js or loading.tsx and place your loader there. It will display until your server component fetches data.

solar canyon
#

like these

barren umbra
#

yeah its skeleton, there are many libraries out there to make such ui, plus you could create the same using css too.

#

Tailwind code

<div role="status" class="max-w-sm animate-pulse">
<div class="h-2.5 bg-gray-200 rounded-full dark:bg-gray-700 w-48 mb-4"></div>
<div class="h-2 bg-gray-200 rounded-full dark:bg-gray-700 max-w-[360px] mb-2.5"></div>
<div class="h-2 bg-gray-200 rounded-full dark:bg-gray-700 mb-2.5"></div>
<div class="h-2 bg-gray-200 rounded-full dark:bg-gray-700 max-w-[330px] mb-2.5"></div>
<div class="h-2 bg-gray-200 rounded-full dark:bg-gray-700 max-w-[300px] mb-2.5"></div>
<div class="h-2 bg-gray-200 rounded-full dark:bg-gray-700 max-w-[360px]"></div>
<span class="sr-only">Loading...</span>
</div>

solar canyon
#

i want to know where to use them

#

so that the server component shows it

#

even while its fetching

#

because making a loading.js would require me to make the entire layout again

#

and then add those loading components

barren umbra
#

just a sibling of server component would do

#

Can you share your folder structure?

solar canyon
#

this is the entire src folder structure

#

layout.js is the parent server component

#

which loads data on each load

#

also its not cached

barren umbra
#

Where is your server component which is fetching data?

solar canyon
#

is the server component

#

and it fetches data and passes the data to the client components

barren umbra
#

place the loading.jsx/loading.tsx at the same level.

solar canyon
#

but then how would i make loading components

barren umbra
#

Sorry, didn't get you

solar canyon
#

while fetching data

barren umbra
#

place this code in the loading.tsx file, it'll show untill the fetching ends

solar canyon
#

but that would make the layout of the website change no?

barren umbra
#

Why would it change? Loading.tsx will display until your data fetches, once it fetches the data your actual content with show not the loading.

solar canyon
#

i want loading to have same layout

#

just the component loads

barren umbra
#

ok then wrap your loading components inside suspense and you can show a placeholder

solar canyon
#

whats a suspense

barren umbra
solar canyon
#

ok

#

how do i wrap my layout inside suspense

#

cause layout.js is fetching data

#

@barren umbra where do you fetch data

#

inside layout.js or another server component

terse dove
#

you can wrap your component with its data in a suspense boundary

barren umbra
uneven totem
#

precisely, that should be enough

solar canyon
#

i wrapped client component that uses data passed from server component in suspense

#

but it doesnt load

#

that seperately

#

but for now i just wrapped whole layout children in suspense

#

and its fine ig

barren umbra
#

pass a fallback to suspense to show while loading

uneven totem
#

not to be that guy

#

but its also in the docs

#

😅

#

like fallback is super standard, unless sinista referred to someth else