Skip to content

Commit

Permalink
Extract rather than inline styles in Global component when in compat …
Browse files Browse the repository at this point in the history
…mode (#1443)

* Extract rather than inline styles in Global component when in compat mode

* Add a changeset

* Update a comment
  • Loading branch information
emmatown committed Aug 2, 2019
1 parent 4a3b18a commit 188dc0e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 22 deletions.
1 change: 1 addition & 0 deletions .changeset/friendly-experts-jump/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "releases": [{ "name": "@emotion/core", "type": "patch" }], "dependents": [] }
1 change: 1 addition & 0 deletions .changeset/friendly-experts-jump/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Extract styles rather than inline them in compat mode with the Global component
6 changes: 5 additions & 1 deletion packages/cache/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ let createCache = (options?: Options): EmotionCache => {
} else {
// in compat mode, we put the styles on the inserted cache so
// that emotion-server can pull out the styles
// except when we don't want to cache it(just the Global component right now)
// except when we don't want to cache it which was in Global but now
// is nowhere but we don't want to do a major right now
// and just in case we're going to leave the case here
// it's also not affecting client side bundle size
// so it's really not a big deal

if (shouldCache) {
cache.inserted[name] = rules
Expand Down
16 changes: 8 additions & 8 deletions packages/core/__tests__/__snapshots__/server.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ exports[`ssr styled with keyframes 1`] = `

exports[`ssr with old api @import 1`] = `
<style data-emotion-css="1sh9nmh">
@import url('https://some-url');h1{color:hotpink;}
</style>
<style data-emotion-css="1lrxbo5">
.css-1lrxbo5{color:hotpink;}
</style>
<div class="css-1lrxbo5">
</div>
<style data-emotion-css="1sh9nmh">
@import url('https://some-url');h1{color:hotpink;}
</style>
`;

Expand Down Expand Up @@ -330,6 +330,11 @@ exports[`ssr with old api styled with keyframes 1`] = `

exports[`ssr with old api works with nonces 1`] = `
<style data-emotion-css="1r145pv"
nonce="some-nonce"
>
html{margin:0;padding:0;font-family:sans-serif;}
</style>
<style data-emotion-css="ihiui2"
nonce="some-nonce"
>
Expand All @@ -344,11 +349,6 @@ exports[`ssr with old api works with nonces 1`] = `
</style>
<div class="css-1lrxbo5">
</div>
<style data-emotion-css="1r145pv"
nonce="some-nonce"
>
html{margin:0;padding:0;font-family:sans-serif;}
</style>
`;

Expand Down
11 changes: 11 additions & 0 deletions packages/core/__tests__/compat/__snapshots__/server.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Global component extracts the styles rather than inlines it 1`] = `
Object {
"css": ".css-1lrxbo5{color:hotpink;}html{color:green;}",
"html": "<div class=\\"css-1lrxbo5\\"></div>",
"ids": Array [
"1lrxbo5",
"1vwj31w",
],
}
`;

exports[`it works 1`] = `
Object {
"css": ".css-1lrxbo5{color:hotpink;}",
Expand Down
20 changes: 17 additions & 3 deletions packages/core/__tests__/compat/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@
* @jest-environment node
* @flow
*/
import { jsx } from '@emotion/core'
import { cache } from 'emotion'
import { extractCritical } from 'emotion-server'
import { jsx, Global } from '@emotion/core'
import createEmotionServer from 'create-emotion-server'
import createCache from '@emotion/cache'
import { CacheProvider } from '@emotion/core'
import { renderToString } from 'react-dom/server'

test('it works', () => {
let cache = createCache()
let { extractCritical } = createEmotionServer(cache)
let ele = (
<CacheProvider value={cache}>
<div css={{ color: 'hotpink' }} />
</CacheProvider>
)
expect(extractCritical(renderToString(ele))).toMatchSnapshot()
})

test('Global component extracts the styles rather than inlines it', () => {
let cache = createCache()
let { extractCritical } = createEmotionServer(cache)
let ele = (
<CacheProvider value={cache}>
<div css={{ color: 'hotpink' }} />
<Global styles={{ html: { color: 'green' } }} />
</CacheProvider>
)
expect(extractCritical(renderToString(ele))).toMatchSnapshot()
})
24 changes: 14 additions & 10 deletions packages/core/src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,26 @@ class InnerGlobal extends React.Component<InnerGlobalProps> {
next = next.next
}

let shouldCache = this.props.cache.compat === true

let rules = this.props.cache.insert(
``,
{ name: serializedNames, styles: serializedStyles },
this.sheet,
false
shouldCache
)

return (
<style
{...{
[`data-emotion-${this.props.cache.key}`]: serializedNames,
dangerouslySetInnerHTML: { __html: rules },
nonce: this.props.cache.sheet.nonce
}}
/>
)
if (!shouldCache) {
return (
<style
{...{
[`data-emotion-${this.props.cache.key}`]: serializedNames,
dangerouslySetInnerHTML: { __html: rules },
nonce: this.props.cache.sheet.nonce
}}
/>
)
}
}
return null
}
Expand Down

0 comments on commit 188dc0e

Please sign in to comment.