Server client html mismatch occurs on first render
See original GitHub issueIssue Description
Warnings like Warning: Expected server HTML to contain a matching <button> in <div>.
occurs because html was rendered with no url params on server side, but rendered with url params in client side.
I think Next.js handles this problem by setting router.query to an empty object on the first render, and sets client url params on the next render. But because useUrlState uses window.location.search and not router, this warning occurs.
This can be avoided by using code like this:
const [queryState, setQueryState] = useQueryState(...);
const [state, setState] = useState();
useEffect(() => setState(queryState), [queryState]);
But it would be better if this boilerplate could be avoided in some way.
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top Results From Across the Web
server side rendering and client mismatch · Issue #100 - GitHub
The bug you're seeing can be viewed in more detail here and is caused by a html mismatch from the server to the...
Read more >Handling the React server hydration mismatch error
The problem is that the server-side render doesn't contain the final className that is generated on the client.
Read more >Easily Fix React Hydration Errors - Travis Wimer
The reason behind these errors is a mismatch between the HTML provided by ... Returns null on first render, so the client and...
Read more >Error: Text content does not match server-rendered HTML
The mismatch happens because the random numbers generated when the page gets pre-rendered on the server do not match the random numbers ......
Read more >HTML Mismatch Server Side Rendering with React and Redux
HTML Mismatch Server Side Rendering with React and ReduxSubscribe ▻https://www.youtube.com/c/BeALearnerofficialSubscribe 2nd Channel ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think the correct solution would be to always return the default value on both server and client-side on the initial render. Reading from
window.location
should be done in an effect. See this article for an in-depth explanation of why this is important https://joshwcomeau.com/react/the-perils-of-rehydration/Unfortunately the Next.js router does not expose querystring params in the render tree in SSR, so those should be treated as client-side only (just like you would for code that depends on local storage values for example).
While theoretically it could be possible to get this kind of information on “per request” SSR (ie: page with
getServerSideProps
), other kinds of non-client renders (ISR, static optimisation at build time) are not attached to a request and therefore don’t have querystring parameters.