Skip to content
Source
Sign inSign up
TanStack/router

Server Function called from inside createClientOnlyFn results in error "Server function module export not resolved" #5381

Send to my agent
Closedulrichstark(GitHub profile, opens in a new tab) opened this issue 3 comments

Which project does this relate to?

Start

Describe the bug

Calling a server function from inside a createClientOnlyFn triggers following error:

serverFnInfo {
  functionName: 'serverFn_createServerFn_handler',
  importer: [Function: importer]
}
fnModule [Object: null prototype] [Module] { component: [Getter] }
Error: Server function module export not resolved for serverFn ID: src_routes_index_tsx--serverFn_createServerFn_handler
export const Route = createFileRoute("/")({
    component: Home,
});

const serverFn = createServerFn().handler(() => {
    console.log("Hello");
});

const clientFn = createClientOnlyFn(() => {
    serverFn();
});

function Home() {
    return <button onClick={() => clientFn()}>Hello</button>;
}

Your Example Website or App

https://github.com/ulrichstark/tanstack-start-server-fn-export-not-resolved

Steps to Reproduce the Bug or Issue

  1. Clone https://github.com/ulrichstark/tanstack-start-server-fn-export-not-resolved
  2. npm run dev
  3. click the button

Expected behavior

I expect to see "Hello" in terminal of server because I expect that I can call server functions from functions that are only available on the client.

Screenshots or Videos

No response

Platform

  • Router / Start Version: 1.132.41
  • OS: macOS
  • Browser: Chrome

Additional context

Removing the wrapping createClientOnlyFn of clientFn fixes the issue.

  1. root cause here is order of compilation, we first apply createClientOnlyFn which removes the reference to serverFn on the server and then dead code elimination kicks in. we need to rearrange the compiler plugins to make this work

  2. @schiller-manuel Is there any workaround for now?

    In my case it was mainly happening due to tanstack-devtool vite plugin. Someone else also reported it https://github.com/TanStack/devtools/issues/209

  3. @manish-baghel this is a different issue IMO, not related to this one here. are you using createClientOnlyFn?

  4. schiller-manuel(GitHub profile, opens in a new tab) added labelsneeded-for-start-stable

Sign in to join this conversation.