Skip to content

Instantly share code, notes, and snippets.

@jlozovei
Created March 14, 2023 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlozovei/86ae5e68af352f7a8198f18de1112199 to your computer and use it in GitHub Desktop.
Save jlozovei/86ae5e68af352f7a8198f18de1112199 to your computer and use it in GitHub Desktop.
Next + styled-components
import { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
export default function Document() {
const getInitialProps = async (ctx) => {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />)
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
};
} finally {
sheet.seal();
}
};
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment