RSC: createServerFn handlers cannot use react-dom/server (blocks @react-email/render, etc.) #7500
Summary#
When rsc: { enabled: true } is passed to the tanstackStart Vite plugin, createServerFn handlers are resolved with the react-server condition. This makes it impossible to use any library that depends on react-dom/server — including @react-email/render — inside a server function, even though the code only ever runs on the server.
Any import of react-dom/server (or react-dom/server.edge) resolves to react-dom/server.react-server.js, which simply throws:
react-dom/server is not supported in React Server Components.
By contrast, a Nitro/Elysia route handler defined in a server file route (e.g. routes/api.\$.ts with server.handlers) runs without the react-server condition and the same code works fine. This suggests the constraint is specific to how createServerFn is bundled when RSC is enabled, not a fundamental limitation.
Reproduction#
Minimal repro: a createServerFn handler that dynamically imports @react-email/render and renders any React Email template.
```ts // src/lib/nodemailer.ts const sendEmail = async ({ react }: { react: JSX.Element }) => { const { render } = await import('@react-email/render'); const html = await render(react); // throws in RSC server function // ... }; ```
```ts // src/routes/_auth/register.tsx export const handleForm = createServerFn({ method: 'POST' }) .inputValidator(registerSchema) .handler(async ({ data }) => { // ...create user... await sendEmail({ react: VerificationEmail({ name, url }) }); // ❌ }); ```
```ts // vite.config.ts tanstackStart({ rsc: { enabled: true }, }), rsc(), ```
What works#
Putting the same render call inside an Elysia handler exposed via a server file route succeeds — same package, same component, same process:
```ts // src/routes/api.$.ts const app = new Elysia({ prefix: '/api' }) .get('/test-email-render', async () => { const html = await render(VerificationEmail({ name: 'Test', url: 'https://example.com' })); return { ok: true, length: html.length }; // ✅ { ok: true, length: 5690 } }); ```
What was tried#
- Dynamic import of
@react-email/render— bundler still resolves it viareact-servercondition at request time. - Aliasing
@react-email/renderto itsdist/edge/index.mjs(which usesreact-dom/server.edge) — same error, becausereact-dom's ownexportsmap mapsreact-serverfor both./serverand./server.edgeto the throwing stub. - Pinning Vite
resolve.conditionsto preferworkerd— doesn't reach into the RSC environment.
Expected behavior#
createServerFn handlers should either:
- Run in a server environment that does not apply the
react-servercondition (matching how server file route handlers work today), or - Provide a documented way to opt a server function out of the RSC bundle when it needs
react-dom/server(e.g. for transactional email rendering, PDF generation, etc.).
Email rendering with @react-email/render is a common server-side need and currently has no workable path when RSC is enabled, other than introducing an internal HTTP hop to a Nitro/Elysia route handler.
Environment#
- `@tanstack/react-start`: 1.168.14
- `@vitejs/plugin-rsc`: 0.5.26
- `react` / `react-dom`: 19.2.6
- `vite`: 8.0.14
- `@react-email/render`: 2.0.8
- Runtime: Bun (Nitro preset: `bun`)
- OS: macOS (Darwin 25.5.0)
schiller-manuel(GitHub profile, opens in a new tab) added labelsinformation needed
Additional context — this is the same class of bug Cloudflare hit in vinext (their Vite-based Next.js port): cloudflare/vinext#1237.
Their root-cause analysis matches ours exactly:
@react-email/render's workerd/edge entry dynamically importsreact-dom/server.edge, but [the framework] resolves that import under thereact-servercondition inside an App Route handler. React then resolvesreact-dom/server.edgetoreact-dom/server.react-server.js, which throws.The naive workaround (aliasing
react-dom/server.edgeto its absolute path) is not sufficient — it gets past the outer throw, butreact-dom's internalrequire('react')is still resolved with thereact-servercondition and picks up__SERVER_INTERNALS_…instead of__CLIENT_INTERNALS_…, surfacing as:TypeError: Cannot set properties of undefined (setting 'recentlyCreatedOwnerStacks') at resetOwnerStackLimit (react-dom-server.edge.development.js) at render (@react-email/render/src/edge/render.tsx)What worked for vinext#
Their actual fix was to bundle App Route handlers into the SSR env instead of the RSC env, so
react-dom/server.edgedoesn't pick up thereact-servercondition. A patched RSC dispatcher then fetches the handler from the SSR env viaimport.meta.viteRsc.loadModule("ssr", "index").I'd expect the analogous fix here is to bundle
createServerFnhandlers into the SSR environment rather than the RSC environment — they're conceptually closer to Nitro/Elysia route handlers (which already work) than to React Server Components.Caveat from vinext#
Moving handlers to SSR introduced a separate issue with
'use server'functions called from those handlers (noServerCall), tracked in cloudflare/vinext#1246. That's worth considering up-front if a similar move is made here.So the upstream fix probably lives partly in
@vitejs/plugin-rsc's env split and partly in how TanStack Start opts handlers into which env. Happy to test patches.Filed an upstream issue at the `@vitejs/plugin-rsc` level: vitejs/vite-plugin-react#1263.
Posting there because the underlying mechanism — the `react-server` condition propagating into every transitive require/import inside a handler bundled into the RSC env — lives at the plugin layer, and the same problem hits any framework that uses plugin-rsc (vinext already ran into it at cloudflare/vinext#1237).
If plugin-rsc adds a public API for routing specific handler modules through the SSR env (or a `'use ssr'`-style escape hatch), TanStack Start could adopt it for `createServerFn` handlers as the canonical fix. Otherwise, this likely needs a TanStack-Start-specific workaround.
Resolved upstream — vitejs/vite-plugin-react#1263 was closed by @hi-ogawa with:
`react-dom/server` being not allowed in react-server environment is as designed by React.
`@vitejs/plugin-rsc` provides a helper `import.meta.viteRsc.import(..., { environment: "ssr" })` and it allows importing arbitrary module between rsc and ssr environment. This may be useful for creating `react-dom/server` module in non-rsc world.
References for the React-side rationale: vercel/next.js#43810, vercel/next.js#61165 (comment).
So the user-land workaround when RSC is enabled in TanStack Start is to put any `react-dom/server`-using code (`@react-email/render`, etc.) in its own module and import it via:
```ts const { renderEmail } = await import.meta.viteRsc.import<typeof import('./email-renderer')>( './email-renderer', { environment: 'ssr' }, ); ```
Verified working with `@react-email/render` from a `createServerFn` handler.
Might still be worth a short note in the TanStack Start RSC docs — `createServerFn` looks server-shaped to users, so they reasonably expect any server-side library to work there. A pointer at `import.meta.viteRsc.import` would save the next person the chase. Happy to close this otherwise.
Sign in to join this conversation.
please provide a minimal complete reproducer project