You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
964 B
37 lines
964 B
4 years ago
|
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;
|