Skip to content

Instantly share code, notes, and snippets.

@manakuro
Created April 17, 2020 01:20
Show Gist options
  • Save manakuro/26d96d5f9b8ceae845279acac49b31d2 to your computer and use it in GitHub Desktop.
Save manakuro/26d96d5f9b8ceae845279acac49b31d2 to your computer and use it in GitHub Desktop.
import React from 'react'
import NextDocument from 'next/document'
import { ServerStyleSheet } from 'styled-components'
export default class Document extends NextDocument {
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 NextDocument.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
}
} finally {
sheet.seal()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment