diff --git a/pages/_app.js b/pages/_app.js
index befe7e0..5883c70 100644
--- a/pages/_app.js
+++ b/pages/_app.js
@@ -1,11 +1,13 @@
import {useEffect, useMemo, useState} from 'react'
import {useRouter} from 'next/router'
import PropTypes from 'prop-types'
-import {createTheme, ThemeProvider, StyledEngineProvider} from '@mui/material/styles'
+import {createTheme, ThemeProvider} from '@mui/material/styles'
import CssBaseline from '@mui/material/CssBaseline'
import {Provider, useSession} from 'next-auth/client'
-
+import {CacheProvider} from '@emotion/react'
import {grey, green, red} from '@mui/material/colors'
+import createEmotionCache from '../lib/create-emotion-cache'
+
import SwitchTheme from '../components/switch-theme'
const getDesignTokens = mode => ({
@@ -39,8 +41,10 @@ const getDesignTokens = mode => ({
},
})
+const clientSideEmotionCache = createEmotionCache()
+
export default function MyApp(props) {
- const {Component, pageProps} = props
+ const {Component, emotionCache = clientSideEmotionCache, pageProps} = props
const [mode, setMode] = useState('light')
const [switchFixed, setSwitchFixed] = useState(false)
const theme = useMemo(() => createTheme(getDesignTokens(mode)), [mode])
@@ -75,7 +79,7 @@ export default function MyApp(props) {
}, [])
return (
-
+
@@ -87,7 +91,7 @@ export default function MyApp(props) {
)}
-
+
)
}
@@ -117,6 +121,7 @@ function Auth({children}) {
MyApp.propTypes = {
Component: PropTypes.elementType.isRequired,
pageProps: PropTypes.object.isRequired,
+ emotionCache: PropTypes.object,
router: PropTypes.object.isRequired
}
diff --git a/pages/_document.js b/pages/_document.js
index e2d8fee..f1fef1e 100644
--- a/pages/_document.js
+++ b/pages/_document.js
@@ -1,8 +1,9 @@
-import React from 'react'
import Document, {
Html, Head, Main, NextScript
} from 'next/document'
-import ServerStyleSheets from '@mui/styles/ServerStyleSheets'
+
+import createEmotionServer from '@emotion/server/create-instance'
+import createEmotionCache from '../lib/create-emotion-cache'
export default class MyDocument extends Document {
render() {
@@ -12,6 +13,7 @@ export default class MyDocument extends Document {
+ {this.props.emotionStyleTags}
@@ -27,17 +29,30 @@ export default class MyDocument extends Document {
}
MyDocument.getInitialProps = async ctx => {
- const sheets = new ServerStyleSheets()
const originalRenderPage = ctx.renderPage
+ const cache = createEmotionCache()
+ const {extractCriticalToChunks} = createEmotionServer(cache)
- ctx.renderPage = () => originalRenderPage({
- enhanceApp: App => props => sheets.collect()
- })
+ ctx.renderPage = () =>
+ originalRenderPage({
+ enhanceApp: App =>
+ function EnhanceApp(props) { // eslint-disable-line react/function-component-definition
+ return
+ },
+ })
const initialProps = await Document.getInitialProps(ctx)
+ const emotionStyles = extractCriticalToChunks(initialProps.html)
+ const emotionStyleTags = emotionStyles.styles.map(style => (
+
+ ))
return {
...initialProps,
- styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()]
+ emotionStyleTags
}
}