Skip to content

Commit 8413984

Browse files
authored
[nextjs] Fix nonce issues (#45794)
1 parent 05118f5 commit 8413984

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import createCache from '@emotion/cache';
1+
import createCache, { Options } from '@emotion/cache';
22

33
const isBrowser = typeof document !== 'undefined';
44

55
// On the client side, Create a meta tag at the top of the <head> and set it as insertionPoint.
66
// This assures that MUI styles are loaded first.
77
// It allows developers to easily override MUI styles with other styling solutions, like CSS modules.
8-
export default function createEmotionCache() {
8+
export default function createEmotionCache(options?: Partial<Options>) {
99
let insertionPoint;
1010

1111
if (isBrowser) {
@@ -15,5 +15,5 @@ export default function createEmotionCache() {
1515
insertionPoint = emotionInsertionPoint ?? undefined;
1616
}
1717

18-
return createCache({ key: 'mui', insertionPoint });
18+
return createCache({ key: 'mui', insertionPoint, ...options });
1919
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './pagesRouterV13Document';
22
export * from './pagesRouterV13App';
3+
export { default as createEmotionCache } from './createCache';

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,17 @@ export async function documentGetInitialProps(
102102
const { styles } = extractCriticalToChunks(initialProps.html);
103103
return {
104104
...initialProps,
105-
emotionStyleTags: styles.map((style) => (
106-
<style
107-
data-emotion={`${style.key} ${style.ids.join(' ')}`}
108-
key={style.key}
109-
// eslint-disable-next-line react/no-danger
110-
dangerouslySetInnerHTML={{ __html: style.css }}
111-
/>
112-
)),
105+
emotionStyleTags: styles.map((style) =>
106+
style.css.trim() ? (
107+
<style
108+
data-emotion={`${style.key} ${style.ids.join(' ')}`}
109+
key={style.key}
110+
// eslint-disable-next-line react/no-danger
111+
dangerouslySetInnerHTML={{ __html: style.css }}
112+
nonce={cache.nonce}
113+
/>
114+
) : null,
115+
),
113116
};
114117
},
115118
},

0 commit comments

Comments
 (0)