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

Conversation

tonyvugithub
Copy link
Contributor

Issue This PR Addresses

Fixed #1549

Type of Change

  • Bugfix: Change which fixes an issue
  • New Feature: Change which adds functionality
  • Documentation Update: Change which improves documentation
  • UI: Change which improves UI

Description

For those JSS that generating class names at run time like MUI, Next renders different class names on server vs client. In this PR, I have modified the _app.tsx and _document.tsx following the recommended example on MUI's website as well as how to do it with Next. More information can be found below.
References:
https://material-ui.com/guides/server-rendering/
https://github.com/mui-org/material-ui/tree/master/examples/nextjs

Checklist

  • Quality: This PR builds and passes our npm test and works locally
  • Tests: This PR includes thorough tests or an explanation of why it does not
  • Screenshots: This PR includes screenshots or GIFs of the changes made or an explanation of why it does not (if applicable)
  • Documentation: This PR includes updated/added documentation to user exposed functionality or configuration variables are added/changed or an explanation of why it does not(if applicable)

import { AppProps } from 'next/app';
import '../styles/globals.css';

const App = ({ Component, pageProps }: AppProps) => {
const AppComponent = ({ Component, pageProps }: AppProps) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the rename from App to AppComponent?

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 : I try to make everything more in React world. Everything is a component including the App. But the name does not affect any functionality

Copy link
Contributor

Choose a reason for hiding this comment

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

But to Chris' point, were not going to include a Component suffix on all our components, right? TypeScript provides this via typing, so we don't need to explicitly label it in the names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@humphd : got it. I changed it back to App now

@@ -0,0 +1,49 @@
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

src/frontend/next/src/pages/_document.tsx Show resolved Hide resolved
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

Looks good, a few things to modify.

src/frontend/next/src/pages/_app.tsx Show resolved Hide resolved
import { AppProps } from 'next/app';
import '../styles/globals.css';

const App = ({ Component, pageProps }: AppProps) => {
const AppComponent = ({ Component, pageProps }: AppProps) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

But to Chris' point, were not going to include a Component suffix on all our components, right? TypeScript provides this via typing, so we don't need to explicitly label it in the names.

src/frontend/next/src/pages/_document.tsx Show resolved Hide resolved
Copy link
Contributor

@humphd humphd left a comment

Choose a reason for hiding this comment

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

One issue with CSS loading, which can be done now in this PR, or in a follow-up if you want to file that.

R+ otherwise.

src/frontend/next/src/pages/_document.tsx Outdated Show resolved Hide resolved
src/frontend/next/src/pages/_document.tsx Outdated Show resolved Hide resolved
@tonyvugithub tonyvugithub merged commit 37097b3 into Seneca-CDOT:master Jan 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: nextjs Nextjs related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[next] - Modify _app and _document files to ensure class names matching in both client and server side
3 participants