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.

Responsive: SSR is broken

See original GitHub issue

Stack: Latest versions of Next.js, React, Semanic UI React.

Issue: When I attempt to render the the example layout from here using Next.js, I get a warning that says Expected server HTML to contain a matching <div> in [<div>.]

I believe it’s related to this issue https://github.com/Semantic-Org/Semantic-UI-React/issues/3110 which has something to do with the server not getting the browser size.

However, none of the solutions in that link worked for me. One requirement I have is that SSR cannot be turned off.

Here’s the code I’m using: https://github.com/jlei523/semantic-ui-next

Minor note: I believe https://react.semantic-ui.com/layouts/homepage/ is also affected by this issue. Go to the URL and then refresh the page and you should see the styles get messed up. If you shrink the browser window and then shrink it back, it displays the elements correctly again.

Has anyone solved this problem already without turning SSR off? Also, does anyone have an example of how to do display mobile specific elements using SUIR without turning off SSR?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
layershiftercommented, Jan 10, 2019

Solution 1: use react-no-ssr

It’s simple, will omit all warnings.

+import NoSSR from 'react-no-ssr'

//

-<div>
+<NoSSR>
  <DesktopContainer>{children}</DesktopContainer>
  <MobileContainer>{children}</MobileContainer>
-</NoSSR>
+</div>

HalfSolution 2: use getWidth() prop

const isBrowser = () => typeof window !== 'undefined'
const getWidth = () => {
  if (isBrowser()) return window.innerWidth
  return 1000
}
-<Responsive>
+<Responsive getWidth={getWidth}>

Still will cause a warning on mobile devices 🐗

Solution 3: use mobile-detect

The best solution that I know, everything works like a charm 👍

+import MobileDetect from 'mobile-detect'

+const getWidthFactory = (isMobileFromSSR) => () => {
+  const isSSR = typeof window === 'undefined';
+  const ssrValue = isMobileFromSSR ? 767 : 768
+
+  return isSSR ? ssrValue : window.innerWidth
+}

//

-const HomepageLayout = () => (
+const HomepageLayout = ({ getWidth }) => (

//

+HomepageLayout.getInitialProps = async ({ req }) => {
+  const result = new MobileDetect(req.headers['user-agent'])
+  const isMobile = !!result.mobile()
+
+  return { getWidth: getWidthFactory(isMobile) }
+}

//

-const ResponsiveContainer = ({ children }) => (
-const ResponsiveContainer = ({ children, getWidth }) => (
  <div>
-    <DesktopContainer>{children}</DesktopContainer>
-    <MobileContainer>{children}</MobileContainer>
+    <DesktopContainer getWidth={getWidth}>{children}</DesktopContainer>
+    <MobileContainer getWidth={getWidth}>{children}</MobileContainer>
  </div>
);

Live example on Codesandbox: https://codesandbox.io/s/ly3zpw7yzm

0reactions
layershiftercommented, Jul 28, 2020

https://github.com/Semantic-Org/Semantic-UI-React/issues/4002#issuecomment-665000116

Responsive will be deprecated and removed. I will provide an upgrade guide.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Responsive Rendering With SSR : r/reactjs - Reddit
However, doing this via SSR is an interesting problem. I honestly don't think there is a solution. The best I can come up...
Read more >
react-responsive doesn't work with Server-side rendering
This way, the UI doesn't appear broken before hydration. Share.
Read more >
Server-Rendering Responsively - Artsy Engineering
Combining SSR and responsive design is a non-trivial problem. There are many concerns to manage, and they are sometimes in conflict with each ......
Read more >
Server rendering - Material UI - MUI
When the server receives the request, it renders the required component(s) into an HTML string, and then sends it as a response to...
Read more >
react-hydration-error - Next.js
When css-in-js libraries are not set up for pre-rendering (SSR/SSG) it will often lead to a hydration mismatch. In general this means the...
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 Hashnode Post

No results found