Next.js

Users of MUI: The MUI team now provides a dedicated package for easing up the integration with Next: @mui/material-nextjs. You can use it instead of the TSS tooling documented below. In any case you should use one (@mui/material-nextjs) or the other (tss-react/next) but not both!

This is the recommended approach.

app/layout.tsx
import { NextAppDirEmotionCacheProvider } from "tss-react/next/appDir";

export default function Layout({ children }: { children: React.ReactNode; }) {
    return (
        <html>
            {/* It's important to keep a head tag, even if it's empty */}
	    <head></head> 
	    <body>
	        <NextAppDirEmotionCacheProvider options={{ 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.

You can keep track of Emotion's developing support for ServerComponents at this link. In the interim, if you wish to utilize ServerComponents at present, you can implement the following approach.

Make MUI and TSS use different caches

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.

app/layout.tsx
import { NextAppDirEmotionCacheProvider } from "tss-react/next/appDir";
import { TssCacheProvider }ย from "tss-react";

export default function RootLayout({ children }: { children: JSX.Element }) {
    return (
        <html>
            <head></head>
	    <body>
                <NextAppDirEmotionCacheProvider options={{ "key": "mui" }}>
	            <NextAppDirEmotionCacheProvider 
	                options={{ "key": "tss" }} 
	                CacheProvider={TssCacheProvider}
	            >
			<AppMuiThemeProvider>
			    {children}
			</AppMuiThemeProvider>
		    </NextAppDirEmotionCacheProvider>
		</NextAppDirEmotionCacheProvider>
	    </body>
	</html>
    );
}

Last updated