Skip to content

Commit

Permalink
Add _document.tsx for SSR support
Browse files Browse the repository at this point in the history
  • Loading branch information
arcatdmz committed Jul 7, 2020
1 parent bb9482e commit 1cdf9d5
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Document, { DocumentContext } from "next/document";
import { ServerStyleSheet } from "styled-components";

export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
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();
}
}
}

0 comments on commit 1cdf9d5

Please sign in to comment.