import Document, { DocumentContext, Head, Html, Main, NextScript } from 'next/document'; import React from 'react'; import { ServerStyleSheet } from 'styled-components'; // https://github.com/vercel/next.js/blob/master/examples/with-styled-components/pages/_document.js export default class CustomDocument extends Document { static getInitialProps = async (context: DocumentContext) => { const sheet = new ServerStyleSheet(); const originalRenderPage = context.renderPage; try { context.renderPage = () => originalRenderPage({ enhanceApp: (App) => (props) => { return sheet.collectStyles(); }, }); const initialProps = await Document.getInitialProps(context); return { ...initialProps, styles: [React.Children.toArray(initialProps.styles), sheet.getStyleElement()], }; } finally { sheet.seal(); } }; render() { return (
); } }