1234567891011121314151617181920212223242526272829303132333435363738394041 |
- 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(<App {...props} />);
- },
- });
- const initialProps = await Document.getInitialProps(context);
- return {
- ...initialProps,
- styles: [React.Children.toArray(initialProps.styles), sheet.getStyleElement()],
- };
- } finally {
- sheet.seal();
- }
- };
- render() {
- return (
- <Html lang="en">
- <Head />
- <body>
- <Main />
- <NextScript />
- </body>
- </Html>
- );
- }
- }
|