Replies: 4 comments 6 replies
-
is that a remix warning or the react hydration warning? if you're sure what you're doing is expected, you can add |
Beta Was this translation helpful? Give feedback.
-
I answered you on Discord (🙏 try to don't duplicate so people don't answer on both places) but I will copy it here: the error is shown by React not Remix
you can solve the light/dark mode issue by using CSS media queries instead of localStorage, or if you want to let the user pick a theme use a cookie to keep the user preference, this way you can return the cookie value from the loader and render the correct theme
you can solve the timezone issues by
1. Using your server timezone always
2. Using your server timezone on a first visit, then after the user loads the page detect the timezone and save it in a cookie, from that point use the timezone from the cookie
3. Delay showing any date to the browser
I personally would use 1 or 2, but many prefer 3, you can create a component to render dates that can use ClientOnly from Remix Utils to only render the date client-side after hydration, and server-side you can render null or something else
finally, check Epic Stack's Client Hint package https://github.com/epicweb-dev/client-hints as it was built to solve these issues
it even has a built-in solution to timezones using @epic-web/client-hints/time-zone' that will simplify implementing the second solution |
Beta Was this translation helpful? Give feedback.
-
@flexdinesh It's a Remix warning, it's warning that the server-rendered HTML code is different from what the client rendered. It's a correct warning, it's just so happens I can't make it always equal because I can't check the user's timezone or the system light/dark mode until I have access to the user's browser document object. @sergiodxa It's not a localStorage issue. It's actually not a timezone issue either. It's a DOM API issue, something I can't access until I have access to the document DOM. |
Beta Was this translation helpful? Give feedback.
-
@sergiodxa Also, yes I am using CSS media queries. It's just when the user selects "SYSTEM", I'm suppose to auto-pick the computer's current light/dark mode and I can't access that without the DOM API. Which means, Server Side Rendering can never "always" have same code as the client 100% of the time. Which makes it difficult to get rid of this warning. |
Beta Was this translation helpful? Give feedback.
-
When the server html code does not match the rendered html code in client, Remix (correctly) shows a warning that the prop/html did not match what was rendered in the server.
But sometimes, there's just no way to get the exact HTML code in the server to match the client render.
For example, a "system" light mode is a common feature where user's theme changes from light to dark mode depending on time of the day. User's timezone and whether they selected the computer to "prefer" night mode, is hard information to get to the server for server side rendering.
Is there a way to "expect" error so the warning is silenced?
Beta Was this translation helpful? Give feedback.
All reactions