Alternative to useSearchParams to prevent deopting from CSR
Answered
Griffon Nivernais posted this in #help-forum
Griffon NivernaisOP
Since adopting this its caused a meriad of stops that I find non-trivial to solve in a reasonable amount of time, the fact I always get locked out of CSR due to the way I use this function is absurd to me. Wondering what alternatives exist instead of this at the moment.
Answered by joulev
use dynamic rendering, just add
export const revalidate = 0. useSearchParams will then not deopt into (not from) CSR32 Replies
@Griffon Nivernais Since adopting this its caused a meriad of stops that I find non-trivial to solve in a reasonable amount of time, the fact I always get locked out of CSR due to the way I use this function is absurd to me. Wondering what alternatives exist instead of this at the moment.
have you wrapped the component that use
https://nextjs.org/docs/app/api-reference/functions/use-search-params#static-rendering
useSearchParams with Suspense?https://nextjs.org/docs/app/api-reference/functions/use-search-params#static-rendering
or passing the
searchParams props from the page component down if you want to avoid useSearchParams@Ray have you wrapped the component that use `useSearchParams` with `Suspense`?
https://nextjs.org/docs/app/api-reference/functions/use-search-params#static-rendering
Griffon NivernaisOP
I've a single page that relies entirely on search parameters so this isn't exactly feasible.
I did try to treat as much of the page as a component then wrap it in Suspense to no avail.
Ended up with the same kind of error, speaking of I've not posted it.
Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about
the nature of the error.
âš Entire page "/test/introduction" deopted into client-side rendering due to "undefined". Read more: https://nextjs.org/docs/messages/deopted-into-client-rendering The problematic page is
/test, but this odd behaviour has caused a cascading effect on inner pages like /test/introduction.@Ray or passing the `searchParams` props from the page component down if you want to avoid `useSearchParams`
Griffon NivernaisOP
I'll give this a shot.
I have to use Zod to properly type them as this page needs them to exist in the first place or it won't work.
Unsure if that can also cause issues.
@Griffon Nivernais Since adopting this its caused a meriad of stops that I find non-trivial to solve in a reasonable amount of time, the fact I always get locked out of CSR due to the way I use this function is absurd to me. Wondering what alternatives exist instead of this at the moment.
use dynamic rendering, just add
export const revalidate = 0. useSearchParams will then not deopt into (not from) CSRAnswer
if you want the page to be static, the search params cannot be known ahead of time
* if you are fine with it being known via client-side js (hence client-side rendering), then use
* if you are NOT fine with client-side rendering then it is fundamentally impossible since static pages don't know the request content in advance
* if you are fine with it being known via client-side js (hence client-side rendering), then use
Suspense like Ray said* if you are NOT fine with client-side rendering then it is fundamentally impossible since static pages don't know the request content in advance
Griffon NivernaisOP
Honestly, at this stage I don't care how it renders, I just want it to work.
Normally I opt for static rendering as that's performant and thanks to app router giving me the ability to be more choose-y of what renders where, I can also allow more private things to render as components within a server component.
But at this moment in time I genuinely just want to get this working lol
@Griffon Nivernais Honestly, at this stage I don't care how it renders, I just want it to work.
export const revalidate = 0 should do itGriffon NivernaisOP
I'll give it a try, had to wrestle GTM for a second.
@Griffon Nivernais
Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about
the nature of the error.
âš Entire page "/test/introduction" deopted into client-side rendering due to "undefined". Read more: https://nextjs.org/docs/messages/deopted-into-client-rendering
Griffon NivernaisOP
For context the number of these kinds of errors I got was 7, it's now dwindled down to 1 thanks to the exported variable (good news) but the page is still considered de-opted out of CSR (bad news because still receiving the error).
âš Entire page "/test" deopted into client-side rendering due to "useSearchParams()".Griffon NivernaisOP
Don't know, how can I confirm this?
It doesn't have anything like gSSP.
there is a report after you run
next buildGriffon NivernaisOP
Builds are successful now thanks to you two, let me take a look.
I believe it's static:
@Griffon Nivernais Builds are successful now thanks to you two, let me take a look.
does your page render correctly?
Griffon NivernaisOP
I believe it does yes, give me a moment to check.
I just figured out how to retrieve search parameters and opt out of SR.
Ding.
Looks all good to me.
nice
Griffon NivernaisOP
Thank you so much. 💞