Skip to content
Source
Sign inSign up
TanStack/router

RSC: createServerFn handlers cannot use react-dom/server (blocks @react-email/render, etc.) #7500

Openramonmalcolm10(GitHub profile, opens in a new tab) opened this issue 4 comments

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 via react-server condition at request time.
  • Aliasing @react-email/render to its dist/edge/index.mjs (which uses react-dom/server.edge) — same error, because react-dom's own exports map maps react-server for both ./server and ./server.edge to the throwing stub.
  • Pinning Vite resolve.conditions to prefer workerd — doesn't reach into the RSC environment.

Expected behavior

createServerFn handlers should either:

  1. Run in a server environment that does not apply the react-server condition (matching how server file route handlers work today), or
  2. 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)
  1. please provide a minimal complete reproducer project

  2. schiller-manuel(GitHub profile, opens in a new tab) added labelsinformation needed

  3. 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 imports react-dom/server.edge, but [the framework] resolves that import under the react-server condition inside an App Route handler. React then resolves react-dom/server.edge to react-dom/server.react-server.js, which throws.

    The naive workaround (aliasing react-dom/server.edge to its absolute path) is not sufficient — it gets past the outer throw, but react-dom's internal require('react') is still resolved with the react-server condition 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.edge doesn't pick up the react-server condition. A patched RSC dispatcher then fetches the handler from the SSR env via import.meta.viteRsc.loadModule("ssr", "index").

    I'd expect the analogous fix here is to bundle createServerFn handlers 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.

  4. 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.

  5. 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.