-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make router.query available from any route, regardless of data fetching requirements. #20026
Comments
Hi @acalvino4. There are a number of issues like this (that I can't track down right now), but I did find this answer to be descriptive of what the reasoning is: #16019 (comment) |
Hi @jamesmosier, thank you for pointing me to that discussion. I'm still a little confused, as this paragraph from that discussion:
implies to me that while at initial load the query params are of course unknown, that useRouter could still have client side code that is executed to read in the query params and update the page from there - is that correct? |
But only after hydration https://reactjs.org/docs/react-dom.html#hydrate as otherwise the html content does not match up with the client-side hydration React tree and React will throw away all the pre-rendered content causing performance issues. If you don't have When a hydration mismatch happens it causes UI jumps which is worse for user experience. I'll close this issue as there's multiple others already like #8259 |
@acalvino4 I just published a package with a workaround It's a tiny package that wrap the next.js router to provide the router.query and router.pathname on client-side first render |
@morhogg as said in #20026 (comment) providing it is not the way to go and will cause performance issues and jumps in your application. |
@timneutkens @jamesmosier There are a few points in my original post that were not really answered yet.
|
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Feature request
To access query parameters (i.e.
?author=Nick&category=lifestyle
) in a page route in next.js, the appropriate method seems to be getting a router reference via the useRouter hook. However, the documentation notes thatrouter.query
"will be an empty object during prerendering if the page doesn't have data fetching requirements". Would it be possible to do away with this requirement? Is there any reason that a route without getStaticProps or getServerSideProps can't have access to these params, given that they are typically accessible fromwindow.location
? I think implementing this would improve the user experience.Of note, the router object returned from useRouter already has access to these params in it's
asPath
attribute. I may be missing something, but it would seem trivial to me to have a method that loads these into therouter.query
object from theuseRouter()
call.Describe alternatives
The alternative seems to be implementing getStaticProps or getServerSideProps, though besides this being more work than is ideal from a developer experience perspective, there aren't really instructions on how exactly this is accomplished. Does just having
getStaticProps
implemented then load params intorouter.query
? Or do I need to passcontext.params
fromgetServerSideProps
in as a prop? So far I haven't figured out how this alternative works either.The text was updated successfully, but these errors were encountered: