Repository navigation
Expose Server Rendering API - #214
Conversation
chiefjester
commented
Nov 18, 2016
- fixes Server Side Rendering #124
- remap flush to reset()
- convenience function getCSS() for SSR
7bc8311 to
ce0776f
Compare
| /* Export everything */ | ||
| export default styled | ||
| export { css, keyframes, injectGlobal, ThemeProvider } | ||
| export { css, keyframes, injectGlobal, ThemeProvider, serverStyleSheet } |
There was a problem hiding this comment.
Isn't it better to export it as ServerStyleSheet (pascal case)? What do you think?
There was a problem hiding this comment.
I'm just following their convention, works for me either way. Please refer to:
https://github.com/styled-components/styled-components/blob/master/src/models/GlobalStyle.js#L7
where they import styleSheet from './StyleSheet'
Also I think ThemeProvider is actually a component hence its pascal case.
|
This LGTM code-wise, but maybe @geelen has something to say about the general approach? |
geelen
left a comment
There was a problem hiding this comment.
Happy with the approach for now. My only question is whether we should be exporting serverStyles (i.e. something SSR-specific) or just a styleSheet.
But that can come later. Happy for this to be merged.
| ### Added | ||
|
|
||
| - N/A | ||
| - Expose API for Server Side rendering: `serverStyleSheet.reset()` and `serverStyleSheet.getCSS()`, thanks to [@thisguychris](https://github.com/thisguychris), (see [#214](https://github.com/styled-components/styled-components/pull/214)) fixes [#124](https://github.com/styled-components/styled-components/issues/124) |
| @@ -0,0 +1,8 @@ | |||
| import styleSheet from './StyleSheet' | |||
|
|
|||
| styleSheet.reset = styleSheet.flush | |||
There was a problem hiding this comment.
Hmm, I'd prefer not mutating things here. I'd suggest this:
class ServerStyleSheet {
reset() {
styleSheet.flush()
}
getCSS() {
return styleSheet.rules().map(rule => rule.cssText).join('\n')
}
}
export default new ServerStyleSheet()That way if styleSheet changes we don't have potentially public-facing changes to worry about here.
| /* Export everything */ | ||
| export default styled | ||
| export { css, keyframes, injectGlobal, ThemeProvider } | ||
| export { css, keyframes, injectGlobal, ThemeProvider, ServerStyleSheet } |
There was a problem hiding this comment.
You're exporting a singleton instance, so I'd go with a name like serverStyles
There was a problem hiding this comment.
@geelen this contradicts the one in the CHANGELOG.md comment. You are wanting to remove the capitalization in the export even though you want capitalization in the CHANGELOG. 🤔
There was a problem hiding this comment.
I think it's more about being consistent 😉 renaming both to serverStyles should be fine!
b33fbbb to
e216704
Compare
|
I think we need to bikeshed on the name a bit more, I'm not super happy with This is a bit weird: import { serverStyles } from 'styled-components'
serverStyles.getCSS()I don't like the name, because you're not getting the CSS of some styles that are used on the server. (whatever that would mean) You're getting styles you want to have on the client. Why don't we just call it import { styleSheet } from 'styled-components'
styleSheet.getCSS()From a users perspective it makes sense that
|
c69021c to
21c9197
Compare
@mxstbr |
fee45b4 to
dcb7a14
Compare
| - Added support for camelized style properties in ReactNative (e.g. `fontWeight`), thanks [@jacobp100](https://github.com/jacobp100). (see [#145](https://github.com/styled-components/styled-components/pull/145)) | ||
| - Properly expose `flow` typings by adding a `flow:build` step and `flow` support docs, thanks to [@ryyppy](https://github.com/ryyppy). (see [#219](https://github.com/styled-components/styled-components/pull/219)) | ||
| - Added support for deeply nested styles in ReactNative (e.g. `transform`), thanks [@jacobp100][https://github.com/jacobp100]. (see [#139](https://github.com/styled-components/styled-components/pull/139)) | ||
| - Added support for camelized style properties in ReactNative (e.g. `fontWeight`), thanks [@jacobp100][https://github.com/jacobp100]. (see [#145](https://github.com/styled-components/styled-components/pull/145)) |
|
I much prefer the |
| return super.flush() | ||
| } | ||
| getCSS() { | ||
| return super.rules().map(rule => rule.cssText).join('\n') |
There was a problem hiding this comment.
Why are we including linebreaks by default? Shouldn't we just do .join('') for performance reasons, so that the code the developers send down to the client is as small as possible?
If we want to allow devs to get non-minified CSS (for debugging purposes), maybe we could have an option called min which is set to true by default?
getCSS({ min = true }) {
return super.rules().map(rule => rule.cssText).join(min ? '' : '\n')
}That way devs can do normal mode, which is minified, or non-minified mode if they want to debug:
// minified
styleSheet.getCSS()
// Not minified
styleSheet.getCSS({ min: false })There was a problem hiding this comment.
oooh, I like this. 👏 Adding this option now.
|
Can we also add tests for this btw? Just render some things and make sure (Sorry for the bikeshedding, but this'll be a huge part of our public API so we need to make sure we're solid here) |
|
on it @mxstbr |
|
This looks good to me. I'm gonna refactor |
|
Pushed my changes as a new branch and have PR-ed them against your project @ThisGuyChris: chiefjester#1 |
a8c2981 to
1554629
Compare
|
It's worth to note that I wanted to write this exactly how @geelen wrote the refactor 👍 Thanks for the assist. |
1554629 to
a8c2981
Compare
b7c3ae8 to
9a7f0a3
Compare
|
Somebody please merge this :) 👍 |
|
@lucianlature we still need to figure out how we're going to do #227 before we can ship this, sorry! |
|
Is there any ETA when this will be ready? |
fdd3c69 to
6b0c2ef
Compare
- get injected property
Update README.md
added vim syntax plugin to the readme
…ity-doc Add security doc
mxstbr
left a comment
There was a problem hiding this comment.
Amazing work, thanks so much @ThisGuyChris! Shipping this in the v2 branch for now, would appreciate a PR against that implementing some first rehydration functionality. (the branch contains the new className stuff too!)
|
@mxstbr what about a nicer API? |
|
Ah sorry, what did I miss here? |
|
@mxstbr the current API feels awkward. I love the API other libs like glamor and aphrodite have - #214 (comment) |
|
@satya164 the example you propose actually needs to do a render run to know which CSS to build, and then another render run to generate the string. This is expensive. S-C generates out-of-band data, so it is reasonable to receive it out-of-band too… Redux store collection works in the same way. I'm happy with the current API. |
|
@mxstbr is there a reason this can't go in v1? It only extends the API, right? How can I try v2, is there breaking API? |
|
This won't work without the babel plugin, which won't work without #227, which is why this is v2.
Does it? I wasn't aware of that! But generally that's true, |
|
Glamor /aphrodite don't do two passes. Glamor works on raw HTML as well, so you don't even have to run the renderToString inside the callback. |
Can't it use the same render to get the CSS that it uses to get the HTML string? Why would it need two passes? How is it diffrent from calling these 3 functions manually?
I'll trust @threepointone on this one |
|
Very strange, I thought I left a comment yesterday but now I don't see it. I was wrong in my comment about the API, there are no two passes, the callback is for proper lifecycling of the style generation. With the API @satya164 proposes, you don't need to call const { css, html } = StyleSheet.collect(() =>
ReactDOMServer.renderToString(<App />)
);is a nicer API. |
|
@satya164 @mxstbr I found a problem with that nicer API: it is not composable. If you have other out-of-band data that is generated (like HEAD metadata or SVG symbols), you can't nest this. Unless… if const {css, html, svg, headers} = StyleSheet.collect(SvgSprites.collect(Helmet.collect(
() => ReactDOMServer.renderToString(<App />)
)))all those other projects have to do the same, of course. |
|
It will work just fine. let { css, html, svg };
{ svg } => Svg.collect(() => {
{ css, html } = StyleSheet.collect(() => ReactDOMServer.renderToString(<App />);
return html; // don't need this return if `Svg.collect` doesn't need the html string
});Even though it completely depends on what kind of API other libs have, it will work fine in any kind of API. Probably need to write a helper function to make it look nicer, but it works. |
|
Hmmm yeah I suppose. I dislike that I have to use `let` though :P
…On Thu, Jan 12, 2017 at 1:53 PM Satyajit Sahoo ***@***.***> wrote:
It will work just fine.
let { css, html, svg };
{ svg } => Svg.collect(() => {
{ css, html } = StyleSheet.collect(() => ReactDOMServer.renderToString(<App />);
return html; // don't need this return if `Svg.collect` doesn't need the html string
});
Even though it completely depends on what kind of API other libs have, it
will work fine in any kind of API.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#214 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADWlsj4Zs_JtqktWwZkEhuwdTDauyNQks5rRiJXgaJpZM4K2ni0>
.
|
|
@ThisGuyChris I am using the v2 branch and I can't get SSR to work - I just get |
|
oh, scratch that, it works, but only once. That's due to #378 |