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
injectGlobal leaks accross multiple requests in SSR #573
Comments
Oh so I found out flush mentioned in #491 (comment) but could not find its documentation. Is it not part of the spec or bad practice? |
Try moving the injectGlobal styles out of the component like this PR shows @azizhk |
How do you recommend we work with third party components which have their own stylesheets? |
realized that I cannot use flush along with https://github.com/zeit/next.js/pull/3821/files component.js import styled from 'react-emotion'
const WhiteDiv = styled.div`
background-color: white;
`
export default function () {
return (
<WhiteDiv>This should be white</WhiteDiv>
)
} import WhiteDiv from './component.js'
import { flush } from 'emotion'
function render () {
// lets say first request you render
const { html, ids, css } = extractCritical(renderToString(<WhiteDiv />))
// before second request you flush
flush()
// second request comes in
const { html, ids, css } = extractCritical(renderToString(<WhiteDiv />))
// now this type css will not have the proper styles because
// WhiteDiv was hoisted and styled.div was not called again.
} |
@azizhk There are two ways I think you could solve this.
// put this in a separate file and make it the first import in your app so that it runs before `@emotion/core`
import React from 'react'
import createReactContext from 'create-react-context'
React.createContext = createReactContext |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. |
emotion
version: 9.0.1react
version: 16.0.0Relevant code.
What you did:
What I did was something bad just to break and create an example.
I am picking a random number from 22, 24 and 26 and setting the
font-size
ofhtml,body
to be it.What happened:
On my first request I got the font with 22px and then my page specific style.
On my second request I got the
html, body {font-size: 24px}
appended after the first request's styles.On my third request onwards I got
html, body {font-size: 26px}
appended after the second request's styles.Reproduction:
Clone this:
https://github.com/zeit/next.js/tree/master/examples/with-emotion
and modify:
https://github.com/zeit/next.js/blob/master/examples/with-emotion/pages/index.js
to randomize font-size.
Problem description:
If I want to use a third party component in one page, I would injectGlobal it and then it would come up in other places.
Suggested solution:
Something like a wrapper around ReactDOM.renderToString
Where it only gives me the styles injected in the render of
And extractCritical automatically resets the global styles before and after the function call.
The text was updated successfully, but these errors were encountered: