-
-
Notifications
You must be signed in to change notification settings - Fork 298
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
How to work with Gatsby SSR & react-responsive #312
Comments
I do not recommend using it together with gatsby, after building it throws errors about hydration. "Hydration failed because the initial UI does not match what was rendered on the server."
|
Does gatsby have an equivalent of this next.js code? import dynamic from "next/dynamic"
const MediaQuery = dynamic(() => import("react-responsive"), {
ssr: false
}) |
@yocontra As Kyle Mathews said: "Yeah you should generally use CSS for media queries since when we render on the server, we don't know what kind of device will be viewing the page. If you do responsive stuff in JavaScript then your page will jump around often when you load because it'll look one way on the initial load and then when JS loads, it'll re-render based on the media query there." |
I think that's fine, using plain CSS media queries should work if they don't have the concept of dynamic imports. There are some novel workarounds (https://developer.chrome.com/blog/automating-resource-selection-with-client-hints/#automating-resource-selection-with-client-hints) that may make this easier to implement in the future just using the context stuff outlined in the README. Using client hints the browser can provide enough information for the server to accurately render, but only works with deviceWidth / width. No FF or Safari support either. |
Was facing a similar issue on nextjs. I did following as a workround
|
Going to close this since the answer from the gatsby team seems to be "just use css" and I don't disagree for building purely static sites that seems fine. |
Hello, I am integrating react-responsive into my Gatsby (V4) application. However, I am having trouble with server-side rendering (SSR). Specifically, I need to return a different context width in gatsby-ssr.js based on the type of device.
My idea would be to have a result that looks like this:
if (desktop) { <ResponsiveContext.Provider value={{ width: 1440 }}> {element} </ResponsiveContext.Provider> } else { <ResponsiveContext.Provider value={{ width: 991 }}> {element} </ResponsiveContext.Provider> }
I'm relatively new. Any help you can offer would be greatly appreciated.
The text was updated successfully, but these errors were encountered: