Nextjs: Expected server HTML to contain a matching <div> in <div>
See original GitHub issueI have tried all the available options and all of them brings me back this same error.
Example:
<MediaQuery minDeviceWidth={1024}> <div>Desktop Only</div> </MediaQuery>
Issue Analytics
- State:
- Created 3 years ago
- Reactions:21
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Warning: Expected server HTML to contain a matching <div ...
Hi, I have a navbar , and it displays a different UI based on if a user is logged in or not. I...
Read more >in NEXTJS Warning: Expected server HTML to contain a ...
Case 1. Most likely a Server<>Client out of date issue. Fix. If you are using the development server > Restart it.
Read more >[Solved]-Next.JS: Warning Expected server HTML to contain a ...
JS: Warning Expected server HTML to contain a matching <a> in <div>; How to rehydrate?- ... However with Nextjs either with Server-Side Rendering...
Read more >Meme Overflow on Twitter: "Next.js Warning: Expected server ...
Next.js Warning: Expected server HTML to contain a matching <div> in <div> https://stackoverflow.com/questions/67655933/806889…
Read more >expected server html to contain a matching <div> in <div nextjs
React expects that the rendered content is identical between the server and the client. It can patch up differences in text content, but...
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
Dynamic import fixes this issue -
Useful link - Dynamic import with no ssr
@ivanhueso it’s because server renders different content than what client does. You need to “somehow” tell react-responsive during SSR what is the size screen of the client app.
Here is an example: https://codesandbox.io/s/gifted-euler-3ykdc.
First - in
_document.js
- you check from request headers what is the client device withua-parser-js
. Then you wrap the whole app withResponsiveContext.Provider
, so that everything renders accordingly.There is one catch though. That is with static pre-rendering - if NEXT see a potential for optimization it will render webpage upfront as a static html. This means that, now there is no way to tell whether this page will be displayed on the mobile or desktop (because there is no server that could decipher this from a request and then render page). This is why I added getServerSideProps method in my example, to prevent that optimization. But I don’t think this is something to be fixed with react-responsive itself…