You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I "inspect", I get the HTML content of the page and it works fine
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';
{/* 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);
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();
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.
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.
Duplicates
Latest version
Steps to reproduce 🕹
Steps:
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 */}
}
}
//
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);
})
return (
<title key="title">acacia.</title>
);
}
MyApp.propTypes = {
Component: PropTypes.elementType.isRequired,
emotionCache: PropTypes.object,
pageProps: PropTypes.object.isRequired,
}
Your environment 🌎
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: