-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Supporting Emotion and The Next.js Edge Runtime #39229
Comments
Emotion maintainer here 👋 Note that we forgot to add a worker condition in I've just released a new version of this package, with the appropriate condition, could you check if this has fixed your issue? |
Thanks, @Andarist for the update! Currently, we are picking the |
@balazsorban44 I think a condition that favors Web-interoperable Runtimes (WinterCG) might even be better, since Vercel, Cloudflare and Deno are also a part of that. |
Edge runtime doesn't support DOM api like @Andarist I wonder if this behavior could changed, such as not tree shaking those conditions, and let runtime to handle it. I tested if I add those checks back, then it won't crash and Another idea is to spearate the exports into different subpath imports, like what react-dom does for |
Technically we could remove the browser-specific bundles but then we'd regress a little bit on the bundlesize. I would prefer not to do that.
I don't think this is a feasible solution for us (note that for some server-only stuff we have a completely separate package already). The idea behind the structure of our packages is that their API surface is the same, regardless of the target environment. Our packages are also often wrapped by other packages (3rd party ones) and "repackaged" - for instance, the user of Chakra UI might not even interact with Emotion directly, even though Chakra UI is built on top of Emotion. Given that - all of our consumers would have to restructure their code to accommodate for such a change. It's much easier for us and our consumers to have the same entrypoint for all targets and for the per-target code to be picked by a bundler or at runtime.
I think that it makes sense for you to support a I understand that, in the long run, Note that I don't think that you should immediately remove the support for the browser condition in this context - maybe it should stay here forever, but supporting both at the same time allows package maintainers to add more-specific bundles for workers than the ones already prepared for browsers. |
Removing the browser field also sounds a way to go, I think bundler can still optimize those conditions when emits client bundle, and do proper tree shake to keep the size aligned with "browser" bundle. |
If you suggest that Emotion should remove a browser field from its conditions then I don't think it's a viable strategy for us. It shifts the responsibility to drop the unused code to the bundler. The problem with that is that not every bundler does this out of the box. It would also limit us from introducing more complex code differences between those targets - mainly because we'd be limited to conditional statements and stuff but sometimes it's nice to swap out whole files or even dependencies. |
@Andarist would it be worth seeing/testing what the bundle size/performance impact would be to keep these checks in place for e.g. Other than that the core API of the browser/web environments, whether it's browser, workers, deno should be intentionlly the same. |
I don't think this is worth testing - it's also always subject to change. I don't see a compelling reason to degrade any aspect of the implementation to improve the compatibility with other tools when there is a somewhat clear path towards a better way of doing those things.
And the public API of Emotion is the same, regardless of the environment. |
Here's an example of a custom webpack configuration / next.config.js that actually made this work in some sense, we add resolve conditionName module.exports = async (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
reactStrictMode: true,
compiler: {
emotion: true,
},
webpack: (config, ctx) => {
if (ctx.nextRuntime === "edge") {
if (!config.resolve.conditionNames) {
config.resolve.conditionNames = ['require', 'node'];
}
if (!config.resolve.conditionNames.includes("worker")) {
config.resolve.conditionNames.push("worker");
}
}
return config;
},
experimental: {
runtime: 'experimental-edge',
},
}
return nextConfig
} |
Is there a separate issue tracking this or is this an internal conversation? Curious what will be decided on! |
Just want to point out that it used to work on an older version of Next.js, but after a certain minor version (forgot about the exact version), Emotion stops working with edge runtime again. |
This doesn't seem to be working anymore |
Just a heads up that the above solution does work on Next 12 BUT there's no |
This works ✅ for me on EDIT: It is also worth to say that |
@huozhi would you consider enabling |
Looks like we have a standard from https://runtime-keys.proposal.wintercg.org/ which recommends |
We could do that - but arguably... I agree that |
Will reach out to team to see if we can pick up the "edge-light" for edge runtime |
picking |
I believe that this can now be closed. Emotion provides a |
This closed issue has been automatically locked because it had no new activity for a month. 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
Emotion recently implemented
workers
support, which means that it should work for the Edge Runtime of Next.js. I've quickly tested this with the latest version of Emotion (emotion-js/emotion#2819), since the worker runtime should have the same APIs as the Edge Runtime of Verce / Next.js.Here's a more advanced repository to test Emotion / Chakra with Edge Runtime support:
https://github.com/ItsWendell/next-edge-runtime-chakra-ui-emotion
While a specific
worker
bundle output is available for emotion now, the edge runtime/compiler doesn't seem to use it.Emotion exports separate bundles for
workers
, e.g.*.worker.esm.js
, which might also work with the Edge Runtime.I would love to see what it requires to make Emotion plug and play for the Edge Runtime of Next.js / Vercel, this will open up the door for a lot of UI libraries like Chakra-UI to be compatible with it rendering on the edge too!
Expected Behavior
Next.js uses the worker bundle with the edge-runtime enabled.
Link to reproduction
https://github.com/ItsWendell/next-emotion-cache-edge-runtime
To Reproduce
I've opened a similar comment at emotion-js/emotion#2801
The text was updated successfully, but these errors were encountered: