Skip to content

Expose Server Rendering API - #214

Merged
mxstbr merged 22 commits into
styled-components:v2from
chiefjester:getStylesheet
Jan 11, 2017
Merged

mxstbr merged 22 commits into
styled-components:v2from
chiefjester:getStylesheet

Conversation

@chiefjester

Copy link
Copy Markdown
Member

@chiefjester

chiefjester commented Nov 18, 2016 •

Copy link
Copy Markdown
Member Author

@geelen and @mxstbr do I need to add tests here? I'm just exposing functions from glamor itself.

Comment thread src/index.js Outdated
/* Export everything */
export default styled
export { css, keyframes, injectGlobal, ThemeProvider }
export { css, keyframes, injectGlobal, ThemeProvider, serverStyleSheet }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to export it as ServerStyleSheet (pascal case)? What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we need a 3rd opinion? /cc @mxstbr / @geelen

@chiefjester

Copy link
Copy Markdown
Member Author

@diegohaz so I changed it to PascalCase as per your advice. Let me know what else do we need for this PR. /cc @mxstbr and @geelen

@mxstbr

mxstbr commented Nov 19, 2016

Copy link
Copy Markdown
Member

This LGTM code-wise, but maybe @geelen has something to say about the general approach?

@geelen geelen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread CHANGELOG.md Outdated
### 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs capitalisation here

Comment thread src/models/ServerStyleSheet.js Outdated
@@ -0,0 +1,8 @@
import styleSheet from './StyleSheet'

styleSheet.reset = styleSheet.flush

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/index.js Outdated
/* Export everything */
export default styled
export { css, keyframes, injectGlobal, ThemeProvider }
export { css, keyframes, injectGlobal, ThemeProvider, ServerStyleSheet }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're exporting a singleton instance, so I'd go with a name like serverStyles

@chiefjester chiefjester Nov 20, 2016 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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. 🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more about being consistent 😉 renaming both to serverStyles should be fine!

@mxstbr

mxstbr commented Nov 20, 2016

Copy link
Copy Markdown
Member

I think we need to bikeshed on the name a bit more, I'm not super happy with serverStyles. (sorry for the drawn out PR @ThisGuyChris!)

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. serverStyles.getCSS() kind of doesn't fit that, even client.getCSS() is better in that regard. (don't take that, it's not a good name)

Why don't we just call it styleSheet and make it part of our public API that way? This makes much more sense imo:

import { styleSheet } from 'styled-components'

styleSheet.getCSS()

From a users perspective it makes sense that styled-components has a style sheet, and that you can get the CSS of that style sheet as a string. This is a logical and fitting public API imo.

There shouldn't be any confusion with users thinking it's the glamor styleSheet, since nobody knows what that even is except for people who contribute, and those can also check what styleSheet is anyway.

@chiefjester

Copy link
Copy Markdown
Member Author

You're getting styles you want to have on the client. serverStyles.getCSS() kind of doesn't fit that

@mxstbr
Correct me if I'm wrong but we are actually getting styles to have on the server right? Hence the name seems appropriate? I don't really have a strong opinion if you want a more generic name like styleSheet. I can update this PR :)

@chiefjester

Copy link
Copy Markdown
Member Author

@mxstbr / @geelen cleaned up the commits, and just styleSheet as per your recommendation. Let me know if this works 👍

Comment thread CHANGELOG.md Outdated
- 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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate changelog entries?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoopsie, fixed

@mxstbr

mxstbr commented Nov 20, 2016

Copy link
Copy Markdown
Member

I much prefer the styleSheet.getCSS() API. @geelen?

Comment thread src/models/StyleSheet.js Outdated
return super.flush()
}
getCSS() {
return super.rules().map(rule => rule.cssText).join('\n')

@mxstbr mxstbr Nov 20, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 })

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh, I like this. 👏 Adding this option now.

@mxstbr

mxstbr commented Nov 20, 2016

Copy link
Copy Markdown
Member

Can we also add tests for this btw? Just render some things and make sure styleSheet.reset(), styleSheet.getCSS() and styleSheet.getCSS({min: false}) do the correct things 👌

(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)

@chiefjester

Copy link
Copy Markdown
Member Author

on it @mxstbr

@geelen

geelen commented Nov 20, 2016

Copy link
Copy Markdown
Member

This looks good to me. I'm gonna refactor StyleSheet.js a bit because it's growing in to our own thing, using Glamor underneath is an implementation detail. But it won't change the public API so feel free to release without that. (I'll PR separately)

@geelen

geelen commented Nov 20, 2016

Copy link
Copy Markdown
Member

Pushed my changes as a new branch and have PR-ed them against your project @ThisGuyChris: chiefjester#1

@chiefjester

chiefjester commented Nov 20, 2016 •

Copy link
Copy Markdown
Member Author

@geelen 👋 merged your commit from my fork and pushed it here in this PR.
@mxstbr let me know if these tests suffice.

@chiefjester

chiefjester commented Nov 20, 2016 •

Copy link
Copy Markdown
Member Author

It's worth to note that I wanted to write this exactly how @geelen wrote the refactor 👍 Thanks for the assist.

@geelen geelen mentioned this pull request Nov 21, 2016
@chiefjester
chiefjester force-pushed the getStylesheet branch 2 times, most recently from b7c3ae8 to 9a7f0a3 Compare November 21, 2016 13:57
@lucianlature

Copy link
Copy Markdown

Somebody please merge this :) 👍

@mxstbr

mxstbr commented Nov 21, 2016

Copy link
Copy Markdown
Member

@lucianlature we still need to figure out how we're going to do #227 before we can ship this, sorry!

@Kuirak

Kuirak commented Nov 23, 2016

Copy link
Copy Markdown

Is there any ETA when this will be ready?

@mxstbr
mxstbr changed the base branch from master to v2 January 11, 2017 11:07

@mxstbr mxstbr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
mxstbr merged commit 49bfb46 into styled-components:v2 Jan 11, 2017
@satya164

Copy link
Copy Markdown

@mxstbr what about a nicer API?

@mxstbr

mxstbr commented Jan 11, 2017

Copy link
Copy Markdown
Member

Ah sorry, what did I miss here?

@satya164

Copy link
Copy Markdown

@mxstbr the current API feels awkward. I love the API other libs like glamor and aphrodite have - #214 (comment)

@wmertens

Copy link
Copy Markdown
Contributor

@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.

@wmertens

Copy link
Copy Markdown
Contributor

@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?

@mxstbr

mxstbr commented Jan 11, 2017 •

Copy link
Copy Markdown
Member

This won't work without the babel plugin, which won't work without #227, which is why this is v2.

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.

Does it? I wasn't aware of that! But generally that's true, styled-components knows what the CSS is once it's been rendered once.

@threepointone

Copy link
Copy Markdown

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.

@satya164

Copy link
Copy Markdown

needs to do a render run to know which CSS to build, and then another render run to generate the string.

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?

Glamor /aphrodite don't do two passes

I'll trust @threepointone on this one

@wmertens

Copy link
Copy Markdown
Contributor

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 reset() on the stylesheet, and it is more obvious that one depends on the other. So I agree that

const { css, html } = StyleSheet.collect(() =>
  ReactDOMServer.renderToString(<App />)
);

is a nicer API.

@wmertens

Copy link
Copy Markdown
Contributor

@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 collect(cb) accepts either a string or an object from the cb, it would work. If it's a string, it presumes that to be the html, and if it's an object, it just adds css to the object and returns it. Would that work? Then you could have something like

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.

@satya164

satya164 commented Jan 12, 2017 •

Copy link
Copy Markdown

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.

@wmertens

wmertens commented Jan 12, 2017 via email

Copy link
Copy Markdown
Contributor

@wmertens

wmertens commented Feb 1, 2017

Copy link
Copy Markdown
Contributor

@ThisGuyChris I am using the v2 branch and I can't get SSR to work - I just get '' from getCSS(). Thoughts?

@wmertens

wmertens commented Feb 1, 2017

Copy link
Copy Markdown
Contributor

oh, scratch that, it works, but only once. That's due to #378

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.