-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
fix(cloudflare): use React edge renderer for compatibility with React 19 #436
base: main
Are you sure you want to change the base?
Conversation
React 19 requires MessageChannel from node:worker_threads, which isn't available in Cloudflare's workerd runtime. Switch to React's edge-optimized server renderer to avoid MessageChannel dependency.
🦋 Changeset detectedLatest commit: 86d1ac5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 10 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Can I interest @alexanderniebuhr or @bholmesdev in a review? |
This breaks for React 18? I think we could probably simply remove the alias altogether, it has this exports: "./server": {
"deno": "./server.browser.js",
"worker": "./server.browser.js",
"browser": "./server.browser.js",
"default": "./server.node.js"
}, And the cloudflare vite config will respect worker or browser conditions already. So it should load the right entrypoint. |
Removing the alias doesn't work, unfortunately. Here's what will happen. React 18:
React 19:
The correct files are loaded even without the alias (as you suggested), but something else breaks. I guess that's why the alias was added in the first place. Also, you're right that my change actually breaks React 18. 😕
We need a better fix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking, since this doesn't work for all versions, to avoid accidental merge.
Is this still happening in the latest version of the cloudflare integration? We've updated the resolve conditions recently so maybe it works again. |
Unfortunately, it's still a problem. Resolving works fine. The problem is that SSR will load |
PS: Removing the alias altogether still results in |
any temporary workaround for this? I'm getting same issue after upgraded to astro 5 |
Running into this as well as a completely new user of Astro v5.
and I am unable to preview or deploy, with the error:
None of the previously working workarounds I have found seem to have done anything.
Hopefully this can be resolved soon or if anyone else has another workaround to try 🙁 |
@ducan-ne @filiptibell As a workaround that doesn't require patching, you can add this to your Astro config: diff --git astro.config.mjs astro.config.mjs
index 9a077e5..7d30410 100644
--- astro.config.mjs
+++ astro.config.mjs
@@ -34,6 +34,13 @@ export default defineConfig({
build: {
sourcemap: true,
},
+ resolve: {
+ // Use react-dom/server.edge instead of react-dom/server.browser for React 19.
+ // Without this, MessageChannel from node:worker_threads needs to be polyfilled.
+ alias: import.meta.env.PROD && {
+ 'react-dom/server': 'react-dom/server.edge',
+ },
+ },
plugins: [
// While @sentry/astro isn't compatible with CF Pages yet, we can still use the Vite plugin to upload source maps.
// Based on https://github.com/getsentry/sentry-javascript/blob/develop/packages/astro/src/integration/index.ts Note: |
@filiptibell Are you 100% sure you're using React 18? AFAIK, the Also, according to https://react.dev/versions, the latest 18 release is 18.3.1. The version you specified does not exist (?). |
Ah yep, you're actually right. For some reason I had my |
Didnt work me , any insight ? Success: Assets published!14:01:29.160 | Error: Failed to publish your Function. Got error: Uncaught ReferenceError: MessageChannel is not defined at chunks/@astro-renderers_3VCRMvak.mjs:6534:16 in requireReactDomServer_browser_production at chunks/@astro-renderers_3VCRMvak.mjs:12531:8 in requireServer_browser at chunks/_@astro-renderers_3VCRMvak.mjs:12543:29 |
I also tried the suggested fix and no luck. How did you manage to get the assets published? |
I know it's not feasible for everyone. But I gave up on using react in my astro project and used svelte instead. I was only using react for tabler icons so the switch was quick. Solved deployment issues for me |
Import meta env doesn't work. i would use process env i forget which environment variable node_env maybe. |
I try this and it run smoothly. I hope this PR is resolved soon. resolve: {
// Use react-dom/server.edge instead of react-dom/server.browser for React 19.
// Without this, MessageChannel from node:worker_threads needs to be polyfilled.
// TODO: wait for this PR: https://github.com/withastro/adapters/pull/436#issuecomment-2525190557
alias:
process.env.NODE_ENV === 'production'
? {
'react-dom/server': 'react-dom/server.edge',
}
: undefined,
}, |
@alexanderniebuhr Do we need to add anything to preview this PR? I can help. |
!preview react-19 |
Note: Until a proper fix gets merged, you can use this workaround.
React 19 requires
MessageChannel
from node:worker_threads, which isn't available in Cloudflare's workerd runtime. Switch to React's edge-optimized server renderer to avoid the MessageChannel dependency.More background
Trying to use
useActionState
from React 19 as described here currently results inUncaught ReferenceError: MessageChannel is not defined
when workerd is involved.Rather than polyfilling
MessageChannel
and its dependencies, e.g. via unenv, I found that switching the renderer is much easier.The edge renderer probably makes more sense at the edge anyway (😄), though I don't know if the browser variant was initially chosen for a specific reason.