question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nextjs: Expected server HTML to contain a matching <div> in <div>

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Reactions:21
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
bhavesh-chaudharicommented, Jan 30, 2022

Dynamic import fixes this issue -

import dynamic from "next/dynamic"

const MediaQuery = dynamic(()=>{
  return import("react-responsive")
}, {ssr: false})

Useful link - Dynamic import with no ssr

5reactions
Tomekmularczykcommented, Dec 14, 2020

@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 with ua-parser-js. Then you wrap the whole app with ResponsiveContext.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…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found