Skip to content

Commit 7a7c530

Browse files
authored
[nextjs] Add warnings to nextjs router integration (#45778)
1 parent 02c4b06 commit 7a7c530

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('next/compat/router');

packages/mui-material-nextjs/src/v13-appRouter/appRouterV13.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import * as React from 'react';
33
import createCache, { EmotionCache, Options as OptionsOfCreateCache } from '@emotion/cache';
44
import { CacheProvider as DefaultCacheProvider } from '@emotion/react';
5-
import { useServerInsertedHTML } from 'next/navigation';
5+
import { useServerInsertedHTML } from './nextNavigation.cjs';
6+
import { useRouter as usePagesRouter } from '../nextCompatRouter.cjs';
67

78
export type AppRouterCacheProviderProps = {
89
/**
@@ -28,6 +29,18 @@ export type AppRouterCacheProviderProps = {
2829
* See https://github.com/mui/material-ui/issues/26561#issuecomment-855286153 for why it's a problem.
2930
*/
3031
export default function AppRouterCacheProvider(props: AppRouterCacheProviderProps) {
32+
if (process.env.NODE_ENV !== 'production') {
33+
// eslint-disable-next-line react-hooks/rules-of-hooks
34+
const router = usePagesRouter();
35+
if (router) {
36+
console.error(
37+
[
38+
'The App Router CacheProvider is not compatible with the Pages Router.',
39+
'Please use the Pages Router CacheProvider from `@mui/material-ui-nextjs/vx-pagesRouter` instead.',
40+
].join('\n'),
41+
);
42+
}
43+
}
3144
const { options, CacheProvider = DefaultCacheProvider, children } = props;
3245

3346
const [registry] = React.useState(() => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('next/navigation');

packages/mui-material-nextjs/src/v13-pagesRouter/pagesRouterV13App.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { CacheProvider, EmotionCache } from '@emotion/react';
33
import createEmotionCache from './createCache';
4+
import { useRouter as usePagesRouter } from '../nextCompatRouter.cjs';
45

56
export interface EmotionCacheProviderProps {
67
emotionCache?: EmotionCache;
@@ -12,5 +13,17 @@ export function AppCacheProvider({
1213
emotionCache = defaultEmotionCache,
1314
children,
1415
}: React.PropsWithChildren<EmotionCacheProviderProps>) {
16+
if (process.env.NODE_ENV !== 'production') {
17+
// eslint-disable-next-line react-hooks/rules-of-hooks
18+
const router = usePagesRouter();
19+
if (!router) {
20+
console.error(
21+
[
22+
'The Pages router CacheProvider is not compatible with the App router.',
23+
'Please use the App Router CacheProvider from `@mui/material-ui-nextjs/vx-appRouter` instead.',
24+
].join('n'),
25+
);
26+
}
27+
}
1528
return <CacheProvider value={emotionCache}>{children}</CacheProvider>;
1629
}

0 commit comments

Comments
 (0)