From 5272a79dc7b1781954d978842bb5dd2f00d7f8a6 Mon Sep 17 00:00:00 2001 From: liqingwei Date: Mon, 26 Apr 2021 09:59:40 +0800 Subject: [PATCH] fix: material-ui styles & upgrade webpack --- components/container/post-container.tsx | 8 ++--- libs/server/middlewares/note.ts | 2 +- next.config.js | 4 +++ pages/_app.tsx | 26 ++++++++++---- pages/_document.tsx | 48 +++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 12 deletions(-) diff --git a/components/container/post-container.tsx b/components/container/post-container.tsx index 4e67752..8b8df06 100644 --- a/components/container/post-container.tsx +++ b/components/container/post-container.tsx @@ -5,13 +5,13 @@ import rules from 'rich-markdown-editor/dist/lib/markdown/rules' import styled from 'styled-components' import { NextSeo } from 'next-seo' +/** + * FIXME https://github.com/outline/rich-markdown-editor/pull/432/files + */ const renderToHtml = (markdown: string) => { return rules({ embeds: [] }).render(markdown).trim() } -/** - * FIXME https://github.com/outline/rich-markdown-editor/pull/432/files - */ export const PostContainer: FC<{ baseURL: string }> = ({ baseURL }) => { const { note } = NoteState.useContainer() @@ -25,7 +25,7 @@ export const PostContainer: FC<{ baseURL: string }> = ({ baseURL }) => { { [resolvedTheme, settings] ) + useEffect(() => { + // Remove the server-side injected CSS. + const jssStyles = document.querySelector('#jss-server-side') + + if (jssStyles) { + jssStyles.parentElement?.removeChild(jssStyles) + } + }, []) + return ( - - + + @@ -90,8 +102,8 @@ const AppInner = ({ Component, pageProps }: AppProps) => { - - + + ) } diff --git a/pages/_document.tsx b/pages/_document.tsx index 6bc518c..546793e 100644 --- a/pages/_document.tsx +++ b/pages/_document.tsx @@ -1,4 +1,6 @@ +import { ServerStyleSheets } from '@material-ui/core' import Document, { Html, Head, Main, NextScript } from 'next/document' +import { Children } from 'react' class MyDocument extends Document { render() { @@ -45,3 +47,49 @@ class MyDocument extends Document { } export default MyDocument + +// `getInitialProps` belongs to `_document` (instead of `_app`), +// it's compatible with server-side generation (SSG). +MyDocument.getInitialProps = async (ctx) => { + // Resolution order + // + // On the server: + // 1. app.getInitialProps + // 2. page.getInitialProps + // 3. document.getInitialProps + // 4. app.render + // 5. page.render + // 6. document.render + // + // On the server with error: + // 1. document.getInitialProps + // 2. app.render + // 3. page.render + // 4. document.render + // + // On the client + // 1. app.getInitialProps + // 2. page.getInitialProps + // 3. app.render + // 4. page.render + + // Render app and page and get the context of the page with collected side effects. + const sheets = new ServerStyleSheets() + const originalRenderPage = ctx.renderPage + + ctx.renderPage = () => + originalRenderPage({ + enhanceApp: (App) => (props) => sheets.collect(), + }) + + const initialProps = await Document.getInitialProps(ctx) + + return { + ...initialProps, + // Styles fragment is rendered after the app and page rendering finish. + styles: [ + ...Children.toArray(initialProps.styles), + sheets.getStyleElement(), + ], + } +}