Skip to content

Conversation

souporserious
Copy link

What:
Updated docs about using CacheProvider with SSR.

Why:
I ran into issues when using CacheProvider and SSR specifically with Gatsby. After chatting with @tkh44 on Slack we came to realize it was the fact that I didn't use my own server implementation using createEmotionServer and passing the same cache through.

How:
Added documentation describing what to be aware of when using CacheProvider + SSR. I'm happy to expand on this with a code snippet or more information if it seems like it would be more helpful.

@changeset-bot
Copy link

changeset-bot bot commented Jun 11, 2019

💥 No Changeset

Latest commit: 28a85c8

Merging this PR will not cause any packages to be released. If these changes should not cause updates to packages in this repo, this is fine 🙂

If these changes should be published to npm, you need to add a changeset.

Click here to learn what changesets are, and how to add one.


## Using CacheProvider with Server-Side-Rendering

Emotion works with SSR out of the box. However, when implementing `CacheProvider`, we must also create our own server implementation using [`createEmotionServer`](https://emotion.sh/docs/create-emotion-server) and pass it the same `cache` to ensure styles are created and hydrated properly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This README has to be tweaked too as it mentions only one reason to use this API, but it also should mentioned what you have discovered here.

@emmatown
Copy link
Member

This shouldn’t be true and if it is then there’s a bug in emotion or your code. Do you have a reproduction and is this with next by any chance?

@souporserious
Copy link
Author

@mitchellhamilton my set up is pretty specific right now with Gatsby, but I'll try and get a small repo together to showcase it. It seems to make sense why I need it my case, but maybe I'm just doing something wrong 😅.

I create my own server using my cache from createCache:

export const {
  extractCritical,
  renderStylesToString,
  renderStylesToNodeStream,
} = createEmotionServer(cache)

And then in my gatsby-ssr file I import and use renderStylesToString:

import * as React from 'react'
import { renderToString } from 'react-dom/server'

import { ThemeWrapper } from './src/components/Theme'

import { renderStylesToString } from '../packages/server'

export const replaceRenderer = ({
  bodyComponent,
  replaceBodyHTMLString,
  setHeadComponents,
}) => {
  replaceBodyHTMLString(
    renderStylesToString(
      renderToString(<ThemeWrapper>{bodyComponent}</ThemeWrapper>)
    )
  )
}

I also have gatsby-browser setup as well:

import { ThemeWrapper } from './src/components/Theme'

export const wrapRootElement = ({ element }) => (
  <ThemeWrapper>{element}</ThemeWrapper>
)

Should I not have to do this? 🤔 If I don't use createEmotionServer it won't use my custom cache options that I set and it falls back to the default settings.

@emmatown
Copy link
Member

emmatown commented Aug 2, 2019

Gonna close this. @souporserious if you can get a reproduction, please open an issue.

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.

4 participants