Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server side rendering wrong css styles #30

Open
simb4 opened this issue Sep 15, 2019 · 9 comments
Open

Server side rendering wrong css styles #30

simb4 opened this issue Sep 15, 2019 · 9 comments

Comments

@simb4
Copy link

simb4 commented Sep 15, 2019

I've got Header that renders different elements depending on viewport. When mobile page is loaded, some css styles from desktop viewport are stay on mobile viewport elements, which is strange:

<Breakpoint medium up>
  <div1 />
  <div2 className='a' />
  <div3 />
</Breakpoint>
<Breakpoint small down>
  <div4 />
  <div5 />
  <div6 />
</Breakpoint>

on mobile viewports results in:

...
  <div4 />
  <div5 className='a' />
  <div6 />
...

Where <divN> some component.

@mackan92
Copy link

I have the same issue. To solve it I use Bootstrap's breakpoint classes instead. Not optimal considering there will be a lot of unnecessary rendering. Would appreciate if this issue can be solved.

@flexdinesh
Copy link
Owner

This is because while rendering in server, React cannot identify the breakpoint as breakpoints exist only in browsers. That's why we default to desktop breakpoint in server. One solution is to extend the api in a way that users can identify useragent from request header and pass it in to the lib as default. It will take some time to get it done as the approach is not standard and we will have to unify the approach for all server frameworks (Next, Gatsby, vanilla).

@simb4
Copy link
Author

simb4 commented Oct 28, 2019

This is because while rendering in server, React cannot identify the breakpoint as breakpoints exist only in browsers. That's why we default to desktop breakpoint in server. One solution is to extend the api in a way that users can identify useragent from request header and pass it in to the lib as default. It will take some time to get it done as the approach is not standard and we will have to unify the approach for all server frameworks (Next, Gatsby, vanilla).

Yes, this is exactly what I figured out while trying to surpass this problem.
But the problem is, that while rendered on client side (after hydrate) we don't see the correct elements, so I need to force update it to render properly.
I have 2 solutions(ish, they are ugly) for now, you can either hide them on server side to show up only on client side(when you know dimensions), or you can force update after component mounted on client side.

Is there any approach to handle this without re-render (and without integrating with server side)?

@cyril94440
Copy link

Same issue here... :( Too bad this library could be very useful

@dongepulango
Copy link

Same Issue here. Anyone with a workaround to it yet?

@ghost
Copy link

ghost commented Aug 30, 2020

Same issue.

@hems
Copy link

hems commented Sep 21, 2020

This is because while rendering in server, React cannot identify the breakpoint as breakpoints exist only in browsers. That's why we default to desktop breakpoint in server.

I reckon the problem lies here on the function getWidthSafely() as it defaults to 9999.

It's probably a good idea to have an option to set that default value instead of having 9999, that would be easy to implement and would probably be already enough for most use-cases?

One solution is to extend the api in a way that users can identify useragent from request header and pass it in to the lib as default.

With https://www.npmjs.com/package/device ( which powers https://github.com/rguerreiro/express-device ) it should be very easy to implement that.

It will take some time to get it done as the approach is not standard and we will have to unify the approach for all server frameworks (Next, Gatsby, vanilla).

I believe with the option to set default width and the device package people should be able to sort implement themselves while we don't sort out out-of-the-box compatibility with Next and Gatsby?

@hems
Copy link

hems commented Sep 21, 2020

This is because while rendering in server, React cannot identify the breakpoint as breakpoints exist only in browsers. That's why we default to desktop breakpoint in server.

I reckon the problem lies here on the function getWidthSafely() as it defaults to 9999.

I have created a PR #40 which adds this option to the library, this way at least we can set default value to be the mobile breakpoint, so server-side would render mobile-first.

From that point on I believe workaround can be implemented by users on case-to-case basis?

I'm using Next.js myself, so i had to implement a quick fix, i was able to do it by installing https://www.npmjs.com/package/next-useragent and then calling the function setDefaultWidth based on the flag isMobile, and it seens to be working so far!

cc: @YJoris @dongepulango @cyril94440

@flexdinesh
Copy link
Owner

There is no magical fix for this but setDefaultWidth API published as part of v2.2.0 will help with this. This is a popular issue and I'm gonna keep it open for other folks who might run into similar problems.

@flexdinesh flexdinesh pinned this issue Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants