Styles appear for maybe 50ms and disappear in the below code in this SSR app. I'm curious what could be causing that.
// This component is a child of index.tsx in the /pages folder
<Button
color="primary"
variant="outlined"
size="large"
>Test Button</Button>
After the styles disappear a plain HTML button is left.
I believe Next.js is causing this. I checked the Next.js file and have added the next/babel loader to .babelrc. Other than that I only saw this other relevant change. This is in /pages/_document.js:
MyDocument.getInitialProps = async ctx => {
const sheets = new MuiServerStyleSheets();
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: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
};
Things done to attempt to resolve
- Restart server
No change to issue.
- Force refresh Chrome 78 (CTRL + F5)
No change.
Theory
I think there is a server side problem. Client and server should be on the same machine, localhost. That would explain the correct initial result (client initial UI) then being overridden by a server update.
Update 1
Forgot to mention that I did update /pages/_app.js
too. Here's the updated portion:
class MyApp extends App {
componentDidMount() {
// Remove the server-side injected CSS.
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles && "parentElement" in jssStyles) {
(jssStyles.parentElement as HTMLElement).removeChild(jssStyles);
}
}