From ac766961c3e58e16445db217db78ab8f0f02358d Mon Sep 17 00:00:00 2001 From: Ossaid Date: Fri, 25 Sep 2026 13:01:04 +0500 Subject: [PATCH] fix(start-plugin-core): compile env-only and isomorphic fns before server fns Passing createServerOnlyFn(...) or createIsomorphicFn()... straight to createServerFn().handler() produced a broken server function. The server-only case compiled to .handler("") and crashed the whole app at startup, and the isomorphic case silently returned undefined. ServerFn ran first and replaced or cloned the handler argument, so the env-only and isomorphic transforms then edited stale or detached nodes. The same ordering left createClientOnlyFn calls inside a handler body uncompiled in the provider file, so client-only code ran on the server without its guard. Run the in-place rewrites before ServerFn and Middleware. Fixes #8446 --- .changeset/swift-radios-battle.md | 5 ++ .../server-functions/src/routes/env-only.tsx | 23 +++++++-- .../src/routes/isomorphic-fns.tsx | 22 +++++++-- .../tests/server-functions.spec.ts | 6 +++ .../src/start-compiler/compiler.ts | 4 +- .../start-plugin-core/tests/compiler.test.ts | 49 +++++++++++++++++++ 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 .changeset/swift-radios-battle.md diff --git a/.changeset/swift-radios-battle.md b/.changeset/swift-radios-battle.md new file mode 100644 index 00000000000..a2db9af32c4 --- /dev/null +++ b/.changeset/swift-radios-battle.md @@ -0,0 +1,5 @@ +--- +'@tanstack/start-plugin-core': patch +--- + +Compile `createServerOnlyFn`, `createClientOnlyFn` and `createIsomorphicFn` calls before server functions and middleware, so they are transformed when passed directly to `.handler()` or nested inside a handler body. diff --git a/e2e/react-start/server-functions/src/routes/env-only.tsx b/e2e/react-start/server-functions/src/routes/env-only.tsx index 2a938d692e4..8caf51e6b8d 100644 --- a/e2e/react-start/server-functions/src/routes/env-only.tsx +++ b/e2e/react-start/server-functions/src/routes/env-only.tsx @@ -22,6 +22,11 @@ const testOnServer = createServerFn().handler(() => { return { serverOnServer, clientOnServer } }) +// Env-only fn passed directly as the handler, see #8446 +const inlineServerOnly = createServerFn().handler( + createServerOnlyFn(() => 'inline server-only handler ran'), +) + export const Route = createFileRoute('/env-only')({ component: RouteComponent, }) @@ -30,7 +35,8 @@ function RouteComponent() { const [results, setResults] = useState>>() async function handleClick() { - const { serverOnServer, clientOnServer } = await testOnServer() + const [{ serverOnServer, clientOnServer }, inlineServerOnlyResult] = + await Promise.all([testOnServer(), inlineServerOnly()]) const clientOnClient = clientEcho('hello') let serverOnClient: string try { @@ -45,11 +51,17 @@ function RouteComponent() { clientOnServer, clientOnClient, serverOnClient, + inlineServerOnlyResult, }) } - const { serverOnServer, clientOnServer, clientOnClient, serverOnClient } = - results || {} + const { + serverOnServer, + clientOnServer, + clientOnClient, + serverOnClient, + inlineServerOnlyResult, + } = results || {} return (
@@ -73,6 +85,11 @@ function RouteComponent() {
{clientOnServer}
When we called the function on the client:
{clientOnClient}
+
+

+ inlineServerOnly +

+
{inlineServerOnlyResult}
)} diff --git a/e2e/react-start/server-functions/src/routes/isomorphic-fns.tsx b/e2e/react-start/server-functions/src/routes/isomorphic-fns.tsx index 2c7bbc4a8fa..157236a771a 100644 --- a/e2e/react-start/server-functions/src/routes/isomorphic-fns.tsx +++ b/e2e/react-start/server-functions/src/routes/isomorphic-fns.tsx @@ -16,6 +16,13 @@ const getServerEcho = createServerFn() .validator((input: string) => input) .handler(({ data }) => getEcho(data)) +// Isomorphic fn passed directly as the handler, see #8446 +const getInlineIsomorphic = createServerFn().handler( + createIsomorphicFn() + .server(() => 'server') + .client(() => 'client'), +) + export const Route = createFileRoute('/isomorphic-fns')({ component: RouteComponent, loader() { @@ -31,13 +38,15 @@ function RouteComponent() { async function handleClick() { const envOnClick = getEnv() const echo = getEcho('hello') - const [serverEnv, serverEcho] = await Promise.all([ + const [serverEnv, serverEcho, inlineIsomorphic] = await Promise.all([ getServerEnv(), getServerEcho({ data: 'hello' }), + getInlineIsomorphic(), ]) - setResults({ envOnClick, echo, serverEnv, serverEcho }) + setResults({ envOnClick, echo, serverEnv, serverEcho, inlineIsomorphic }) } - const { envOnClick, echo, serverEnv, serverEcho } = results || {} + const { envOnClick, echo, serverEnv, serverEcho, inlineIsomorphic } = + results || {} return (
)} diff --git a/e2e/react-start/server-functions/tests/server-functions.spec.ts b/e2e/react-start/server-functions/tests/server-functions.spec.ts index f87e6993301..b3560e05dcd 100644 --- a/e2e/react-start/server-functions/tests/server-functions.spec.ts +++ b/e2e/react-start/server-functions/tests/server-functions.spec.ts @@ -123,6 +123,9 @@ test('isomorphic functions can have different implementations on client and serv await expect(page.getByTestId('client-echo-result')).toContainText( 'client received hello', ) + await expect(page.getByTestId('inline-isomorphic-result')).toContainText( + 'server', + ) }) test('env-only functions can only be called on the server or client respectively', async ({ @@ -148,6 +151,9 @@ test('env-only functions can only be called on the server or client respectively await expect(page.getByTestId('client-on-client')).toContainText( 'client got: hello', ) + await expect(page.getByTestId('inline-server-only')).toContainText( + 'inline server-only handler ran', + ) }) test('Server function can return null for GET and POST calls', async ({ diff --git a/packages/start-plugin-core/src/start-compiler/compiler.ts b/packages/start-plugin-core/src/start-compiler/compiler.ts index cbb2978ec07..f37403f2705 100644 --- a/packages/start-plugin-core/src/start-compiler/compiler.ts +++ b/packages/start-plugin-core/src/start-compiler/compiler.ts @@ -212,9 +212,11 @@ const BuiltInKindHandlers: Record< // ClientOnlyJSX is handled separately via JSX traversal, not here } +// In-place rewrites run first: ServerFn and Middleware replace or clone whole +// subtrees, which would leave nested candidates pointing at detached nodes. const BuiltInKindHandlerOrder: Array< Exclude -> = ['ServerFn', 'Middleware', 'IsomorphicFn', 'ServerOnlyFn', 'ClientOnlyFn'] +> = ['IsomorphicFn', 'ServerOnlyFn', 'ClientOnlyFn', 'ServerFn', 'Middleware'] // All lookup kinds as an array for iteration with proper typing const AllBuiltInLookupKinds = Object.keys( diff --git a/packages/start-plugin-core/tests/compiler.test.ts b/packages/start-plugin-core/tests/compiler.test.ts index 94780d140b2..1ba397e4369 100644 --- a/packages/start-plugin-core/tests/compiler.test.ts +++ b/packages/start-plugin-core/tests/compiler.test.ts @@ -1266,3 +1266,52 @@ describe('re-export chain resolution', () => { expect(updatedResult!.code).not.toContain('server-only-value') }) }) + +describe('env-only and isomorphic fns nested in createServerFn().handler()', () => { + // https://github.com/TanStack/router/issues/8446 + const code = ` + import { createServerFn, createServerOnlyFn, createClientOnlyFn, createIsomorphicFn } from '@tanstack/react-start' + + export const inlineServerOnly = createServerFn().handler(createServerOnlyFn(() => 'server-only-impl')) + export const inlineIsomorphic = createServerFn().handler( + createIsomorphicFn().server(() => 'server-impl').client(() => 'client-impl'), + ) + export const nestedClientOnly = createServerFn().handler(() => { + const readWindow = createClientOnlyFn(() => window.innerWidth) + return 'nested-impl' + }) + ` + + test.each([ + { env: 'client', rpc: 'createClientRpc(' }, + { env: 'server', rpc: 'createSsrRpc(' }, + ] as const)( + '$env caller replaces every handler with an RPC stub', + async ({ env, rpc }) => { + const compiler = createFullCompiler(env) + const result = await compiler.compile({ code, id: '/test/src/test.ts' }) + + expect(result!.code.split(rpc)).toHaveLength(4) + expect(result!.code).not.toContain('-impl') + expect(result!.code).not.toContain('.handler("') + }, + ) + + test('server provider keeps the server implementation of each handler', async () => { + const compiler = createFullCompiler('server') + const result = await compiler.compile({ + code, + id: '/test/src/test.ts?tss-serverfn-split', + }) + + expect(result!.code).toContain("'server-only-impl'") + expect(result!.code).toContain("'server-impl'") + expect(result!.code).not.toContain("'client-impl'") + expect(result!.code).toContain( + 'createClientOnlyFn() functions can only be called on the client!', + ) + expect(result!.code).not.toMatch( + /create(ServerOnly|ClientOnly|Isomorphic)Fn\((?!\))/, + ) + }) +})