0

After I build my project, I realized SSR for Material-ui not working on page where I used functional components.

My _document.js:

import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components'
import { ServerStyleSheets } from '@material-ui/styles';
import theme from '../lib/themes/hk-theme-light/index';

class MyDocument extends Document {
  static async getInitialProps (ctx) {
    const styledComponentsSheet = new ServerStyleSheet()
    const materialSheets = new ServerStyleSheets()
    const originalRenderPage = ctx.renderPage;

    try {
        ctx.renderPage = () => originalRenderPage({
            enhanceApp: App => props => styledComponentsSheet.collectStyles(materialSheets.collect(<App {...props} />))
          })
        const initialProps = await Document.getInitialProps(ctx)
        return {
          ...initialProps,
          styles: (
            <React.Fragment>
              {initialProps.styles}
              {materialSheets.getStyleElement()}
              {styledComponentsSheet.getStyleElement()}
            </React.Fragment>
          )
        }
      } finally {
        styledComponentsSheet.seal()
      }
  }

  render() {
    return (
      <Html lang="tr">
        <Head>
          <meta charSet="utf-8" />
          <meta
            name="theme-color"
            content={theme.palette.primary.main}
          />
          <link
            rel="stylesheet"
            href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;0,800;1,300;1,400;1,600;1,700;1,800&display=swap"
          />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

Example Page:

import { DynamicTable } from '../../components/Table'
import { Section, DefaultContainer } from '../../components/elements/widgets'
import Layout from '../../layouts/LayoutDefault'


const IhtiyacKredisi = () => {
          <Layout>
            <Section>
                <DefaultContainer maxWidth={false}>
                    <Grid container spacing={1}>
                        <Grid item sm={12}>
                            <H2 variant="title">İhtiyaç Kredisi Maliyet Tablosu ve Örnek Hesaplama</H2>
                        </Grid>
                        <DynamicTable
                            tableData={ratesTable}
                            description={rateDescription}
                        />
                    </Grid>
                </DefaultContainer>
            </Section>
        </Layout>

 }

With this code "jss-server-side" style is empty

jss-server-side

If I remove components and I add all functions and imports to page file It works well

enter image description here

I wonder what can cause this problem.

1
  • 1
    did you wrap your app with themeprovider Commented May 6, 2021 at 7:42

1 Answer 1

1

The problem was key error. In component my key attribute was append by same name key={item.name}. So on build next.js doesn't compile critical css according to this error. I set key attribute by unique key. Problem fixed.

Sign up to request clarification or add additional context in comments.

1 Comment

I can confirm this works; setting unique keys to each page components works. Why though? @BurakDuman

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.