-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Compatibility with server-side Web API runtimes: Next.js Edge Runtime, Deno, Cloudflare Workers / Pages. #2801
Comments
This is probably related to this: #2777 |
There has been some work on this here: #2589. It is targeting Cloudflare Workers, but I assume there is considerable overlap with supporting the Next.js Edge Runtime. |
@srmagura that's amazing, the Next.js Edge Runtime is actually confirmed to be built on Cloudflare Workers (at this time). So both should work here. |
I've quickly tested this with the latest version of Emotion (#2589), since the worker runtime should have the same APIs as the Edge Runtime of Verce / Next.js. I've created a repository here to quickly demonstrate the errors that showup: There's a couple of things that seem to go wrong here: Next.js is having issues with body streams, but doesn't seem to be affecting the rest of the render process.error - node_modules/next/dist/server/body-streams.js (43:0) @ eval
error - unhandledRejection: Cannot read properties of null (reading 'on') Emotion is having issues with its cache and is loading the browser bundle instead of the worker bundle, which might actually fix this issue.ReferenceError: document is not defined
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
node_modules/@emotion/cache/dist/emotion-cache.browser.esm.js (199:0)
eval
node_modules/@emotion/cache/dist/emotion-cache.browser.esm.js (199:0) Server Error
TypeError: Cannot read properties of null (reading 'registered')
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
eval
webpack-internal:///./node_modules/@emotion/styled/base/dist/emotion-styled-base.browser.esm.js (142:134)
eval
webpack-internal:///./node_modules/@emotion/react/dist/emotion-element-cbed451f.browser.esm.js (58:12) Would love to see what it requires to make Emotion plug and play for the Edge Runtime of Next.js / Vercel too! I've added a similar issue at Next.js too see if this is something they could fix with their compiler / bundler. vercel/next.js#39229 |
Actually... I'm going to release a new version with this condition in a moment.
Would be cool to get more info about this to assess where the problem is. |
It's weird though that |
Since Next.js Edge Runtime is sort of of a hybrid model, you can choose per page / endpoint which runtime you want to use, I'm not sure how their compiler handles importing the right modules, it at least seems to attempt browser modules, but worker would be even better here. That's also why I opened an issue at Next.js! |
Did you have a chance to test this out with the new release of |
@Andarist I just tested the latest version on the Next.js Ege Runtime. As we're discussing in vercel/next.js#39229, the browser bundle is still being loaded. If I manually replace the browser bundle with the worker bundle, it works without crashes, but the styles are not loading. I did this by directly editing node_modules, removing the next.js cache (removing the .next folder), and rerunning the application. If I manually replace the browser bundle with the regular ESM bundle, it actually works. See these screenshots: Using Using |
Could you tell me what HTML got flushed to the browser when using the worker bundle? |
@Andarist I actually just made this work with a little custom webpack configuration for Here's the update I just did: ItsWendell/next-edge-runtime-chakra-ui-emotion@8d6b389 With the |
Yeah, we are not really that concerned with the text content so I don't think that the hydration mismatch comes from Emotion itself. To sum it up... it feels like there is nothing actionable on our side here, right? The only problem is that this doesn't work out of the box with Next.js as they don't use the |
There are some new APIs~ coming to React in the future. In the face of those possibilities, I'm not sure if it's worth working on a custom streaming solution: https://twitter.com/joshcstory/status/1559482199388426240 Maybe we could work on a custom stream transformer, but I would be hesitant to add any code to Emotion itself (I mean our components etc) to support this. |
No plan for this? |
Tried your workaround, doesn't seem to be working anymore after upgrading Next.js to a newer version (not only 13, but also 12.x). |
The problem
Next.js recently released Edge Runtime in version 12.2 as an experimental feature, this includes doing serverside rendering on the edge in a different runtime than Node.js. It's a runtime similar to Deno and Cloudflare Workers / Pages, they're built on the v8 browser engine, for the server.
I've experimented with running the with-emotion-swc-app example with the Experimental Edge runtime enabled, which initially caused the following issue:
The compiler/bundler loads by default the .browser version of the build output of
@chakra-ui/cache
, this output doesn't have theisBrowser
checks that the regular outputs have (since it assumes it's already in the browser). This was initially fixed for me locally when I manually replaced the .browser.esm.js file with the regular .esm.js file.Proposed solution
Supporting these runtimes would greatly benefit the ecosystem and allow users of Emotion, and everything built on top of it, to use Emotion in newer runtimes and frameworks like Deno (Fresh), Next.js Edge Runtime, and Cloudflare Workers.
The text was updated successfully, but these errors were encountered: