Server side render

This commit is contained in:
Morten Olsen
2021-08-25 23:34:06 +02:00
parent 175bdd17e7
commit a1bad1750e
2 changed files with 36 additions and 0 deletions

32
pages/_document.tsx Normal file
View File

@@ -0,0 +1,32 @@
import Document from 'next/document';
import { ServerStyleSheet } from 'styled-components';
class RootDocument extends Document {
static async getInitialProps(ctx: any) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App: any) => (props: any) =>
sheet.collectStyles(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
};
} finally {
sheet.seal();
}
}
}
export default RootDocument;