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
const{ css, html }=StyleSheet.collect(()=>ReactDOMServer.renderToString(<App/>));
Please feel free to suggest what the API will look like, (let the bikeshedding begin!)
Also if we're already doing de-duping in SSR level, do we even need reset()? (related issue #378) Or would that just be a convenience function since we don't need it anymore?
Reacted by Einar Löve, Tom, Shripad Krishna, Cameron Manavian, Dylan Frankland, Loïc Mahieu, Satyajit Sahoo, Xiaobin Cui, Alex Reardon, Ben "Moose" Hadfield and 4 more
Reacted by Shripad Krishna, Dylan Frankland, Loïc Mahieu and Erik Cupal
One thing I would throw out there is that it would be very useful to be able to have separate bundles of CSS. That way, you could target styles to a style tag or to a separate stylesheet. When I worked for a publisher, we used this type of optimization to achieve very fast initial renders, with nonessential styles deferred until later.
I can ticket this separately, if that would be helpful. It would require some sort of ability to annotate at the component or CSS level where rulesets would be targeted, and an API for collecting the separate bundles (which might be a collect variant).
This happens either way @acjay. With styled-components, only the styles of components that are actually used on the initial render are injected 👍
Generating .css files for server-side rendered apps would not be complicated to add, but should not live in core. (it's more or less just a fs.write(css) away) We're going to work on a babel or webpack plugin to make this possible for non-ssr apps but that's a different issue. (see #59)
Do we also want to be able to export class names here ( in addition to CSS and HTML) like Aphrodite and Glamor does so that we can consume them on the front end to make sure things stay in sync or are just going to rely on the babel plugin to ensure deterministic class names?
Even if the names are deterministic, we still want a list of the class names, so we don't inject every rule twice. Either that, or maybe we could have a class attribute on style and links like __styled-component-sheet__, and then just build the list from those sheets.
I don't think we need to worry about duplicate classnames after SSR; just remove the server-provided style block after the app code arrived and let S-C rebuild the styles.
I like what Aphrodite does. Just pass the classnames along with the initial state and rehydrate without rebuilding client-side. Does anyone see any cons with this approach?
Oh right that's a different issue, that should be handled by the Babel
plugin.
Actually, I am running v2 with the plugin and it doesn't work for me, the
generated classnames change between server and client.
I don't understand how the names can differ though; given that the entire
render result is normally the same, and the order of evaluation thus also,
shouldn't the generated names be the same as well? If you use a
deterministic naming scheme, that is?
@wmertens I found that in order to get the names to match, I had to make sure each file was transpiled via babel once, and then the result re-used on client and server. I was initially applying babel twice (once for the server side and once for the client side). This was resulting in different names.
It would be cool if in the babel plugin, we could make the names deterministic (e.g. use an md5 hash of the file's source)
Reacted by Zach Smith, Jason Axelson and Sergio Toro
I think possibly the simplest option might be some kind of .styled-components-cache.json file that could map sha hashes of files + index of styled component to generated class name. That way we could ensure they were always deterministic, without having to make them long enough to avoid collisions.
On the other hand, if they're currently just random IDs, maybe just converting them to the first few letters of the sha has would be good enough.
And, this is about SSR, so there are no browser APIs available
Sure, you can just use a string if it's truly global, no APIs needed, but that's not what I want
If you have components right down to the document root, why do you need global styles?
Because the styles can be global styles for a single page, not global styles for the all pages. Different pages reuse the same document component. I can't just set global styles on the document because that will insert the styles to all the pages, and not specific pages
The problem with singleton is that it'll insert styles into all pages. I can have a docs page, and an about page with different fonts and such. And inserting same font declarations to both pages is wasteful.
Yeah I think the best way around that then is to really just render a <style> tag inline in your component hierarchy somewhere. It has the correct semantics (it has no effect once unmounted) and will be handled by React's rehydration just fine.
Reacted by Dylan Frankland, Jason Axelson, Karl Herrick, Raphaël Saunier, Thijs Koerselman, Jack Rimell, Rick, Nikita Glazov, matteocng and Bill Fienberg
@philpl@wmertens Wanted to check about the FoUC part mentioned in the above discussion - is that to be expected in development mode? Or is there a way around that?
Activity
acjay commentedon Jan 20, 2017
One thing I would throw out there is that it would be very useful to be able to have separate bundles of CSS. That way, you could target styles to a
styletag or to a separate stylesheet. When I worked for a publisher, we used this type of optimization to achieve very fast initial renders, with nonessential styles deferred until later.I can ticket this separately, if that would be helpful. It would require some sort of ability to annotate at the component or CSS level where rulesets would be targeted, and an API for collecting the separate bundles (which might be a
collectvariant).mxstbr commentedon Jan 21, 2017
This happens either way @acjay. With styled-components, only the styles of components that are actually used on the initial render are injected 👍
Generating
.cssfiles for server-side rendered apps would not be complicated to add, but should not live in core. (it's more or less just afs.write(css)away) We're going to work on a babel or webpack plugin to make this possible for non-ssr apps but that's a different issue. (see #59)dfrankland commentedon Jan 24, 2017
@ThisGuyChris,
reset()is great for doing testing. Considering that tagged templates have logic in them, that logic may need to be tested.mxstbr commentedon Jan 24, 2017
That's a great point actually @dfrankland, I hadn't even considered that use case!
camsjams commentedon Jan 30, 2017
Looks good to me
nicksrandall commentedon Jan 30, 2017
Do we also want to be able to export class names here ( in addition to CSS and HTML) like Aphrodite and Glamor does so that we can consume them on the front end to make sure things stay in sync or are just going to rely on the babel plugin to ensure deterministic class names?
xcoderzach commentedon Jan 31, 2017
Even if the names are deterministic, we still want a list of the class names, so we don't inject every rule twice. Either that, or maybe we could have a
classattribute onstyleandlinks like__styled-component-sheet__, and then just build the list from those sheets.wmertens commentedon Feb 1, 2017
I don't think we need to worry about duplicate classnames after SSR; just remove the server-provided style block after the app code arrived and let S-C rebuild the styles.
kukagg commentedon Feb 1, 2017
I like what Aphrodite does. Just pass the classnames along with the initial state and rehydrate without rebuilding client-side. Does anyone see any cons with this approach?
wmertens commentedon Feb 1, 2017
ForbesLindesay commentedon Feb 2, 2017
@wmertens I found that in order to get the names to match, I had to make sure each file was transpiled via babel once, and then the result re-used on client and server. I was initially applying babel twice (once for the server side and once for the client side). This was resulting in different names.
It would be cool if in the babel plugin, we could make the names deterministic (e.g. use an md5 hash of the file's source)
wmertens commentedon Feb 2, 2017
GGAlanSmithee commentedon Feb 2, 2017
I am experiencing the same @wmertens
ForbesLindesay commentedon Feb 2, 2017
I think possibly the simplest option might be some kind of
.styled-components-cache.jsonfile that could map sha hashes of files + index of styled component to generated class name. That way we could ensure they were always deterministic, without having to make them long enough to avoid collisions.On the other hand, if they're currently just random IDs, maybe just converting them to the first few letters of the sha has would be good enough.
76 remaining items
satya164 commentedon Apr 13, 2017
Sure, you can just use a string if it's truly global, no APIs needed, but that's not what I want
Because the styles can be global styles for a single page, not global styles for the all pages. Different pages reuse the same document component. I can't just set global styles on the document because that will insert the styles to all the pages, and not specific pages
The problem with singleton is that it'll insert styles into all pages. I can have a docs page, and an about page with different fonts and such. And inserting same font declarations to both pages is wasteful.
satya164 commentedon Apr 13, 2017
Portals don't, they exist outside of the current component tree.
It's not a new concept really. There are actually several components like
StatusBarin React Native which do this.geelen commentedon Apr 16, 2017
Yeah I think the best way around that then is to really just render a
<style>tag inline in your component hierarchy somewhere. It has the correct semantics (it has no effect once unmounted) and will be handled by React's rehydration just fine.mxstbr commentedon Apr 28, 2017
@Ismay
npm i --save styled-components@rc, use the API from #679 and you're good. I doubt we'll change it again!mxstbr commentedon Apr 28, 2017
I'll thusly also close this issue. Thanks for the lively discussion everybody!
kitten commentedon Apr 28, 2017
For anyone who ends up here and is searching for some documentation. There's some text explaining it briefly on the
upcomingdocs:https://sc-next-docs.philpl.com/docs/advanced#server-side-renderingEdit: As @ChingxWando says below, the website is now live. So the new link is: https://www.styled-components.com/docs/advanced#server-side-rendering
AxolotlxWando commentedon Jun 24, 2017
I believe the documentation was shortly updated afterwards and was live on the main site:
https://www.styled-components.com/docs/advanced#server-side-rendering
p.s. Hiii phillll & been wanting for ssr for so long & it's so exciting & thanks for creating styled component
oyeanuj commentedon Sep 30, 2017
@philpl @wmertens Wanted to check about the FoUC part mentioned in the above discussion - is that to be expected in development mode? Or is there a way around that?
GGAlanSmithee commentedon Sep 30, 2017
@oyeanuj as long as you render your styles server side, there won't be any FoUC, in development or production.
mxstbr commentedon Sep 30, 2017
@oyeanuj no, there shouldn't be any FOUC. Mind opening a new issue about that?
oyeanuj commentedon Oct 1, 2017
@mxstbr @GGAlanSmithee thanks for the quick response. It turned out to be an issue in which I had set up SC, so false alarm. Sorry for the noise!
wilfredonoyola commentedon Nov 23, 2017
Hey :) , I have integrated this with (https://github.com/reactjs/react-rails). Actually is not working.
Can you provider some solution?
Thanks,
kitten commentedon Nov 23, 2017
@wilfredonoyola can you please start a thread on Spectrum or open a new issue, as this one is unrelated, stale, and closed 🙂