Skip to content

Comments

Add Cloudflare Workers entrypoint and fix workerd/worker export conditions for tanstackstart-react#19461

Open
smorimoto wants to merge 3 commits intogetsentry:developfrom
smorimoto:fix/tanstackstart-worker-export-conditions
Open

Add Cloudflare Workers entrypoint and fix workerd/worker export conditions for tanstackstart-react#19461
smorimoto wants to merge 3 commits intogetsentry:developfrom
smorimoto:fix/tanstackstart-worker-export-conditions

Conversation

@smorimoto
Copy link

@smorimoto smorimoto commented Feb 21, 2026

Problem

When deploying a TanStack Start application to Cloudflare Workers, importing @sentry/tanstackstart-react causes two issues:

  1. Build failure: The @cloudflare/vite-plugin configures resolve conditions as ["workerd", "worker", "module", "browser"] for the SSR environment. Since this package only defined browser and node conditions, the resolver fell through to browser, which points to index.client.js. TanStack Start's import-protection plugin denies files matching **/*.client.* in the server environment, causing a build failure.

  2. Runtime incompatibility: Even after adding workerd/worker conditions (commit 773d0a1), they pointed to index.server.js which re-exports @sentry/node. Since @sentry/node depends on Node.js APIs not available in Cloudflare Workers, this causes runtime errors.

Related: TanStack/router#6688

Solution

  1. Add a dedicated src/cloudflare/index.ts entrypoint that provides Worker-compatible server utilities without depending on @sentry/node. This follows the same pattern used by @sentry/remix and @sentry/react-router (packages/remix/src/cloudflare/index.ts, packages/react-router/src/cloudflare/index.ts).

  2. Point workerd/worker export conditions to the cloudflare entrypoint instead of index.server.js, so bundlers targeting Workers runtimes automatically get the correct, Node-free build.

  3. Change server module imports from @sentry/node to @sentry/core in middleware.ts, wrapFetchWithSentry.ts, and utils.ts. All used APIs (startSpanManual, getActiveSpan, withActiveSpan, startSpan, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN) are available in @sentry/core. This has no effect on Node.js behavior since @sentry/node re-exports @sentry/core, but allows the cloudflare entrypoint to safely import these server modules.

  4. Add ./cloudflare subpath export for explicit imports (import { ... } from '@sentry/tanstackstart-react/cloudflare').

What the cloudflare entrypoint exports

  • Client SDK (re-export of @sentry/react + client init)
  • Common types
  • Server-side framework utilities: wrapFetchWithSentry, wrapMiddlewaresWithSentry, sentryGlobalRequestMiddleware, sentryGlobalFunctionMiddleware
  • Passthrough ErrorBoundary / withErrorBoundary

What it does NOT export

  • @sentry/node re-exports (not compatible with Workers)
  • Server-side init (Node.js-dependent; use @sentry/cloudflare for SDK initialization in Workers)
  • Vite plugin (build-time tool, not needed at runtime)

Changed files

File Change
packages/tanstackstart-react/src/server/middleware.ts Import from @sentry/core instead of @sentry/node
packages/tanstackstart-react/src/server/wrapFetchWithSentry.ts Import from @sentry/core instead of @sentry/node
packages/tanstackstart-react/src/server/utils.ts Import from @sentry/core instead of @sentry/node
packages/tanstackstart-react/src/cloudflare/index.ts New — Worker-compatible entrypoint
packages/tanstackstart-react/rollup.npm.config.mjs Add cloudflare entrypoint
packages/tanstackstart-react/package.json Update exports, add ./cloudflare subpath, update circularDepCheck

Verification

  • Build succeeds with rollup -c rollup.npm.config.mjs
  • build/esm/cloudflare/index.js and build/cjs/cloudflare/index.js are generated
  • Cloudflare build artifacts contain no references to @sentry/node
  • Existing tests unaffected (same pass/fail results before and after)
  • Tested against a TanStack Start application deployed to Cloudflare Workers — build passes and SDK initializes correctly

On Cloudflare Workers the "browser" exports condition is matched,
resolving the package entry to index.client.js.  TanStack Start's
import-protection plugin denies files matching **/*.client.* in the
server environment, causing the build to fail.

Add "workerd" and "worker" conditions before "browser" so that
bundlers targeting Cloudflare Workers (e.g. @cloudflare/vite-plugin)
resolve to index.server.js instead, avoiding the false positive.

This follows the same pattern already used by @sentry/nextjs,
@sentry/sveltekit, and @sentry/remix.
@smorimoto smorimoto force-pushed the fix/tanstackstart-worker-export-conditions branch from 773d0a1 to e23013e Compare February 21, 2026 14:45
The workerd/worker export conditions were pointing to the Node.js server
entry which depends on @sentry/node and fails in Workers runtimes.

This adds a dedicated cloudflare entrypoint (following the same pattern
as remix and react-router packages) that re-exports framework utilities
from @sentry/core instead, making the SDK compatible with Cloudflare
Workers. Server module imports are also changed from @sentry/node to
@sentry/core since all used APIs are available in both.
@smorimoto smorimoto changed the title Add workerd and worker export conditions to tanstackstart-react Add Cloudflare Workers entrypoint and fix workerd/worker export conditions for tanstackstart-react Feb 21, 2026
…ypoint

Verify that the cloudflare entrypoint exports server implementations
(not client no-op stubs) and passthrough ErrorBoundary/withErrorBoundary.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant