parent
4ad5640ab0
commit
256c732f8e
1 changed files with 36 additions and 0 deletions
@ -0,0 +1,36 @@ |
||||
import Document, { DocumentContext, DocumentInitialProps } from 'next/document'; |
||||
import { ServerStyleSheet } from 'styled-components'; |
||||
|
||||
class StyledDocument extends Document { |
||||
static async getInitialProps( |
||||
context: DocumentContext, |
||||
): Promise<DocumentInitialProps> { |
||||
const styleSheet = new ServerStyleSheet(); |
||||
const originalRenderPage = context.renderPage; |
||||
|
||||
try { |
||||
context.renderPage = () => |
||||
originalRenderPage({ |
||||
enhanceApp: (App) => (props) => |
||||
// eslint-disable-next-line react/jsx-props-no-spreading
|
||||
styleSheet.collectStyles(<App {...props} />), |
||||
}); |
||||
|
||||
const initialProps = await Document.getInitialProps(context); |
||||
|
||||
return { |
||||
...initialProps, |
||||
styles: ( |
||||
<> |
||||
{initialProps.styles} |
||||
{styleSheet.getStyleElement()} |
||||
</> |
||||
), |
||||
}; |
||||
} finally { |
||||
styleSheet.seal(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
export default StyledDocument; |
Loading…
Reference in new issue