import{NextAppDirEmotionCacheProvider}from"tss-react/next/appDir";exportdefaultfunctionLayout({children}:{children:React.ReactNode;}){return (<html>{/* It's important to keep a head tag, even if it's empty */}<head></head><body><NextAppDirEmotionCacheProvideroptions={{ key: "css" }}>{children}</NextAppDirEmotionCacheProvider></body></html> );}
As it stands, Emotion is currently not compatible with ServerComponents, which, as a result, also makes MUI incompatible. Consequently, any component where you use TSS must be labelled with the "use client" directive.
It's important to note, however, that server-side rendering is indeed functional. The difference lies in the fact that the components will be rendered on both the backend and frontend, as opposed to being rendered solely on the backend.
If you want TSS and MUI to use different caches you can implement this approach. This is mainly usefull if you are migrating from MUI v4 using TSS and some styles don't display like they used to.
import { createEmotionSsrAdvancedApproach } from "tss-react/next/pagesDir";
import type { AppProps } from "next/app";
import { createTheme, ThemeProvider } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import Head from "next/head";
const { augmentDocumentWithEmotionCache, withAppEmotionCache } =
createEmotionSsrAdvancedApproach({ key: "css" });
export { augmentDocumentWithEmotionCache };
const theme = createTheme({
palette: {
mode: "light",
primary: {
main: "#32CD32", //Limegreen
},
},
});
function App({ Component, pageProps }: AppProps) {
return (
<>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</>
);
}
export default withAppEmotionCache(App);
pages/_document.tsx
import Document from "next/document";
import { augmentDocumentWithEmotionCache } from "./_app";
//You can also pass your custom document if you have one.
augmentDocumentWithEmotionCache(Document);
export default Document;