How to conditionally handle Suspense
Answered
Northern Wheatear posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
I need to conditionally handle suspense. Right now I have used a suspense in my layout.tsx file to wait until my server component loads. Once the component is loaded I dont want that suspense to appear while routing to another pages. How can I implement this?
Answered by Siberian Flycatcher
Checkout this CodeSandbox https://codesandbox.io/p/devbox/pedantic-golick-qg3sj7?file=%2Fapp%2Flayout.jsx
28 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
For some context
![Image](https://cdn.discordapp.com/attachments/1180874398727688242/1180875083389087834/image.png?ex=657f029f&is=656c8d9f&hm=75d2c5cb36757df21edfb3e0cb561eb051b2ea63ba51bd57ca6482b2526a442b&)
The suspense works while the page is initially loading. After that I need to remove the fallback from showing for each route change
![Avatar](https://cdn.discordapp.com/avatars/194128415954173952/708a8637ada505b6b7a85972807d9dca.webp?size=256)
Alfonsus Ardani
you can cache the result of dataWrapper so that it always used the cached version on each route change
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
Actually its kind of cached because I'm storing that data in a context which is shared with the entire app
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siberian Flycatcher
Hey 👋
Your setup should already work the way you describe.
Your setup should already work the way you describe.
RootLayout
does not re-render during soft navigation (route change). So it should show the Suspense fallback during the initial page load, but not when the user navigates to another route using Link
or router.push
.![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
Oo okey but this loading component is coming up while im routing to a new page... Its happening only in dev I guess
Let me build and check
Let me build and check
No its showing up on production also
As you have mentioned @Siberian Flycatcher
RootLayout
is not rerendering but the children during each page change right?... I think that is causing the suspense to show the fallback while route changes![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siberian Flycatcher
Do you have the code on GitHub?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
Nope its a company project so private 😥
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siberian Flycatcher
Do you mind sharing your file structure in a sidebar?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
Sure
![Image](https://cdn.discordapp.com/attachments/1180874398727688242/1180919583364874320/image.png?ex=657f2c11&is=656cb711&hm=504462f879d7a30aa1451fea5c55c2862ceeaffb1005b6214aad0a8dd5dd2515&)
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
Let me know if you need more context into this
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siberian Flycatcher
Checkout this CodeSandbox https://codesandbox.io/p/devbox/pedantic-golick-qg3sj7?file=%2Fapp%2Flayout.jsx
Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
Siberian Flycatcher
Notice that Suspense fallback shows up on page load but not on soft navigation.
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
Yes I saw.... In my case the header and footer rendered outside this data wrapper... Let me try to reorganise the code and I will let you know
Also thanks a lot for taking so much effort and time to come up with this sandbox â¤ï¸
![Avatar](https://cdn.discordapp.com/avatars/743561772069421169/3c5d689eee4459a24fc420063411c016.webp?size=256)
Ray
Alex got a lot of cool stuff on twitter, you should check it out 😆
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
Sure can you share the twitter Id
Yours also😄
Wow... I went through some of the posts
Amazing stuff
Code animations are sick!!
or you found it😂
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Northern WheatearOP
This solution worked... In my case I had some components above the DataWrapper which was remouting on route change... That was causing the issue... Thanks a lott!!
Yes sir 😅