-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Description
Question
Is createIsomorphicFn intended to work when called at module top-level?
I noticed that it returns a function instead of the expected value when called at module top-level, but works correctly when called inside a function scope (components, server functions, loaders).
Reproduction
https://github.com/mountain1009/tanstack-start-isomorphic-bug
pnpm install
pnpm dev
# Open http://localhost:5173Observed Behavior
| Call Location | Result | Status |
|---|---|---|
| Module top-level | Returns [Function] |
❓ |
| Inside Component | Returns "server" / "client" |
✅ Works |
| Inside Server Function | Returns "server" |
✅ Works |
| Inside Loader | Returns "server" |
✅ Works |
Code Example
import { createIsomorphicFn } from "@tanstack/react-start";
const getEnvironment = createIsomorphicFn()
.server(() => "server")
.client(() => "client");
// Module top-level: returns [Function] instead of "server"
const moduleLevel = getEnvironment();
console.log(typeof moduleLevel); // "function" ❓
function MyComponent() {
// Inside function: works correctly
const env = getEnvironment();
console.log(typeof env); // "string" ✅
}My Use Case
I was trying to initialize an oRPC client at module top-level:
const getORPCClient = createIsomorphicFn()
.server(() => createRouterClient(...))
.client(() => createORPCClient(...));
const client = getORPCClient(); // Returns function, not client
export const orpc = createTanstackQueryUtils(client); // FailsQuestions
- Is module top-level usage supported?
- If not, could this limitation be documented?
- Is there a recommended pattern for initializing isomorphic singletons?
Workaround
Using manual environment detection works:
function getORPCClient() {
if (typeof window === "undefined") {
return createRouterClient(...);
}
return createORPCClient(...);
}Environment
@tanstack/react-start: 1.143.6@cloudflare/vite-plugin: 1.0.0+- Vite: 7.0.0
Thank you!
Metadata
Metadata
Assignees
Labels
No labels