You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@tanstack/server-functions-plugin

Package Overview
Dependencies
Maintainers
0
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tanstack/server-functions-plugin

Modern and scalable routing for React applications

1.130.12
latest
Source
npmnpm
Version published
Weekly downloads
75K
4.57%
Maintainers
0
Weekly downloads
 
Created
Source

TanStack Server Functions Plugin

Configuration

Create a new instance of the plugin with the following options:

const TanStackServerFnsPlugin = createTanStackServerFnPlugin({
  // This is the ID (virtual module) that will be made available to look up
  // and import our server function manifest and resolve its modules.
  manifestVirtualImportId: 'tanstack:server-fn-manifest',
  client: {
    getRuntimeCode: () =>
      `import { createClientRpc } from '@tanstack/react-start/client-runtime'`,
    replacer: (opts) => `createClientRpc(${JSON.stringify(opts.functionId)})`,
  },
  ssr: {
    getRuntimeCode: () =>
      `import { createSsrRpc } from '@tanstack/react-start/ssr-runtime'`,
    replacer: (opts) => `createSsrRpc(${JSON.stringify(opts.functionId)})`,
  },
  server: {
    getRuntimeCode: () =>
      `import { createServerRpc } from '@tanstack/react-start/server-runtime'`,
    replacer: (opts) =>
      `createServerRpc(${JSON.stringify(opts.functionId)}, ${opts.fn})`,
  },
})

Then you can inject the plugin into the appropriate vite config plugin arrays:

clientVitePlugins: [TanStackServerFnsPlugin.client]
ssrVitePlugins: [TanStackServerFnsPlugin.ssr]
serverVitePlugins: [TanStackServerFnsPlugin.server]

Providing the wrapper implementations

Each runtime replacement should be implemented by your framework. Generally, on the client and SSR runtimes, you'll end up using a fetch call to call the server function your desired endpoint, like this:

function createClientRpc(functionId: string) {
  const url = `${process.env.YOUR_SERVER_BASE}/_serverFn/${functionId}`

  const fn = async (...args: any[]) => {
    const res = await fetch(url, {
      method: 'POST',
      // You'll likely want to use a better serializer here
      body: JSON.stringify(args),
    })

    return await res.json()
  }

  // You can also assign any other properties you want to the function
  // for things like form actions, or debugging
  Object.assign(fn, {
    url: url,
  })

  return fn
}

Using the manifest

In your server handler, you can import the manifest and use it to look up and dynamically import the server function you want to call.

import serverFnManifest from 'tanstack:server-fn-manifest'

export const handler = async (req: Request) => {
  const functionId = req.url.split('/').pop()
  invariant(functionId, 'No function ID provided')

  const fnInfo = serverFnManifest[functionId]
  invariant(fn, `Server function ${functionId} not found`)

  const fnModule = await fnInfo.importer()
  invariant(fnModule, `Server function ${functionId} could not be imported`)

  const args = await req.json()

  return await fnModule(...args)
}

Keywords

react

FAQs

Package last updated on 01 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts