[labs/ssr] Lit SSR doesn't work in Cloudflare Workers
- Dominant language
- TypeScript
- Stars
- 21.8k
- Forks
- 1.1k
- Avg merge
- 18h 25m
- Merged PRs (30d)
- 2
Description
### Which package(s) are affected?
Lit Core (lit / lit-html / lit-element / reactive-element), SSR (@lit-labs/ssr)
### Description
Lit SSR doesn't work in Cloudflare Workers as they rely on the ["workerd", "worker", "browser" export conditions](https://github.com/cloudflare/workers-sdk/blob/133c0423ccb4c2b35a1dd26157ce9a24c6a743bb/packages/wrangler/src/bundle.ts#L391) for bundling with esbuild. In which case, the `lit-html` and `@lit/reactive-element` packages are bundled using the `browser` condition.
But because those two packages uses `window` in the `browser` condition (which is fair), it doesn't work in Cloudflare. We've found that using the `node` condition works since they don't seem to use actual Node-specific APIs.
Related https://github.com/withastro/astro/pull/6915. We're fixing Astro's Lit + Cloudflare compatibility.
### Reproduction
https://github.com/bluwy/cloudflare-lit-ssr-repro
1. npm install
2. npm run dev
The repo readme has some extra information of how to debug and patch it.
### Workaround
Adding the "worker" condition that's an exact copy of the "node" condition to the aforementioned two packages fixes it.
The repro has a patch script to work around it.
### Is this a regression?
No or unsure. This never worked, or I haven't tried before.
### Affected versions
`@lit-labs/ssr` - `3.1.2`, `lit` - `2.7.4`
### Browser/OS/Node environment
(I don't think these are related, but)
OS: macos 13.3
node: 16.19.1
npm: 6.19.3
Contributor guide
Assessment
This issue has not been assessed yet.