-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add CacheProvider SSR usage to docs #1396
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
Conversation
💥 No ChangesetLatest 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. |
There was a problem hiding this comment.
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.
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? |
@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 export const {
extractCritical,
renderStylesToString,
renderStylesToNodeStream,
} = createEmotionServer(cache) And then in my 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 |
Gonna close this. @souporserious if you can get a reproduction, please open an issue. |
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 usingcreateEmotionServer
and passing the samecache
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.