Query params loading suspense
Answered
Mainak posted this in #help-forum
MainakOP
Updating the query params doesnt trigger the loading.tsx file. Any workaround for that?
Answered by joulev
use
key to manually trigger a Suspense boundary. https://github.com/joulev/debug/tree/nextjs-search-rsc-searchparams-with-loading-Caio-Ricciuti120 Replies
loading.tsx only applies when the dom is changing
MainakOP
But as it is a suspense boundary it should trigger when we fetch something related to the query parameter. Some part of the dom is changing when the query is rerun?
For example currency parameter, the prices will change based on it!
For example currency parameter, the prices will change based on it!
when the component is rerendering cause of a state change then it will trigger the suspense
for example: revalidatePath passes a prop to the clientside and a useEffect checks for the propchange and sets a state that is displayed on the return
react will automatically reload that component
loading.tsx triggers
MainakOP
But the query param is itself a state if I’m not wrong
I’m using the state from the url to refetch using trpc
So I believe it counts as state change as well?
I’m using the state from the url to refetch using trpc
So I believe it counts as state change as well?
if u use it within a hook
then yes
are u using useSearchParams
did u check if the values change
example: rooms
or end_date
just console.log the values without useEffect
MainakOP
For now I'm checking the currency only
and yes, its changing
as it is suspended query, I believe it it should trigger the loading
and yes, its changing
as it is suspended query, I believe it it should trigger the loading
yeah it should
if the values change
is your loading.tsx working correctl tho?
somewhere in your page
@gin is your loading.tsx working correctl tho?
MainakOP
its working when I reload the page, or load the page the first time
so can confirm it works
so can confirm it works
MainakOP
Wrapping suspense manually, loses the feature of having loading.tsx in the first place, right?
@Mainak Wrapping suspense manually, loses the feature of having loading.tsx in the first place, right?
nope it just overwrites it
for that component
MainakOP
also found this, but its quite confusing https://github.com/vercel/next.js/issues/53543
mind hopping in a screenshare and i try to fix it
maybe its better when i directly face the problem
helped a bunch of people like this with weird bugs
MainakOP
wait, give me sometime
MainakOP
1660457197
@gin joining?
MainakOP
wait
remove everything except mouse and keyboard
perms
MainakOP
check
wait check the /rooms page
wait let me do it
dont use your keyboard
show me where it dont work
wait
i see so the state is changing but no loading ui
correct
MainakOP
can u zoom in or something
MainakOP
wehy is trpc not working
let me check
one monebnt
okay CMS is down :(((((((((((
LMAOOO
i was saying
can u turn it back on?
nope, cannot
no control on me
we was about to fix it
unlucky
yeah sry we cant do aynthing rn while its down
i disconnect for now tell me when its back up
sure
i'll ping you up
what were you trying at last though, I'm curious 😄
@Mainak what were you trying at last though, I'm curious 😄
i was trying to trigger a complete rerender of the route
u can do that using a pseudo server action doing nothing
its a trick in my book
its a passive rerender without reloading the page completely
just mutates the data
and that triggers suspense aswell
MainakOP
Okay, so the revalidate path value will be the url of the current path?
Hmm that makes sense
Call a server action just to trigger a suspense is neat
Call a server action just to trigger a suspense is neat
@Mainak Okay, so the revalidate path value will be the url of the current path?
revalidatePath clears the router cache of the given path
@Mainak Hmm that makes sense
Call a server action just to trigger a suspense is neat
for example lets say a ssr component fetched data and passes it to the client
MainakOP
So won’t it be revalidatePath(“/rooms”)
triggering revalidatePath mutates the data
MainakOP
Got it
Okay understood now, bad luck server went out 🙁
Okay understood now, bad luck server went out 🙁
So can we call the formAction without a form?
MainakOP
@gin server up, let me know when you're available!
@gin issue fixed?
MainakOP
Hey man, instead of using router push I checked that if I used the browser API of history pushState then it’s working as expected
So I guess it’s the one way, coz revalidatePath also doesn’t trigger the loading!
So I guess it’s the one way, coz revalidatePath also doesn’t trigger the loading!
@Mainak Updating the query params doesnt trigger the loading.tsx file. Any workaround for that?
use
key to manually trigger a Suspense boundary. https://github.com/joulev/debug/tree/nextjs-search-rsc-searchparams-with-loading-Caio-RicciutiAnswer
MainakOP
@joulev so will pass the loading component from the loading file itself?
@Mainak <@484037068239142956> so will pass the loading component from the loading file itself?
The loading file is just an implicit Suspense boundary, so since here we are using an explicit Suspense boundary we no longer need that loading.tsx file. The loading component can be imported from anywhere.
@joulev The loading file is just an implicit Suspense boundary, so since here we are using an explicit Suspense boundary we no longer need that loading.tsx file. The loading component can be imported from anywhere.
MainakOP
Hmm, that makes sense
I believe then it will also trigger with cookie change as well! 🤔
I believe then it will also trigger with cookie change as well! 🤔
Suspense only works when it is first mounted. if the page is simply rerendered such as when you change the cookies in a server action, you probably need a
key to force trigger Suspense as well.MainakOP
@joulev I dont think the key works tbh
what I did, is
but still the data is updated with no suspense being triggered
what I did, is
JSON.stingyfy the cookie then add it as key to the suspensebut still the data is updated with no suspense being triggered
@Mainak <@484037068239142956> I dont think the key works tbh
what I did, is `JSON.stingyfy` the cookie then add it as key to the suspense
but still the data is updated with no suspense being triggered
i'll need to see some code. how do you add the cookies?
btw if you are using a server action to set cookies, you can do
and use the
btw if you are using a server action to set cookies, you can do
const [isPending, startTransition] = useTransition()
startTransition(() => serverAction())and use the
isPending state to render a loading stateso you just set it from the client side?
MainakOP
yes, from a drawer only
then the cookie change should be instantaneous and doesn't reload any page, so a loading screen doesn't make sense...
that's effectively just
document.cookie = ..., react doesn't even know you have a new cookieif you want to reload the page you probably have something like
router.refresh() in there?Even I tried to put currency to the query params, still same
no loading
no loading
@Mainak Even I tried to put currency to the query params, still same
no loading
with router.refresh then you can just
const [isPending, startTransition] = useTransition()
startTransition(() => router.refresh())@joulev with router.refresh then you can just
tsx
const [isPending, startTransition] = useTransition()
startTransition(() => router.refresh())
MainakOP
Nope, no matter what
i cannot make the loading screen appear 😦
i cannot make the loading screen appear 😦
MainakOP
Hmm, I think I understand it now
I think this lib is to blame for https://www.npmjs.com/package/cookies-next
I think this lib is to blame for https://www.npmjs.com/package/cookies-next
sometimes I need cookie in client component as well, thats why used the lib^
@Mainak Hmm, I think I understand it now
I think this lib is to blame for https://www.npmjs.com/package/cookies-next
no it's not to blame, it is the same as this line
just with extra features. i didn't use it because not necessary to install a dependency for the example.
the important part here is that you read the cookies and use it as the key for Suspense. when the key changes, Suspense will be re-triggered.
document.cookie = `foo=${new Date().toISOString()}`;just with extra features. i didn't use it because not necessary to install a dependency for the example.
the important part here is that you read the cookies and use it as the key for Suspense. when the key changes, Suspense will be re-triggered.
@joulev no it's not to blame, it is the same as this line
tsx
document.cookie = `foo=${new Date().toISOString()}`;
just with extra features. i didn't use it because not necessary to install a dependency for the example.
the important part here is that you read the cookies and use it as the key for Suspense. when the key changes, Suspense will be re-triggered.
MainakOP
i'm not setting the cookie with that, I'm actually using
getCookieYes can confirm, that is the issue^
now I did it with next cookies, and works as expected 😮
now I did it with next cookies, and works as expected 😮