-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
React 19 + Cloudflare Adapter -> "Bug: Uncaught ReferenceError: MessageChannel is not defined" #12824
Comments
here's a workaround withastro/adapters#436 (comment)
// @ts-check
import { defineConfig } from "astro/config";
import cloudflare from "@astrojs/cloudflare";
import react from "@astrojs/react";
// https://astro.build/config
export default defineConfig({
output: "server",
adapter: cloudflare(),
integrations: [react()],
vite: {
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",
},
},
},
}); |
As it seems unlikely this will be upstream fixed by Cloudflare this probably needs to be addressed on the Astro end. |
This turns out not to be a Cloudflare issue at all, but rather an odd misconfiguration of of Vite in Astro's Cloudflare plugin. The culprit appears to be these lines: Note how the Astro's adapter here aliases I'm not sure why Astro created this alias in the first place, but this appears to me to be either obsolete workaround for some no longer present issue, or incorrect workaround for some existing issue. This alias was introduced in the initial commit of the adapter and there are no comments that I can see describing why it exists at all:
Changing this alias to point to I think it's not right that this alias exist in the first place though, so I dug a but more and tried to remove it which surfaced a new issue related to Digging a bit more, I realized that even though the plugin currently tells vite to disable any externals: It then also proceeds to configure This then results in
This configuration is invalid, as workerd/Cloudflare doesn't allow for any npm packages to be externalized during bundling. All deps must be inlined, and thus the externals should be set to EDIT2: in the original comment I forgot to mention explicitly that to get things working I also had to set externals to So the right thing to do here, is to remove the externals config copy-pasted from vue mentioned above, and allow for This then results again a correct bundle and working astro app.
EDIT: I don't believe this is a problem, the conditions resolution algorithm is documented at https://esbuild.github.io/api/#how-conditions-work and states that it's only the order in package.json that matters. Thanks to @vicb and @dario-piotrowicz for pointing this out. So to sum up my resolution recommendation:
Could someone from the Astro team look over these findings, and confirm this sounds right? I have no idea why the alias was added to the plugin in the first place, but I can see that up until React 19 this didn't cause any problems so the alias was just sitting there not causing significant issues, and it was only with React 19 introducing MessageChannel code in the |
Can't git blame help explaining the reason why certain things were added? I believe the PRs associated with them can provide some useful context |
Nope. See my links above to commits introducing this alias. |
Thanks for investigating this and pointing us to the right direction @IgorMinar. The proposal makes sense to me. I think we should try to remove the alias and allow react to resolve itself, though there were reports of that causing a different issue (withastro/adapters#436 (comment)) but maybe needs revisiting again. About the Though, another approach to fixing the |
You are right @bluwy! In my local changes I did set the externals to [] but forgot to document it! I'll update my comment. |
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
originally shared facebook/react#31827 but I think it makes sense to be an issue here as I doubt its a react issue
It doesn't happen during build but instead during the "Deploying to Cloudflare's global network" step
What's the expected result?
The error should not happen
Link to Minimal Reproducible Example
https://github.com/phase/test-astro-react19-cf-workers (thank you @phase)
Participation
The text was updated successfully, but these errors were encountered: