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.

useBreakpoint issues with SSR (eg Next.js)

See original GitHub issue

I came across a bug in my project yesterday and I think have now traced it to styled-breakpoints.

I’ve made a minimal reproduction which you can find at https://github.com/tremby/next-mq-bug

The main branch has the broken case, which is also deployed at http://next-mq-bug-1.surge.sh/

In this reproduction, I initialize a variable mobile, then in a try/catch block I run the useBreakpoint hook. I have to run it in a try/catch because window does not exist during server-side rendering, and it’s impossible to know how wide the user’s browser window is. Without the try/catch block, an error would be thrown here: https://github.com/mg901/styled-breakpoints/blob/master/react-styled/index.js#L9

Whichever version of the display initialized mobile to the value matching the width of the viewport at load time, displays correctly. The other version displays incorrectly. The wrong styled-components classes have been added to the wrong elements. On resizing so that the media query flips, the version which displayed correctly still displays correctly. The version which started out wrong is still wrong.

Here are some screenshots.


After hitting refresh at a narrow width:

Screen Shot 2020-12-04 at 20 16 15


Without refresh, after then resizing to be wider:

Screen Shot 2020-12-04 at 20 16 25


After hitting refresh at that wider width:

Screen Shot 2020-12-04 at 20 16 29


And then after resizing to be narrower again without hitting refresh:

Screen Shot 2020-12-04 at 20 16 36


I then made my own implementation of useBreakpoint (albeit limited to a hard-coded media query) and I do not get the same bug. In my version I don’t actually need the try/catch block either, since everything which tries to use window happens in useEffect, and effects don’t run at SSR time. That version is on my “without-styled-breakpoints” branch, and is deployed here: http://next-mq-bug-2.surge.sh/

If you are open to a rewrite of your useBreakpoint function I could give it a shot. Or do you think the bug is actually elsewhere, like in styled-components or in Next.js?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
trembycommented, Dec 8, 2020

I suggest you open a new issue with more detail. The issue I described, and which this ticket is about, has been resolved.

0reactions
mg901commented, Mar 3, 2021

@fatehwaharp, good time. Did you manage to solve your problem? If not, why didn’t you open an issue? What error were you getting?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SSR for multiple breakpoints using React - DEV Community ‍ ‍
For this example, I am using: NextJS for React SSR; SCSS for styling; CSS Modules for modularizing SCSS styles. However, the final solution...
Read more >
SSR issue with dynamic responsive elements - vercel/next.js
I'm using reactstrap (bootstrap) together with react-responsive for my site's responsive layout. There is no problem when I'm using ...
Read more >
`useMediaQuery`hook that actually works with SSR : r/nextjs
I've seen projects that use react-responsive as well as SSR and it works just fine ... I run this in a brand new...
Read more >
next.js styles doesn't update on first render - Stack Overflow
Using next.js with SSR I'm having a strange problem. My website renders as expected server-side and is served to the client with styles....
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found