You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thought I'd open an issue to kick off the discussion. It's already possible because of the way we build on top of Glamor, but we haven't exposed it as an API. But basically, if you did this:
importstyleSheetfrom'styled-components/lib/models/StyleSheet'/* before each render */styleSheet.flush()/* after each render */styleSheet.rules().map(rule=>rule.cssText).join('\n')
Then you should get the chunk of CSS you need for each request. Can someone with a server-rendered setup take a look and confirm this works, and maybe show how they'd be invoking it? I'd be happy enough to export something like:
import{serverStylesheet}from'styled-components'/* before each render */serverStylesheet.reset()/* after each render */serverStylesheet.getCSS()
What do people think?
Reacted by aesopwolf, Erik Rasmussen, Nick Breaton, Clément, Eric Fennell, Alexander, Brian Hough, Sova, Jamie Dixon, Shripad Krishna and 14 more
I'll gladly help test this out! We currently avoid using styled components for most styles because of the unclear path to support SSR, would be great to move the inline styles into styled components if we can generate the CSS on the server.
Unfortunately, I'm getting back an empty array []. I can confirm that one of the components nested inside Root is wrapped by styled and generates a className when the client loads.
I'm currently implementing SSR on a project and I managed to make this work without styleSheet.flush() (if I call this, the result is the same as @pheuter), only the styles from injectGlobal weren't included.
Once I finish it I will post the complete code and a demo here. (I'm trying to make the app work completely without javascript enabled on client).
I noticed something.
I'm using Grid from 'grid-styled' and using styleSheet.rules().map(rule => rule.cssText).join('\n') seems to go just one level deep. It never rendered code of Grid, but rendered my code that used media queries.
The element receives the correct classname on SSR (class="iMsaJZ hqknNf"), wich are two classes, one for my code(iMsaJZ) and one for Grid's(hqknNf).
But there is no mention to the classname designed to Grid in the initial server response.
When I turn JS on then Grid's code appears to the browser.
Inlining code solves the problem and ssr can be used but it breaks composition :(
I hope I made myself clear :)
Also to notice that while profiling on my dev pc (windows 10) I got 0 ~ 1ms latency calling styleSheet.rules().map(rule => rule.cssText).join('\n')
That's nice!
Based on @pheuter test, I get the same result,(the class is injected in the string markup) but not calling stylesheet.flush() before calling styleSheet.rules() produces TypeError: Cannot read property 'cssRules' of undefined
Please get in touch when somebody start to handle this. I would like to get this to work out of the box in Phenomic (I just integrated Glamor and Aphrodite rendering and would like to cover all populars CSS in JS solutions).
I was thinking glamor integration will be enough but @philpljust told me it won't work :(
@aesopwolf did you get the same problem as @diegohaz of not having styles included from injectGlobal calls?
I think its a general consensus that this is working out of the box. The only minor caveat is styles from injectGlobal isn't included. But fixing that is trivial I guess?
@tomazy You can always remove your SSR style block once the client-side rendering is done. That way, your page will look great while the app is loading and you will not get duplicate styles.
@tomazy I used something similar to remove the server rendered critical-css block when react and styled-components kicked in on the client side.
The problem with this approach is that it will also remove any styles you injected with injectGlobals,Those styles, if rendered on the server, will end up in the ssr block.
It is also feels like a dirty hack compared to the rest of the experience offered by styled-components 😛
Of course there are simpler ways to stick some global css in the pre-rendered page's head section but the point is that it would be nice to have proper support for it.
I am trying to use hedron, but it seems the styles from it are not present. But my custom styles are rendered fine. Any ideas why this is happening/how to work around?
@dignifiedquire - Could you please tell me how you worked around this? I'm having the exact same issue with hedron. @diegohaz - Using the universal-redux branch for this, is there anything that I should look into particularly?
Activity
pheuter commentedon Oct 22, 2016
I'll gladly help test this out! We currently avoid using styled components for most styles because of the unclear path to support SSR, would be great to move the inline styles into styled components if we can generate the CSS on the server.
Here is what I have setup so far:
Unfortunately, I'm getting back an empty array
[]. I can confirm that one of the components nested insideRootis wrapped bystyledand generates a className when the client loads.diegohaz commentedon Oct 24, 2016
I'm currently implementing SSR on a project and I managed to make this work without
styleSheet.flush()(if I call this, the result is the same as @pheuter), only the styles frominjectGlobalweren't included.Once I finish it I will post the complete code and a demo here. (I'm trying to make the app work completely without javascript enabled on client).
mxstbr commentedon Oct 24, 2016
Huh, that's interesting. I think I might accidentally use another
styleSheetfor that? /cc @geelendiegohaz commentedon Oct 24, 2016
Demo: https://arc.diegohaz.com (disable javascript, even the form works).
Code: https://github.com/diegohaz/arc/blob/universal-redux/src/server.js#L74
It doesn't work with
injectGlobal(I removed).The
serverStylesheet.getCSS()API would be nice. 👍lucasavila00 commentedon Oct 29, 2016
I noticed something.
I'm using Grid from 'grid-styled' and using
styleSheet.rules().map(rule => rule.cssText).join('\n')seems to go just one level deep. It never rendered code of Grid, but rendered my code that used media queries.code is like this:
Invoked as
<Wrapper xs={1} sm={1} md={2/3} lg={1/2}>{children}</Wrapper>css gets rendered as
The element receives the correct classname on SSR (class="iMsaJZ hqknNf"), wich are two classes, one for my code(iMsaJZ) and one for Grid's(hqknNf).
But there is no mention to the classname designed to Grid in the initial server response.
When I turn JS on then Grid's code appears to the browser.
Inlining code solves the problem and ssr can be used but it breaks composition :(
I hope I made myself clear :)
Also to notice that while profiling on my dev pc (windows 10) I got 0 ~ 1ms latency calling styleSheet.rules().map(rule => rule.cssText).join('\n')
That's nice!
gbozee commentedon Nov 2, 2016
Based on @pheuter test, I get the same result,(the class is injected in the string markup) but not calling
stylesheet.flush()before callingstyleSheet.rules()producesTypeError: Cannot read property 'cssRules' of undefinedaesopwolf commentedon Nov 3, 2016
Ah, this is so great. I was able to get it working with gatsby in no time
I'm all for exposing a simpler api via
serverStylesheet.getCSS()kitten commentedon Nov 9, 2016
@gbozee I ran into that problem. Turns out when (f.e.) webpack 2 is used, it loads the es bundle, due to the
package.jsonjs:next/module fields.Until there is an API for the stylesheet that we can use, you should work around it using webpack's
resolve.aliasto load thelibfolder instead.✨ Added: support for ``Glamor`` and ``styled-components`` out of the …
✨ Added: support for ``Glamor`` and ``styled-components`` out of the …
diegohaz commentedon Nov 16, 2016
@mxstbr I'm interested in solving this (
injectGlobal). Can you give me a way?MoOx commentedon Nov 17, 2016
Please get in touch when somebody start to handle this. I would like to get this to work out of the box in Phenomic (I just integrated Glamor and Aphrodite rendering and would like to cover all populars CSS in JS solutions).
I was thinking glamor integration will be enough but @philpl just told me it won't work :(
chiefjester commentedon Nov 17, 2016
@aesopwolf did you get the same problem as @diegohaz of not having styles included from injectGlobal calls?
I think its a general consensus that this is working out of the box. The only minor caveat is styles from injectGlobal isn't included. But fixing that is trivial I guess?
Docs: remove mention of styled-components
27 remaining items
ItsBenCodes commentedon Dec 30, 2016
@tomazy You can always remove your SSR style block once the client-side rendering is done. That way, your page will look great while the app is loading and you will not get duplicate styles.
codepunkt commentedon Dec 30, 2016
@exon yes, but styled-components could also just stop injecting it on the client-side again so you don't have to hack stuff like that together ;-)
See #214 for more discussion about this.
teonik commentedon Dec 31, 2016
@tomazy I used something similar to remove the server rendered critical-css block when react and styled-components kicked in on the client side.
The problem with this approach is that it will also remove any styles you injected with injectGlobals,Those styles, if rendered on the server, will end up in the ssr block.
It is also feels like a dirty hack compared to the rest of the experience offered by styled-components 😛
Of course there are simpler ways to stick some global css in the pre-rendered page's
headsection but the point is that it would be nice to have proper support for it.Expose Server Rendering API
kristojorg commentedon Jan 19, 2017
PSA: I was seeing
TypeError: Cannot read property 'cssRules' of undefinedwhen using this line of code:const styles = styleSheet.rules().map(rule => rule.cssText).join('\n');The fix: If you are seeing this as well, make sure you are rendering at least one styled component in your app.
For me, I was just installing
styled-componentsfor the first time, and hadn't used one yet, and was therefore seeing this error.kristojorg commentedon Jan 20, 2017
I have submitted a PR to fix this issue here. Hope this helps some!
karthikiyengar commentedon Mar 23, 2017
@dignifiedquire - Could you please tell me how you worked around this? I'm having the exact same issue with hedron.
@diegohaz - Using the universal-redux branch for this, is there anything that I should look into particularly?
kristojorg commentedon Mar 24, 2017
@karthikiyengar Hedron is using styled-components which I believe means you need to import both your own server markup and theirs.
karthikiyengar commentedon Mar 24, 2017
@kristojorg - Thanks for your answer.
Please disregard, my vendor assets weren't being generated properly, leading to the issue.
mxstbr commentedon Apr 1, 2017
This discussion has now been moved to #386.
✨ Added: support for ``Glamor`` and ``styled-components`` out of the …
Docs: remove mention of styled-components