mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
4dd6094639
`initialProps.styles` is a React node, but not guaranteed to be an array, so we can use a fragment to concatenate additional styles. See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/32932#issuecomment-462372319
25 lines
653 B
JavaScript
25 lines
653 B
JavaScript
import Document from 'next/document'
|
|
import { ServerStyleSheet } from 'styled-components'
|
|
|
|
export default class MyDocument extends Document {
|
|
static async getInitialProps (ctx) {
|
|
const sheet = new ServerStyleSheet()
|
|
const originalRenderPage = ctx.renderPage
|
|
|
|
try {
|
|
ctx.renderPage = () =>
|
|
originalRenderPage({
|
|
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
|
|
})
|
|
|
|
const initialProps = await Document.getInitialProps(ctx)
|
|
return {
|
|
...initialProps,
|
|
styles: <>{initialProps.styles}{sheet.getStyleElement()}</>
|
|
}
|
|
} finally {
|
|
sheet.seal()
|
|
}
|
|
}
|
|
}
|