-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Next.js 13: SWC Emotion Transform Plugin Breaks with root layout Server Component in app/
#41994
Comments
In the same way, I experience the same problem with @mui, which uses emotion under the hood. |
Same with |
Ditto. mui5, which depends on emotion breaks with |
Same issue with |
Maybe the people replying about different libraries here can also mention whether your reproduction uses SWC with If you are not using this config option, then actually your comments are not related to the subject of this issue. |
@karlhorky are you saying there's a correct way to make Next.JS v13 server components work with Emotion? Sorry new to this space Edit: just noticed the workaround mentioned above. Will give it a try |
I am using a vanilla Next 13.0.0 config created using
edit: here are project deps -- just noticed added some add'l modules in anticipation of using, but hit wall early with above issues (or different having same symptom), so extra modules unused).
|
That may be unrelated enough for a new issue (for MUI without this |
As far as I recall, the doc mentioned at this moment Emotion doesn't work with |
I have the same problem with React-redux. When trying to wrap
Call Stackeval
(sc_server)/node_modules/react-redux/lib/components/Context.js (8:65)
Object.(sc_server)/./node_modules/react-redux/lib/components/Context.js
file:///Users/cody/Code/lafuong-web/.next/server/app/page.js (5035:1)
__webpack_require__
file:///Users/cody/Code/lafuong-web/.next/server/webpack-runtime.js (33:43)
eval
(sc_server)/node_modules/react-redux/lib/hooks/useReduxContext.js (8:15)
Object.(sc_server)/./node_modules/react-redux/lib/hooks/useReduxContext.js
file:///Users/cody/Code/lafuong-web/.next/server/app/page.js (5167:1)
__webpack_require__
file:///Users/cody/Code/lafuong-web/.next/server/webpack-runtime.js (33:43)
eval
(sc_server)/node_modules/react-redux/lib/hooks/useSelector.js (9:23)
Object.(sc_server)/./node_modules/react-redux/lib/hooks/useSelector.js
file:///Users/cody/Code/lafuong-web/.next/server/app/page.js (5178:1)
__webpack_require__
file:///Users/cody/Code/lafuong-web/.next/server/webpack-runtime.js (33:43)
eval
(sc_server)/node_modules/react-redux/lib/index.js (22:19)``` |
Doesn't seem to work without "use client" |
There is a version that works with a root layout as a server component (does not have |
@karlhorky To what description are you referring? |
Same problem using |
Issues with different libraries workaround
WorkaroundMy workaround was creating my own custom wrapper for the providers with "use client" like this: Custom Wrapper
|
@magnuen2k I believe we should finally avoid |
@tisonkun I think the issue here is that you cannot use context etc. without specifying that the component is a client component. And react-redux etc. does not specify that. |
Overall, is it valid to say that MUI does not work with Nextjs v13? |
@iSuslov I mean it works, but yea you can't leverage app dir etc. I think soon we'll start seeing solutions. People depend on MUI and people use CSS-in-JS solutions for a lot of existing projects so a fix will come. |
Yep I'm stuck too. I can`t fully migrate to v13 and adopt it because the project depends on MUI |
So we cannot use react-spring currently with server component and Nextjs v13 ? |
if anyone is using some sort of wrapper make sure to define "use client" in frist line of script of wrapper. anything that has useSomthing in it must be in script that start with "use client" |
Same issue exists when using antd with NextJS v13 appdir |
@codyslex1a did you find any solution? I faced the same problem here.... |
I know I can't. 'use client' workaround is not doing it for me even if I truffle all of my components with it. |
Now that I've been using nextjs13appDir for a little while I realize how stupid my previous comment was 😅 'use client' is working for me. Only where it's necessary to not lose the benefits of Server components |
guys. @karlhorky tried to explain multiple times - this issue is about the compiler option in |
Adding |
In the linked example, adding the |
I've tried everything. This is so cursed |
Just want to expand on this / highlight it a little since this lowers the overhead quite a bit for people who DON'T want to disable The vast majority of my files are going to be components that use As some commenters in this thread and the "workaround" thread have mentioned, you can specify What @Systemcluster posted helped me realize that the opposite was true as well; I could leave my compiler settings intact so that my ~99% of files that are using MUI or some custom styled component only need Following other advice here about weaving the client and server components together means that everything works the way I expected after just reading the next docs: I get root layouts that are server (with |
Updates from the React Server Components Emotion ecosystem:
https://www.adebayosegun.com/blog/the-future-of-chakra-ui
|
I had to add the |
Is there any solution yet? |
Add |
### What & Why emotion-js has its own [jsx transform](https://emotion.sh/docs/typescript#emotionreact) which is being applied when `compiler.emotion` is enabled in `next.config.js`. Thanks to emotion-js team that provided an emotion-js example setup with app router [here](emotion-js/emotion#2928 (comment)), so that we can use it as test example with app router. Based on the setup, we create a test case working with emotion js but failed with error mentioned in #41994 that some client hooks appearing in server components. That is because the emotion-js jsx factory includes some client hooks. ### How For server components, css-in-js is not recommended to apply so we disabled the transform before, the emotion jsx factory is a separate config that should also not be applied in server components. So in this case we still use react jsx factory instead of the emotion-js one for server components then it won't error. The test case can also be used as an example for basic emotion-js use case with app router. Fixes #41994 Closes NEXT-1368
Thanks for the fix in #54284 @huozhi 🙌 Once this is released in the next canary version, can someone from this thread who uses Emotion check the new version and provide some proof that it's working in your app? Make sure to install cc @Zagorodnyi @dzcpy @dan-turner @Systemcluster @magnuen2k if you could test as mentioned above, that would be great! |
Will give it a look first thing tomorrow 🙏. Love this community ❤️ |
Seems to be working for me with MUI: Repo: mwskwong/mwskwong.com@f4092af |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Using the
compiler: { emotion: true }
option is throwing an error aboutReact.createContext
not being a function:StackBlitz: https://stackblitz.com/edit/vercel-next-js-nlknru?file=app%2Fpage.tsx,app%2Flayout.tsx,app%2FEmotionRootStyleRegistry.tsx,package.json
Making the root layout component into a client component does work, but this would be unfortunate:
StackBlitz: https://stackblitz.com/edit/vercel-next-js-rtrrxg?file=app%2Flayout.tsx,app%2Fpage.tsx,app%2FEmotionRootStyleRegistry.tsx,package.json
Workaround:
Disable the SWC Emotion transform plugin and use the
/** @jsxImportSource @emotion/react */
directive instead:Expected Behavior
It works to use the SWC Emotion transform plugin via
compiler: { emotion: true }
with a server component as the root layoutLink to reproduction
https://stackblitz.com/edit/vercel-next-js-nlknru?file=app%2Fpage.tsx,app%2Flayout.tsx,app%2FEmotionRootStyleRegistry.tsx,package.json
To Reproduce
[email protected]
,@emotion/[email protected]
,@emotion/[email protected]
app/
dir and the SWC Emotion Transform plugin usingcompiler: { emotion: true }, experimental: { appDir: true }
innext.config.js
/** @jsxImportSource @emotion/react */
directive to rely on the SWC Emotion transform plugin to addcss
prop supportNEXT-1368
The text was updated successfully, but these errors were encountered: