remix: [Bug]: Errors with Styled Components SSR
Which Remix packages are impacted?
@remix-run/react
What version of Remix are you using?
1.0.6
What version of Node are you using? Minimum supported version is 14.
14
Steps to Reproduce
https://codesandbox.io/s/remix-styled-bug-repro-5sv6v
- Create a new remix project
- Follow the steps detailed in the docs under styling: css-in-js
Expected Behavior
The client should hydrate the styles from styled-component without errors.
Actual Behavior
The style tag and all the style rules flash onto the screen as plain text before the javascript kicks in and removes them, applying the correct styles. Some content is also duplicated on the page (in the codesandbox above it is the footer). We also see an error in the console: Expected server HTML to contain a matching <link> in <head>.
Looks like the docs have you run const styles = sheet.getStyleTags() which returns a string. When react renders this string it html encodes it so you end up with a string in your head instead of a style tag.
Switching that to const styles = sheet.getStyleElement() causes the page to server render the correct styles. But as soon as js kicks in it removes those styles and throws this in the console: Warning: Did not expect server HTML to contain a <style> in <head>
If you instead gather up the styles and markup in a single render, and then inject the styles as a string: markup.replace('</head>', styles + '</head>') things seems to work better but it still throws this warning in the console: Warning: Prop 'className' did not match. Server: "sc-fFeiMQ chKvQT" Client: "sc-gsDKAQ fVbgCo"
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 22
- Comments: 26 (1 by maintainers)
Chat me on Whatsapp+2347026260392
On Mon, May 9, 2022, 7:39 PM Elijah Trillionz @.***> wrote:
I’ve published my plugin, try it out:
https://github.com/lukalabs/lukalabs-npm/tree/main/packages/esbuild-inject-plugin https://github.com/lukalabs/lukalabs-npm/tree/main/packages/esbuild-styled-components
If you have any issues please let me know.
@prxg22 fyi
Still no solution for this? @numso I tried following the example you shared but I still get the same error. and besides, it’s no different from what I already have in mine.
And I think before they work on styled-components supporting SSR without the babel-plugin they should work on making the babel-plugin work in remix.
Because in next.js there it’s just an easy setup with .babelrc, but it’s not the case in remix. I don’t know if it’s already possible in remix some other way, but I have searched all around but no solution.
And the error I get is something like
Warning: Prop 'className' did not match. Server: "sc-fFeiMQ chKvQT" Client: "sc-gsDKAQ fVbgCo"add babel-plugin-styled-components as dev dependency. add customer babel.config.js like blow
module.exports = { presets: [ "@remix-run/core/babel", ], plugins: [ [ "babel-plugin-styled-components", { "ssr": true } ] ] };this work for meI have a somewhat working solution for hydration issue, although I’m not ready to make it public yet. It monkey-patches Remix’s esbuild to add a custom plugin which then uses
tree-sitter(a fast code parser that supports TS) to parse the source and add component config, same way asstyled-components/macrodoes. For now it only supportsstyled.tagandstyled(...)(which are the most common usecases I believe). Gonna test it more and share it later.The styled-components example that was recently added to the dev branch worked well for me: https://github.com/remix-run/remix/tree/1576615c0bdb6222bcddf0ffefe19d2335169299/examples/styled-components
The hydration warning is still present. It looks like the plan is for styled-components to support SSR without needing the babel-plugin. That’s being tracked here: https://github.com/styled-components/styled-components/issues/3660
@prxg22 just realized there was
cssmissing in your example, that’s why it didn’t work for me:@AIEPhoenix Your suggestion has zero effect for me. Looking through the source and then finding this closed issue for implementing being able to do this seems to backs up it is not implemented? Issue #36
Ultimately it is what is needed though so that class names stay consist across the server and client.
https://styled-components.com/docs/advanced#tooling-setup
I’m sure enabling us to use/configure Babel would help a lot of people with a lot of issues 😄
I know Remix do not want to “lock in” to things (so they can swap out esbuild later if needed etc) but there’s so many tools we can’t use as things stand now 😓 (#717)
I appreciate and like their take too with how they recommend to do styling (and I have really had a good go at doing it that way!) but at work I work day in day out with styled-components so that’s where my mind and workflow is most efficient.
Sorry, @alexkuz, my bad 😅
@Elijah-trillionz as I said, it was not a fix on the remix app. We are using an ui component lib, which is built using babel. I just add the following line to its
babel.config.json:@prxg22 I could publish it this weekend I think, it works for us so far. It would be helpful to look at your example to see if it would help in your case.
I am getting this from a fresh remix install, not CSS or anything added to it.
Running into similar problems. Hopefully a solution can be found soon, as styled component support is a must for me!