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

Source code of all SSR/SSG page is not rendering properly #34706

Closed
2 tasks done
mxcrtgn opened this issue Oct 11, 2022 · 2 comments
Closed
2 tasks done

Source code of all SSR/SSG page is not rendering properly #34706

mxcrtgn opened this issue Oct 11, 2022 · 2 comments
Labels
status: waiting for author Issue with insufficient information

Comments

@mxcrtgn
Copy link

mxcrtgn commented Oct 11, 2022

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Steps:

  1. I visit an SSG or SSR page online like this one https://www.acaciapp.com/action/environnement/ne-pas-acheter-bouteilles-plastique/3TY0UDde3V5VZt00RB7X
  2. If I "inspect", I get the HTML content of the page and it works fine
  3. If I look at the "Source code" of the page, it only contains the code that is within _document.js and not the code of the above page (only an object with the fetched data from getStaticProps or getServerSideProps)

Current behavior 😯

For SSG & SSR pages : Source code is empty (only fetched data appears but not the HTML code of the page - here)
For standard pages : everything works perfectly (here)

Expected behavior 🤔

I would expect the source code of the page to render the HTML code of the page so that crawlers can index it, but also to enhance og:image and link sharings.

Context 🔦

I think it comes from the rendering method within _document.js but I actually don't really understand it with the emotion cache integration.

Here is my _document.js file :

import * as React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import theme from '../utility/theme';
import createEmotionCache from '../utility/createEmotionCache';

export default class MyDocument extends Document {
render() {
return (


{/* GLOBAL Head /}


{/
/}
{/
Global fonts */}

      {/* PWA */}
      <meta name='application-name' content='acacia.' />
      <meta name='apple-mobile-web-app-capable' content='yes' />
      <meta name='apple-mobile-web-app-title' content='acacia.' />
      <meta name="apple-mobile-web-app-status-bar" content="#EA907A" /> {/* this sets the color of url bar in Apple smatphones */}
      <meta name='description' content="Prendre soin de sa santé et de l'environnement" />
      <meta name='format-detection' content='telephone=no' />
      <meta name='mobile-web-app-capable' content='yes' />
      <meta name="theme-color" content="#EA907A" /> {/* this sets the color of url bar */}
      
      <link rel="apple-touch-icon" href="../public/assets/logos/icon_acaciapp_96.png" /> {/* this sets logo in Apple smatphones. */}
      <link rel='apple-touch-icon' sizes='152x152' href='../public/assets/logos/icon_acaciapp_152.png' />
      <link rel='apple-touch-icon' sizes='180x180' href='../public/assets/logos/icon_acaciapp_180.png' />
      <link rel='apple-touch-icon' sizes='167x167' href='../public/assets/logos/icon_acaciapp_167.png' />
      
      <link href='../public/assets/screens/apple_touch_startup_2048_2732.png' sizes="2048x2732" rel="apple-touch-startup-image" /> {/* start screen of the PWA */}
      <link href='../public/assets/screens/apple_touch_startup_1668_2224.png' sizes="1668x2224" rel="apple-touch-startup-image" />
      <link href='../public/assets/screens/apple_touch_startup_1536_2048.png' sizes="1536x2048" rel="apple-touch-startup-image" />
      <link href='../public/assets/screens/apple_touch_startup_1125_2436.png' sizes="1125x2436" rel="apple-touch-startup-image" />
      <link href='../public/assets/screens/apple_touch_startup_1242_2208.png' sizes="1242x2208" rel="apple-touch-startup-image" />
      <link href='../public/assets/screens/apple_touch_startup_750_1334.png' sizes="750x1334" rel="apple-touch-startup-image" />
      <link href='../public/assets/screens/apple_touch_startup_640_1136.png' sizes="640x1136" rel="apple-touch-startup-image" />
      
      <link rel="icon" key="icon" type="image/png" href="/favicon.ico" />
      <link rel='icon' type='image/png' sizes='32x32' href='../public/assets/logos/favicon_32.png' />
      <link rel='icon' type='image/png' sizes='16x16' href='../public/assets/logos/favicon_16.png' />
      <link rel="manifest" href="/manifest.json" /> {/* link manifest.json */}
      <link key="shortcutIcon" rel="shortcut icon" href="/favicon.ico" />

      {/* Inject MUI styles first to match with the prepend: true configuration. */}
      {this.props.emotionStyleTags} 
    </Head>
    <body>
      <Main />
      <NextScript />
    </body>
  </Html>
);

}
}

// getInitialProps belongs to _document (instead of _app),
// it's compatible with static-site 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

const originalRenderPage = ctx.renderPage;

// You can consider sharing the same emotion cache between all the SSR requests to speed up performance.
// However, be aware that it can have global side effects.
const cache = createEmotionCache();
const { extractCriticalToChunks } = createEmotionServer(cache);

ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) =>
function EnhanceApp(props) {
return <App emotionCache={cache} {...props} />;
},
});

const initialProps = await Document.getInitialProps(ctx);
// This is important. It prevents emotion to render invalid HTML.
// See #26561 (comment)
const emotionStyles = extractCriticalToChunks(initialProps.html);
const emotionStyleTags = emotionStyles.styles.map((style) => (
<style
data-emotion={${style.key} ${style.ids.join(' ')}}
key={style.key}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: style.css }}
/>
));

return {
...initialProps,
emotionStyleTags,
};
};

Here is my _app.js file :

// Infrastructure
import * as React from 'react';
import PropTypes from 'prop-types';
import Head from 'next/head';
import { useRouter } from "next/router"
import {useState, useEffect} from "react"

// Style
import '../styles/globals.css'
import { ThemeProvider, StyledEngineProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import theme from '../utility/theme';

// Contexts
import { AuthProvider } from '../utility/context/authContext';
import { ActionProvider } from '../utility/context/actionContext';
import { ObjectiveProvider } from '../utility/context/objectiveContext';
import { PointsProvider } from '../utility/context/pointsContext';
import { PromptProvider } from '../utility/context/promptContext';
import { IssueProvider } from '../utility/context/issueContext';

// Components
import { BackdropLoader } from '../components';

// Helpers
import { CacheProvider } from '@emotion/react';
import createEmotionCache from '../utility/createEmotionCache';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();

export default function MyApp(props) {
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
const router = useRouter()
const [loading, setLoading] = useState(false);

useEffect(() => {
const handleStart = (url) => (url !== router.asPath) && setLoading(true);
const handleComplete = (url) => (url === router.asPath) && setLoading(false);

  router.events.on('routeChangeStart', handleStart)
  router.events.on('routeChangeComplete', handleComplete)
  router.events.on('routeChangeError', handleComplete)

  return () => {
      router.events.off('routeChangeStart', handleStart)
      router.events.off('routeChangeComplete', handleComplete)
      router.events.off('routeChangeError', handleComplete)
  }

})

return (


<title key="title">acacia.</title>

  </Head>
  <StyledEngineProvider injectFirst>
    <ThemeProvider theme={theme}>
      <AuthProvider>
        <ActionProvider>
          <ObjectiveProvider>
            <PointsProvider>
              <IssueProvider>
                <PromptProvider>
                    {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
                    <CssBaseline />
                    <Component {...pageProps} />
                    <BackdropLoader
                      open={loading}
                      // onClick={handleComplete}
                    />
                </PromptProvider>
              </IssueProvider>
            </PointsProvider>
          </ObjectiveProvider>
        </ActionProvider>
      </AuthProvider>
    </ThemeProvider>
  </StyledEngineProvider>
    
</CacheProvider>

);
}

MyApp.propTypes = {
Component: PropTypes.elementType.isRequired,
emotionCache: PropTypes.object,
pageProps: PropTypes.object.isRequired,
}

Your environment 🌎

npx @mui/envinfo
 I am using Chrome
System:
    OS: macOS 11.7
  Binaries:
    Node: 17.8.0 - /usr/local/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.5.5 - /usr/local/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Edge: Not Found
    Firefox: Not Found
    Safari: 16.0
  npmPackages:
    @emotion/react: ^11.9.0 => 11.9.0 
    @emotion/styled: ^11.8.1 => 11.8.1 
    @mui/base:  5.0.0-alpha.78 
    @mui/icons-material: ^5.6.2 => 5.6.2 
    @mui/material: ^5.6.3 => 5.6.3 
    @mui/private-theming:  5.6.2 
    @mui/styled-engine:  5.6.1 
    @mui/system:  5.6.3 
    @mui/types:  7.1.3 
    @mui/utils:  5.6.1 
    @types/react:  18.0.8 
    react: 18.0.0 => 18.0.0 
    react-dom: 18.0.0 => 18.0.0 
@mxcrtgn mxcrtgn added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Oct 11, 2022
@michaldudak
Copy link
Member

This doesn't seem like a Material UI issue. Does it still happen if you remove Material UI components?
If so, it would be great if you could prepare a minimal repro on CodeSandbox or in a repo we could clone. Pasting large amounts of code in the issue itself isn't very readable and doesn't help us reproduce the issue on our end.

@michaldudak michaldudak added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Oct 11, 2022
@github-actions
Copy link

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

2 participants