Skip to content

Instantly share code, notes, and snippets.

@kosciolek
Created May 4, 2021 13:26
Show Gist options
  • Save kosciolek/10d3566ac56d5c33512fed052753647a to your computer and use it in GitHub Desktop.
Save kosciolek/10d3566ac56d5c33512fed052753647a to your computer and use it in GitHub Desktop.
import Document, { Html, Head, Main, NextScript } from "next/document";
import { ServerStyleSheet } from "styled-components";
class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
});
const styleTags = sheet.getStyleElement();
const initialProps = await Document.getInitialProps(ctx);
return { styleTags, ...initialProps };
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment