Skip to content

Rework stylesheet for ssr - #679

Merged
geelen merged 65 commits into
v2from
rework-stylesheet-for-ssr
Apr 16, 2017
Merged

geelen merged 65 commits into
v2from
rework-stylesheet-for-ssr

Conversation

@geelen

@geelen geelen commented Apr 11, 2017 •

Copy link
Copy Markdown
Member

So, this is the all-singing, all-dancing StyleSheet to end all stylesheets. So far I have rehydration working, as well as deterministic local & global stylesheets. The serialised format looks like this:

<style data-styled-components
       data-styled-components-is-local="false">
  /* sc-component-id: sc-global-1455077013 */
 html { font-size: 16px; }
  /* sc-component-id: sc-global-557410406 */
 body { background: papayawhip; }
</style>
<style data-styled-components="gZjIar dzbzmI"
       data-styled-components-is-local="true">
 /* sc-component-id: ONE */
 .ONE {}
 .gZjIar { color: blue; }
 /* sc-component-id: TWO */
 .TWO {}
 .dzbzmI { color: red; }
</style>

What this lets us do is figure out that there have been two injectGlobal calls and two StyledComponents generated. When the BrowserStyleSheet is created, it reads anything in the DOM with a data-styled-components attribute and builds up a picture of the existing styles.

In the above case, that means four componentIds sc-global-1455077013, sc-global-557410406, ONE and TWO. It also reads the classnames gZjIar and dzbzmI so it knows not to generate those in future.

What this means is that when your app boots, whether or not it's using the no-parser stuff, it won't inject CSS into the head that it doesn't need to. But, when a dynamic style comes along, it all still works.

Here's my todo list for this feature (which I'll be updating as I go):

  • strip back as close to a single inject API call as possible.
  • switch all usages of the existing StyleSheet across
  • handle converting from the initial <style> tag with a single text node child to a one-node-per-component so we can use appendData to inject new CSS in the right order.
  • add a test for keyframes
  • make a ServerStyleSheet that generates this exact format
  • make the StyleSheetProvider pass the instance through context
  • build the SSR api described in Discuss SSR API and methods #386
  • construct the singleton + cloning API on the server
  • delete the old code, including Glamor's sheet (now vendored)
  • hoist @import statements to the top of style tags (pulls @import rules to the top of the stylesheet threepointone/glamor#151) Gonna raise this separately
  • actually keyframes don't need to be global, make them local
  • stop sending down empty classnames for unused components
  • dispatch injectGlobal and keyframes from the singleton to each instance

Lots still to do, but this feels like a really good feature to make SSR really well integrated into Styled Components. Will try to get this finished as soon as I can!

jacobp100 and others added 30 commits March 1, 2017 09:42
Conflicts:
	src/test/basic.test.js
Conflicts:
	src/constructors/keyframes.js
	src/models/ComponentStyle.js
	src/models/test/ThemeProvider.test.js
	src/test/extending.test.js
	src/vendor/glamor/hash.js
clones.delete(this.instance)
this.closed = true
if (!this.closed) {
clones.splice(clones.indexOf(this.instance), 1)

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 miss Set

@geelen

geelen commented Apr 16, 2017

Copy link
Copy Markdown
Member Author

Another small thing, once this lands SSR will be incompatible with next.js. We should probably raise an issue over on their repo, to discuss our approach.

Why is that? Just the public API?

@mxstbr-bot

mxstbr-bot commented Apr 16, 2017 •

Copy link
Copy Markdown
Warnings
⚠️ ❗ Big PR
⚠️ There are library changes, but not tests. That's OK as long as you're refactoring existing code

Generated by 🚫 dangerJS

@geelen

geelen commented Apr 16, 2017

Copy link
Copy Markdown
Member Author

Thanks for your help! I've tweaked a bunch of things, changed up the Context key, exported StyleSheetManager. I'm gonna merge this and cut a new v2 release.

@geelen
geelen merged commit dcee185 into v2 Apr 16, 2017
@geelen
geelen deleted the rework-stylesheet-for-ssr branch April 16, 2017 05:29
@mxstbr

mxstbr commented Apr 16, 2017

Copy link
Copy Markdown
Member

Before you cut a new release, this will break a lot of folks. We should add documentation.

@geelen

geelen commented Apr 16, 2017

Copy link
Copy Markdown
Member Author

It... shouldn't break anything except users of the current SSR API, right?

@geelen

geelen commented Apr 16, 2017

Copy link
Copy Markdown
Member Author

@mxstbr @philpl just deployed this to https://frontend.center. I'm using react-snapshot so not actually hitting the SSR api yet but I'm getting server-rendered styles with full rehydration 👌

@mxstbr

mxstbr commented Apr 16, 2017

Copy link
Copy Markdown
Member

It... shouldn't break anything except users of the current SSR API, right?

Yeah but people are using that and they'll be pissed if we don't at least tell them what the new API is!

@kitten

kitten commented Apr 16, 2017

Copy link
Copy Markdown
Member

@geelen Specifically, the API in next.js doesn't let you wrap another provider around the app. Or at least I haven't figured out how to do it in a nice way.

@oscar-b

oscar-b commented Apr 20, 2017 •

Copy link
Copy Markdown
Contributor

@geelen Any chance of getting all the styles as a component as well as text? Wouldn't that make it possible to avoid dangerouslySetInnerHTML?

@oscar-b

oscar-b commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

react-helmet for instance has both toString() and toComponent() for similar reasons.

@oscar-b

oscar-b commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

Proof of concept: oscar-b@3ba3a36

I don't know Flow enough to do it properly, and there are no tests. It works for both global and local styles, by just doing {sheet.getStyleElement()} inside <head></head>

@ramesius

ramesius commented Apr 24, 2017 •

Copy link
Copy Markdown

Have these changes made it into a release on npm yet?

@ghost

ghost commented Apr 28, 2017

Copy link
Copy Markdown

@ramesius @mxstbr As per your comment here I thought that this code should be available under the rc tag on npm, but it seems that that tag isn't available? Also the 2.0.0-15 release doesn't contain this pr either it seems.

@geelen

geelen commented Apr 28, 2017

Copy link
Copy Markdown
Member Author

Sorry I got stuck on something and haven't pushed it. Gimme a sec!

@oscar-b

oscar-b commented Apr 28, 2017

Copy link
Copy Markdown
Contributor

@geelen Look at merging #736 as well first?

@geelen

geelen commented Apr 28, 2017

Copy link
Copy Markdown
Member Author

Thanks @oscar-b, I just released one without it, but will take a look at #736 now as well to see if it's ready to go

@simenbrekken

simenbrekken commented May 7, 2017 •

Copy link
Copy Markdown

I just ran into an edge-case with the previous API which I couldn't get past even after updating to RC.

The project in question is a Lerna project with common components extracted into a common package which is referenced by other packages. All packages have styled-components listed as a dependency wherein lies the problem:

Warning: Failed context type: Invalid context __styled-components-stylesheet__ of type StyleSheet supplied to styled.nav, expected instance of StyleSheet.

This is caused by lib/models/StyleSheet exporting a singleton which isn't shared across my packages.

Is there a way to explicitly provide StyleSheet via context?

@zentuit

zentuit commented Jun 21, 2017

Copy link
Copy Markdown

I'm migrating to styled-components v2 for a couple of our sites and I'm getting the same warning as @simenbrekken. We also have common components extracted out into a a couple of packages. Is there any resolution or workaround for this?

@etburke

etburke commented Aug 14, 2017

Copy link
Copy Markdown

I'm also experiencing this context type warning issue. Anyone have any further insights on this?

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.

10 participants