Skip to content

Instantly share code, notes, and snippets.

@sbin0819
Created April 8, 2023 03:39
Show Gist options
  • Save sbin0819/2048a2163544d479aa5f5109a0c54fc1 to your computer and use it in GitHub Desktop.
Save sbin0819/2048a2163544d479aa5f5109a0c54fc1 to your computer and use it in GitHub Desktop.
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';

export default class MyDocument extends Document {
  static async getInitialProps(ctx: any) {
    const { renderPage } = ctx;
    const initialProps = await Document.getInitialProps(ctx);

    const sheet = new ServerStyleSheet();

    const page = renderPage(
      (App: any) => (props: any) => sheet.collectStyles(<App {...props} />),
    );

    const styleTags = sheet.getStyleElement();

    return { ...initialProps, ...page, styleTags };
  }

  render() {
    const { styleTags } = this.props as any;
    return (
      <Html>
        <Head>{styleTags}</Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment