Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #1549: Modified _app and _document to ensure matching styling between client and server #1550

Merged
merged 1 commit into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = {
{
files: ['src/frontend/next/**/*.ts', 'src/frontend/next/**/*.tsx'],
plugins: ['@typescript-eslint'],
env: {
browser: true,
},
rules: {
'react/prop-types': 'off',
'react/react-in-jsx-scope': 'off',
Expand Down
10 changes: 10 additions & 0 deletions src/frontend/next/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { useEffect } from 'react';
import { AppProps } from 'next/app';
import '../styles/globals.css';

// Reference: https://github.com/mui-org/material-ui/blob/master/examples/nextjs/pages/_app.js
const App = ({ Component, pageProps }: AppProps) => {
// This hook is for ensuring the styling is sync between client and server
tonyvugithub marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles) {
jssStyles.parentElement?.removeChild(jssStyles);
}
}, []);
return <Component {...pageProps} />;
};

Expand Down
44 changes: 44 additions & 0 deletions src/frontend/next/src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Children } from 'react';
import Document, { Html, Head, Main, NextScript, DocumentContext } from 'next/document';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is _document.tsx used for? Is it similar to create-react-app's public/index.html file? Does this replace that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrispinkney Yes, you are right Chris. In Next SSR, by default we skip the definition of the surrounding document's markup. So to include extra links or script or do sth like remove the server's JSS class name, we need to create a custom document.
More about document here: https://nextjs.org/docs/advanced-features/custom-document

import { ServerStyleSheets } from '@material-ui/core/styles';

// Reference: https://github.com/mui-org/material-ui/blob/master/examples/nextjs/pages/_document.js
class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}

render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}

MyDocument.getInitialProps = async (ctx) => {
humphd marked this conversation as resolved.
Show resolved Hide resolved
// Render app and page and get the context of the page with collected side effects.
tonyvugithub marked this conversation as resolved.
Show resolved Hide resolved
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;

ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});

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()],
};
};

export default MyDocument;