Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mnajdova committed Apr 22, 2021
1 parent 00ce87b commit 124cf3f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 18 deletions.
8 changes: 5 additions & 3 deletions docs/ssr.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ You can also use the advanced integration, it requires more work but does not ha

### On server

#### When using `@emotion/react`

```jsx
import { CacheProvider } from '@emotion/react'
import { renderToString } from 'react-dom/server'
Expand All @@ -43,7 +45,7 @@ import createCache from '@emotion/cache'

const key = 'custom'
const cache = createCache({ key })
const { extractCriticalToChunks, constructStyleTags } = createEmotionServer(cache)
const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(cache)

let element = (
<CacheProvider value={cache}>
Expand All @@ -63,7 +65,7 @@ res
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>My site</title>
${constructStyleTags({ html, styles })}
${constructStyleTagsFromChunks({ html, styles })}
</head>
<body>
<div id="root">${html}</div>
Expand All @@ -73,7 +75,7 @@ res
</html>`);
```

#### Deprecated
#### When using `@emotion/css`

```jsx
import { CacheProvider } from '@emotion/react'
Expand Down
4 changes: 2 additions & 2 deletions packages/react/__tests__/compat/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('extracted rules have correct keys when dealing with multiple caches', () =
)

let cache2 = createCache({ key: 'ssr-second-key' })
let { extractCritical: extractCriticalToChunks } = createEmotionServer(cache2)
let { extractCritical: extractCritical2 } = createEmotionServer(cache2)
let ele2 = (
<CacheProvider value={cache2}>
<div css={{ color: 'rebeccapurple' }} />
Expand All @@ -50,6 +50,6 @@ test('extracted rules have correct keys when dealing with multiple caches', () =

expect([
extractCritical1(renderToString(ele1)),
extractCriticalToChunks(renderToString(ele2))
extractCritical2(renderToString(ele2))
]).toMatchSnapshot()
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { EmotionCache } from '@emotion/utils'
import { generateStyleTag } from './utils'

const createConstructStyleTags = (
const createConstructStyleTagsFromChunks = (
cache: EmotionCache,
nonceString: string
) => (criticalData: {
Expand All @@ -23,4 +23,4 @@ const createConstructStyleTags = (
return styleTagsString
}

export default createConstructStyleTags
export default createConstructStyleTagsFromChunks
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import type { EmotionCache } from '@emotion/utils'

const extractCriticalToChunks = (cache: EmotionCache) => (html: string) => {
const createExtractCriticalToChunks = (cache: EmotionCache) => (
html: string
) => {
// parse out ids from html
// reconstruct css/rules/cache to pass
let RGX = new RegExp(`${cache.key}-([a-zA-Z0-9-_]+)`, 'gm')
Expand Down Expand Up @@ -48,4 +50,4 @@ const extractCriticalToChunks = (cache: EmotionCache) => (html: string) => {
return o
}

export default extractCriticalToChunks
export default createExtractCriticalToChunks
11 changes: 7 additions & 4 deletions packages/server/src/create-instance/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @flow
import type { EmotionCache } from '@emotion/utils'
import createExtractCritical from './extract-critical'
import createextractCriticalToChunks from './extract-critical-to-chunks'
import createExtractCriticalToChunks from './extract-critical-to-chunks'
import createRenderStylesToString from './inline'
import createRenderStylesToStream from './stream'
import createConstructStyleTags from './construct-style-tags'
import createConstructStyleTagsFromChunks from './construct-style-tags-from-chunks'
export default function(cache: EmotionCache) {
if (cache.compat !== true) {
// is this good? should we do this automatically?
Expand All @@ -14,9 +14,12 @@ export default function(cache: EmotionCache) {
const nonceString = cache.nonce !== undefined ? ` nonce="${cache.nonce}"` : ''
return {
extractCritical: createExtractCritical(cache),
extractCriticalToChunks: createextractCriticalToChunks(cache),
extractCriticalToChunks: createExtractCriticalToChunks(cache),
renderStylesToString: createRenderStylesToString(cache, nonceString),
renderStylesToNodeStream: createRenderStylesToStream(cache, nonceString),
constructStyleTags: createConstructStyleTags(cache, nonceString)
constructStyleTagsFromChunks: createConstructStyleTagsFromChunks(
cache,
nonceString
)
}
}
6 changes: 3 additions & 3 deletions packages/server/test/extract-critical-to-chunks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import React from 'react'
import { renderToString } from 'react-dom/server'
import type { Emotion } from '@emotion/css/create-instance'
import { prettifyCritical2 } from './util'
import { prettifyCriticalChunks } from './util'

let emotion = require('@emotion/css')
let reactEmotion = require('@emotion/styled')
Expand Down Expand Up @@ -67,8 +67,8 @@ describe('extractCriticalToChunks', () => {
)

test('returns static css', () => {
expect(prettifyCritical2(page1Critical)).toMatchSnapshot()
expect(prettifyCritical2(page2Critical)).toMatchSnapshot()
expect(prettifyCriticalChunks(page1Critical)).toMatchSnapshot()
expect(prettifyCriticalChunks(page2Critical)).toMatchSnapshot()
})

test('generates correct style tags using constructStyleTags', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const prettifyCritical = ({
}
}

export const prettifyCritical2 = ({
export const prettifyCriticalChunks = ({
html,
styles
}: {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/types/create-instance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export interface EmotionCriticalToChunks {

export interface EmotionServer {
extractCritical(html: string): EmotionCritical
extractCriticalToChunks(html: string): EmotionCriticalToChunks
renderStylesToString(html: string): string
renderStylesToNodeStream(): NodeJS.ReadWriteStream
constructStyleTags(html: string): EmotionCriticalToChunks
constructStyleTagsFromChunks(criticalData: EmotionCriticalToChunks): string
}

export default function createEmotionServer(cache: EmotionCache): EmotionServer

0 comments on commit 124cf3f

Please sign in to comment.