import Document, { DocumentContext, DocumentInitialProps } from 'next/document'; import { ServerStyleSheet } from 'styled-components'; class StyledDocument extends Document { static async getInitialProps( context: DocumentContext, ): Promise { 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(), }); const initialProps = await Document.getInitialProps(context); return { ...initialProps, styles: ( <> {initialProps.styles} {styleSheet.getStyleElement()} ), }; } finally { styleSheet.seal(); } } } export default StyledDocument;