Add support for Cloudflare Worker environment in @emotion/cache, @emotion/styled, @emotion/react, and @emotion/server
See original GitHub issueSome discussion of the issue in the emotion Slack channel.
Current behavior:
Attempting to render a React application using emotion as guided by the documentation will result in the Cloudflare Worker immediately crashing, as described here.
This is because stream
is not present in a Worker environment.
After removing the code in @emotion/server
~which imports~ which indirectly imports stream
through through
, the Worker will no longer immediate crash on startup, but will throw an error on rendering:
ReferenceError: document is not defined
at createCache (./index.mjs:1173:38)
at handleRequest (./index.mjs:2241:19)
at Object.fetch (./index.mjs:2236:22) at line 1172, col 36
This is due to the Worker bundler targeting the browser
. Rollup (webpack untested) will optimize away isBrowser
checks (as it should always be true), which still leaves references to document
, which is undefined in the Worker environment. This issue is also described here. Hardcoding isBrowser = false
results in successful rendering.
To reproduce:
Clone this Github repository and follow the README.
Expected behavior:
Ideally, emotion
+ react
+ SSR should work within Workers.
Environment information:
react
version: 17.0.2@emotion/react
version: 11.6.0@emotion/server
version: 11.4.0@emotion/cache
version: 11.6.0@emotion/styled
version: 11.6.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:13
- Comments:23 (9 by maintainers)
Top GitHub Comments
@chasoft @pavi2410 Hi, this is a repository of samples for running Remix and emotion with cloudflare workers. Please refer to this sample to get by until this issue is resolved. https://github.com/aiji42/remix-emotion-on-cloudflare#conform-to-emotion
I hope this is helpful to you.
Update: I have a PR into preconstruct (which emotion uses to build its packages) to support “custom package exports conditionals” – including support for a new “worker” conditional that can be used for Cloudflare worker style environments.
I also have a PR to emotion (currently in draft mode) that uses this new version of precontruct to enable package exports on all the relevant packages, including the new “worker” conditional.
Finally, I have a super simple Cloudflare Worker deployed using the updated emotion packages and it works!