Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][docs] NextJS app router CSS injection order #40138

Closed
1 task done
mattsbennett opened this issue Dec 7, 2023 · 1 comment · Fixed by #39947
Closed
1 task done

[material-ui][docs] NextJS app router CSS injection order #40138

mattsbennett opened this issue Dec 7, 2023 · 1 comment · Fixed by #39947
Labels
docs Improvements or additions to the documentation nextjs package: material-ui Specific to @mui/material

Comments

@mattsbennett
Copy link

mattsbennett commented Dec 7, 2023

Duplicates

  • I have searched the existing issues

Related page

https://mui.com/material-ui/guides/next-js-app-router/#css-injection-order

Kind of issue

Unclear explanations

Issue description

I'm customizing Material UI styles via CSS modules, so I need to inject them before the MUI emotion styles as described in the doc; however, the example linked at the top of the doc (https://github.com/mui/material-ui/tree/master/examples/material-ui-nextjs-ts) uses much different code than the rest of the code samples in the doc, and it seems the prepend: true option is deprecated in favour of insertionPoint, but it isn't clear how to achieve the same effect with the latter.

I tried changing the following in EmotionCache.tsx from the linked example repo:

- const cache = createCache(options);
+ // It seems prepend is deprecated, but it's not clear how to use insertionPoint
+ // nor can either be passed as a prop to NextAppDirEmotionCacheProvider
+ const cache = createCache({ ...options, prepend: true });

- dangerouslySetInnerHTML={{ __html: styles }}
+ dangerouslySetInnerHTML={{ __html: `@layer emotion {${styles}}` }}

And this sort of works, but the injection order is only correct some of the time when reloading the page. Maybe 10% of regular reloads and 30% of hard reloads. When not injected properly, the emotion styles are not applied with @layer for some reason.

This clarification was also requested here: #37315 (comment).

Context 🔦

No response

@mattsbennett mattsbennett added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: docs-feedback Feedback from documentation page labels Dec 7, 2023
@mj12albert
Copy link
Member

@mattsbennett We'll soon replace this guide with a separate integration package for Next.js: #39947

In the mean time you can refer to the latest version of the CacheProvider component from the PR and see if it fixes your issue: https://github.com/mui/material-ui/blob/f4858d50b57e009afb8f05cbf1e4036374509bd2/packages/mui-material-nextjs/src/v13-appRouter/appRouterV13.tsx

@mj12albert mj12albert linked a pull request Dec 8, 2023 that will close this issue
1 task
@mj12albert mj12albert added package: material-ui Specific to @mui/material nextjs docs Improvements or additions to the documentation and removed support: docs-feedback Feedback from documentation page status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 8, 2023
@danilo-leal danilo-leal changed the title [docs] NextJS app router CSS injection order [material-ui][docs] NextJS app router CSS injection order Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation nextjs package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants