Skip to content

[Bug] TanStack Start 1.142.x breaks production builds with Cloudflare Workers and middleware using platform-specific imports #6185

@jillesme

Description

@jillesme

Which project does this relate to?

Start

Describe the bug

After upgrading from @tanstack/react-start@1.140.5 to @tanstack/react-start@1.142.8, production builds fail when using createMiddleware that imports cloudflare:workers (Cloudflare's platform-specific module for accessing bindings).

The middleware code is only used in .server() blocks, but the imports are being processed during the client build phase, causing Rollup to fail with unresolved imports.

Environment
@tanstack/react-start: 1.142.8 (works on 1.140.5)
@cloudflare/vite-plugin: 1.19.0
vite: 7.3.0
Platform: Cloudflare Workers

Reproduction

  1. Create a middleware file that imports cloudflare:workers:
// src/middleware/authentication.ts
import { createMiddleware } from '@tanstack/react-start'
import { getRequest } from '@tanstack/react-start/server'
import { env } from 'cloudflare:workers'  // Cloudflare-specific import
export const rateLimitMiddleware = createMiddleware({ type: 'function' })
  .server(async ({ next, context }) => {
    const { success } = await env.RATE_LIMITER.limit({ key: 'user:123' })
    if (!success) throw new Error('Too many requests')
    return next()
  })
  1. Use the middleware in a server function:
// src/lib/server-fns/example.ts
import { createServerFn } from "@tanstack/react-start"
import { rateLimitMiddleware } from "@/middleware/authentication"
export const getData = createServerFn()
  .middleware([rateLimitMiddleware])
  .handler(async () => { /* ... */ })
  1. Run pnpm run build (or equivalent)
Error Output
error during build:
[vite]: Rollup failed to resolve import "cloudflare:workers" from "src/middleware/authentication.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`

If you try to add cloudflare:workers to externals, a secondary error occurs:

node_modules/@tanstack/router-core/dist/esm/ssr/transformStreamWithRouter.js (2:9): 
"Readable" is not exported by "__vite-browser-external"
1: import { ReadableStream } from "node:stream/web";
2: import { Readable } from "node:stream";
            ^

Expected behavior

  • Middleware imports should not be analyzed/bundled for the client build
  • Server-only code paths should be properly tree-shaken from the client bundle
  • Production builds should work the same as 1.140.5

vite.config.ts

import { defineConfig } from 'vite'
import { cloudflare } from '@cloudflare/vite-plugin'
import { tanstackStart } from '@tanstack/react-start/plugin/vite'
// ... other imports
export default defineConfig({
  plugins: [
    cloudflare({ viteEnvironment: { name: 'ssr' } }),
    tanstackStart({ /* ... */ }),
    // ... other plugins
  ],
})

Workaround:

Currently the only workaround is to pin to @tanstack/react-start@1.140.5 and related packages.

Additional context:

This appears related to PR #6160 which restructured server function environment handling. The changes seem to have altered how imports in middleware files are processed during compilation, causing platform-specific imports (like cloudflare:workers) to be included in the client bundle analysis.

Your Example Website or App

https://github.com/jillesme/linkflare

Steps to Reproduce the Bug or Issue

  1. clone repo
  2. pnpm upgrade tanstack*
  3. pnpm run build

Expected behavior

It should build (per current version)

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.142.8 (works on 1.140.5)
  • OS: macOS
  • Browser: N/A (build-time error)
  • Browser Version: N/A
  • Bundler: Vite
  • Bundler Version: 7.3.0
  • Node.js: 24.11.1
  • @cloudflare/vite-plugin: 1.19.0

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions