makestyle server side rendering

Solutions on MaxInterview for makestyle server side rendering by the best coders in the world

showing results for - "makestyle server side rendering"
Emely
27 Apr 2017
1import React from "react";
2import Document, { Html, Head, Main, NextScript } from "next/document";
3import { ServerStyleSheets } from "@material-ui/core/styles";
4
5class MyDocument extends Document {
6  static async getInitialProps(ctx) {
7    const sheets = new ServerStyleSheets();
8    const originalRenderPage = ctx.renderPage;
9
10    ctx.renderPage = () =>
11      originalRenderPage({
12        enhanceApp: (App) => (props) => sheets.collect(<App {...props} />)
13      });
14
15    const initialProps = await Document.getInitialProps(ctx);
16
17    return {
18      ...initialProps,
19      // Styles fragment is rendered after the app and page rendering finish.
20      styles: [
21        ...React.Children.toArray(initialProps.styles),
22        sheets.getStyleElement()
23      ]
24    };
25  }
26
27  render() {
28    return (
29      <Html lang="en">
30        <Head />
31        <body>
32          <Main />
33          <NextScript />
34        </body>
35      </Html>
36    );
37  }
38}
39
40export default MyDocument;
41
similar questions
queries leading to this page
makestyle server side rendering