Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renderStylesToString doesn't work with custom emotion instances. #598

Closed
whmountains opened this issue Mar 16, 2018 · 2 comments
Closed

Comments

@whmountains
Copy link

whmountains commented Mar 16, 2018

  • emotion version: 9.0.2
  • react version: 16.2.0

I'm using react-static.

My React component:

// custom emotion instance
import styled from '../lib/react-emotion'

const Title = styled.h1`
  color: limegreen;
`
export default () => {
    <Title style={{ textAlign: 'center' }}>Welcome!</Title>
)

Server rendering code:

const html = renderStylesToString(render(<Comp />))
console.log(global.__EMOTION_CTX)
console.log(html)
return html

console.log(html) prints this: (redacted to just show the important part)

<style data-emotion-css="aoqeef-Title"></style>
<h1 class="css-aoqeef-Title ehamwg80">Welcome!</h1>

The style tag is empty. Not good.

To further debug this, I looked at emotion's context object:

{ __SECRET_EMOTION__: 
   { registered: 
      { 'css-aoqeef-Title': 'color:limegreen;text-align:center;/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozL...snip...' },
     inserted: 
      { 'aoqeef-Title': '' },
     sheet: _StyleSheet { isSpeedy: true, tags: [], ctr: 0, opts: {} },
     stylis: { [Function: F] use: [Function: da], set: [Function: ea] },
     nonce: undefined,
     key: 'css' } }

Notice that inserted.aoqeef-Title is an empty string. Something tells me it should contain css. https://github.com/emotion-js/emotion/blob/master/packages/create-emotion-server/src/inline.js#L18 seems to confirm this.

So where is the empty string coming from? I believe it comes from here: https://github.com/emotion-js/emotion/blob/master/packages/create-emotion/src/index.js#L249-L262

This snippet in particular seems to be the culprit:

current = ''
stylis(scope, styles)
caches.inserted[name] = current

I was able to make everything work by re-writing it this way:

caches.inserted[name] = stylis(scope, styles)

I'm not familiar with emotion's internals, so this may just be a symtom of a mis-configuration on my part.

@whmountains whmountains changed the title renderStylesToString returns empty style elements. renderStylesToString returns empty style tags. Mar 16, 2018
@whmountains whmountains changed the title renderStylesToString returns empty style tags. renderStylesToString doesn't work with custom emotion instances. Mar 19, 2018
@whmountains
Copy link
Author

Update: my original analysis as to the root cause was wrong. The snippet I pointed to isn't the issue after all.

The problem has something to do with using renderStylesToString with a custom emotion instance.

@whmountains
Copy link
Author

Closing in favor of #603

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 a pull request may close this issue.

1 participant