You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

90 lines
2.3 KiB

  1. import React from "react";
  2. import Document, { Head, Main, NextScript } from "next/document";
  3. import { ServerStyleSheets } from "@material-ui/styles";
  4. class MyDocument extends Document {
  5. render() {
  6. return (
  7. <html lang="en">
  8. <Head>
  9. <meta charSet="utf-8" />
  10. <meta
  11. name="viewport"
  12. content="width=device-width, initial-scale=1, shrink-to-fit=no"
  13. />
  14. <meta name="theme-color" content="#000000" />
  15. <link rel="shortcut icon" href={require("assets/img/favicons.png")} />
  16. <link
  17. rel="apple-touch-icon"
  18. sizes="76x76"
  19. href={require("assets/img/apple-icon.png")}
  20. />
  21. {/* Fonts and icons */}
  22. <link
  23. rel="stylesheet"
  24. type="text/css"
  25. href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons"
  26. />
  27. <link
  28. href="https://use.fontawesome.com/releases/v5.0.10/css/all.css"
  29. rel="stylesheet"
  30. />
  31. </Head>
  32. <body>
  33. <div id="page-transition"></div>
  34. <Main />
  35. <NextScript />
  36. </body>
  37. </html>
  38. );
  39. }
  40. }
  41. MyDocument.getInitialProps = async ctx => {
  42. // Resolution order
  43. //
  44. // On the server:
  45. // 1. app.getInitialProps
  46. // 2. page.getInitialProps
  47. // 3. document.getInitialProps
  48. // 4. app.render
  49. // 5. page.render
  50. // 6. document.render
  51. //
  52. // On the server with error:
  53. // 1. document.getInitialProps
  54. // 2. app.render
  55. // 3. page.render
  56. // 4. document.render
  57. //
  58. // On the client
  59. // 1. app.getInitialProps
  60. // 2. page.getInitialProps
  61. // 3. app.render
  62. // 4. page.render
  63. // Render app and page and get the context of the page with collected side effects.
  64. const sheets = new ServerStyleSheets();
  65. const originalRenderPage = ctx.renderPage;
  66. ctx.renderPage = () =>
  67. originalRenderPage({
  68. enhanceApp: App => props => sheets.collect(<App {...props} />)
  69. });
  70. const initialProps = await Document.getInitialProps(ctx);
  71. return {
  72. ...initialProps,
  73. // Styles fragment is rendered after the app and page rendering finish.
  74. styles: [
  75. <React.Fragment key="styles">
  76. {initialProps.styles}
  77. {sheets.getStyleElement()}
  78. </React.Fragment>
  79. ]
  80. };
  81. };
  82. export default MyDocument;