Next.js Discord

Discord Forum

How to conditionally handle Suspense

Answered
Northern Wheatear posted this in #help-forum
Open in Discord
Avatar
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?

28 Replies

Avatar
Northern WheatearOP
For some context
Image
The suspense works while the page is initially loading. After that I need to remove the fallback from showing for each route change
Avatar
Alfonsus Ardani
you can cache the result of dataWrapper so that it always used the cached version on each route change
Avatar
Northern WheatearOP
Actually its kind of cached because I'm storing that data in a context which is shared with the entire app
Avatar
Siberian Flycatcher
Hey 👋

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
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
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
Siberian Flycatcher
Do you have the code on GitHub?
Avatar
Northern WheatearOP
Nope its a company project so private 😥
Avatar
Siberian Flycatcher
Do you mind sharing your file structure in a sidebar?
Avatar
Northern WheatearOP
Sure
Image
Avatar
Northern WheatearOP
Let me know if you need more context into this
Avatar
Siberian Flycatcher
Answer
Avatar
Siberian Flycatcher
Notice that Suspense fallback shows up on page load but not on soft navigation.
Image
Avatar
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
Ray
Alex got a lot of cool stuff on twitter, you should check it out 😆
Avatar
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!!
Avatar
Ray
or you found it😂
Avatar
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 😅