AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack Ignores Custom Emotion Cache During SSR With @Mui/Material-Nextjs In Pages Router

When using Next.js 16 with Turbopack and the official MUI Pages Router integration, SSR renders components with the default Emotion `css` cache instead of the custom `muirtl` cache, causing missing RTL styles on initial page load. The same code works correctly with Webpack.

highConfidence 78%Next.jsAffected V16.2.x

Origin Analysis

Turbopack's server/client module graph creates duplicate instances of `@emotion/react`, causing the `EmotionCacheContext` used by `AppCacheProvider` (from `@mui/material-nextjs`) to be different from the context read by components. During SSR, the components therefore fall back to the default Emotion cache (`css`), while after hydration the client uses the correct custom cache (`muirtl`). This is a module resolution/context-identity bug specific to Turbopack, not an application code error.
1. Create a Next.js 16 project with Pages Router. 2. Install @mui/material, @mui/material-nextjs, @emotion/react, @emotion/cache, @emotion/styled, and @mui/stylis-plugin-rtl. 3. Configure an RTL Emotion cache using `createEmotionCache` from `@mui/material-nextjs/v16-pagesRouter` with key `muirtl` and `rtlPlugin`. 4. In `_document.tsx`, call `documentGetInitialProps(ctx, { emotionCache: createRtlCache() })`. 5. In `_app.tsx`, wrap the app with `<AppCacheProvider emotionCache={createRtlCache()}>`. 6. Run `next dev` (Turbopack) and load an SSR page. 7. Observe that the initial HTML contains `<style data-emotion="css ...">` instead of `<style data-emotion="muirtl ...">`, and RTL styles are missing until client-side navigation.

Fixing Code Block

Edge Case Audit

This workaround assumes a standard npm/yarn/pnpm hoisted `node_modules` structure. In monorepos or custom package managers, the path may need adjustment. Forcing an alias can cause version mismatch issues if multiple Emotion versions exist elsewhere. It may also be affected by future Next.js configuration changes. Rollback safely by removing the `turbopack.resolveAlias` block and restarting the dev server. Not a replacement for an official Turbopack fix; upgrade Next.js when a patch is released.

Ecosystem Topology