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

Styles are now correctly extracted from the correct cache (key-sensitive) on the server #1998

Merged
merged 1 commit into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-carpets-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/cache': patch
---

Styles are now correctly extracted from the correct cache (`key`-sensitive) on the server.
1 change: 1 addition & 0 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test:typescript": "dtslint types"
},
"dependencies": {
"@emotion/memoize": "^0.7.4",
"@emotion/sheet": "1.0.0-next.3",
"@emotion/utils": "1.0.0-next.0",
"@emotion/weak-memoize": "0.2.5",
Expand Down
13 changes: 8 additions & 5 deletions packages/cache/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
prefixer
} from 'stylis'
import weakMemoize from '@emotion/weak-memoize'
import memoize from '@emotion/memoize'
import {
compat,
removeLabel,
Expand All @@ -31,10 +32,12 @@ export type Options = {

let getServerStylisCache = isBrowser
? undefined
: weakMemoize(() => {
let cache = {}
return name => cache[name]
})
: weakMemoize(() =>
memoize(() => {
let cache = {}
return name => cache[name]
})
)

const defaultStylisPlugins = [prefixer]
let movedStyles = false
Expand Down Expand Up @@ -162,7 +165,7 @@ let createCache = (options: Options): EmotionCache => {
const stylis = styles => serialize(compile(styles), serializer)

// $FlowFixMe
let serverStylisCache = getServerStylisCache(stylisPlugins)
let serverStylisCache = getServerStylisCache(stylisPlugins)(key)
let getRules = (selector: string, serialized: SerializedStyles): string => {
let name = serialized.name
if (serverStylisCache[name] === undefined) {
Expand Down
19 changes: 19 additions & 0 deletions packages/react/__tests__/compat/__snapshots__/server.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ Object {
}
`;

exports[`extracted rules have correct keys when dealing with multiple caches 1`] = `
Array [
Object {
"css": ".ssr-first-key-1j7dx2f{color:rebeccapurple;}",
"html": "<div class=\\"ssr-first-key-1j7dx2f\\"></div>",
"ids": Array [
"1j7dx2f",
],
},
Object {
"css": ".ssr-second-key-1j7dx2f{color:rebeccapurple;}",
"html": "<div class=\\"ssr-second-key-1j7dx2f\\"></div>",
"ids": Array [
"1j7dx2f",
],
},
]
`;

exports[`it works 1`] = `
Object {
"css": ".ssr-1lrxbo5{color:hotpink;}",
Expand Down
23 changes: 23 additions & 0 deletions packages/react/__tests__/compat/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,26 @@ test('Global component extracts the styles rather than inlines it', () => {
)
expect(extractCritical(renderToString(ele))).toMatchSnapshot()
})

test('extracted rules have correct keys when dealing with multiple caches', () => {
let cache1 = createCache({ key: 'ssr-first-key' })
let { extractCritical: extractCritical1 } = createEmotionServer(cache1)
let ele1 = (
<CacheProvider value={cache1}>
<div css={{ color: 'rebeccapurple' }} />
</CacheProvider>
)

let cache2 = createCache({ key: 'ssr-second-key' })
let { extractCritical: extractCritical2 } = createEmotionServer(cache2)
let ele2 = (
<CacheProvider value={cache2}>
<div css={{ color: 'rebeccapurple' }} />
</CacheProvider>
)

expect([
extractCritical1(renderToString(ele1)),
extractCritical2(renderToString(ele2))
]).toMatchSnapshot()
})