mirror of
https://github.com/TanStack/router.git
synced 2026-11-16 19:38:07 +00:00
* checkpoint * checkpoint * cleanup * checkpoint * client babel now works, runtime and rpc injection * fix: dont touch anything outside of the function * server functions now import a tsr-serverfn-split file version * checkpoint * checkpoint * fis tests * It's working!!! * move serverFnFetcher to start client * fix: no extra context, throw errors from middleware * docs: add custom link example for mantine (#3033) * ci: apply automated fixes * fix(react-router): make sure full matches are passed into route functions (#3039) * release: v1.91.3 * docs: minor typo in code-based-routing.md (#3043) * feat(start): create-start cli (#2920) * create start cli * update lockfile * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> * docs: Minor typo in middleware.md (#3042) * release: v1.92.0 * refactor(react-router): expose `scrollBehavior` on the `ScrollRestoration` component (#3053) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com> * release: v1.92.1 * fix(start): returning `null` from server functions (#3048) Co-authored-by: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> * release: v1.92.2 * fix: allow serverFn errors to also have context (#3037) * fix: allow serverFn errors to also have context * fix: allow errors, undefined from server functions * remove logs * fix: transformer and tests * ci: apply automated fixes * no logs * fix conficts --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> * release: v1.92.3 * move directive plugin to own package * fixes, tests * suggested fixes from sean * fix tests * fix: import specifier * fix: dev imports * fix: simpler config, better logging, more reliable plugin coordination * fix: update test snapshots * fix: non-split directives, clean up compilers, logging, and * fix: production server fn builds * feat(start): explicitly enable and disable route generation * fix: $fetch during dev * ci: apply automated fixes * Update comparison.md * docs: remove duplicate word (#3125) * docs: remove duplicate word * docs: correct typo in learn-the-basics.md * chore: add skipLibCheck to examples (#3134) * docs: Typo issue - getParent Does not Exists in type RouteOptions (#3131) * docs: Fix notFound import (#3133) * docs: fix dead link and typo in guide (#3136) Typo introduced in https://github.com/TanStack/router/pull/2963. * fix(react-router): separate internal and user provided history state (#3119) * release: v1.95.2 * fix(router-generator) use explicit routes export by default (#3109) Co-authored-by: Vladimir Panov <vladimir.panov@motorsport.com> * fix(start): serialize `FormData` for server function input (#3138) * release: v1.95.3 * test(react-router): check `FormData` serialization in transformer (#3139) * docs(router): missing character in code-splitting guide (#3143) * fix(create-start): readme changes and little fixes (#3142) * release: v1.95.4 * fix(start): preserve multiple values for same key in `FormData` serialization (#3140) Co-authored-by: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> * release: v1.95.5 * docs(migrate): correct typo in migration documentation (#3153) * docs(start): fix `FormData` server functions examples (#3062) Co-authored-by: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com> * docs(router): require the use of React's `createRoot` function (#3151) Co-authored-by: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com> * fix: order of injected scripts (#3158) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> * release: v1.95.6 * perf(start): reduce instantiations of server fn and middleware (again) (#3168) * perf: reduce instantiations of server fns * perf(start): reduce instantiations of middleware * release: v1.95.7 * fix imports * fix: build test --------- Co-authored-by: Youssef Benlemlih <youssefbenlemlih7@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Manuel Schiller <manuel.schiller@caligano.de> Co-authored-by: Tanner Linsley <tannerlinsley@users.noreply.github.com> Co-authored-by: AlexisPin <72150908+AlexisPin@users.noreply.github.com> Co-authored-by: timoconnellaus <tim.oconnell.australia@gmail.com> Co-authored-by: Arnaud Kleinpeter <arnaud.kleinpeter@gmail.com> Co-authored-by: Joshua Knauber <49953016+joshuaKnauber@users.noreply.github.com> Co-authored-by: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com> Co-authored-by: alakhpc <alakhpc@gmail.com> Co-authored-by: Mark Vu <markvu.work@gmail.com> Co-authored-by: Christopher Horobin <chrishorobin@hotmail.com> Co-authored-by: Muthu Kumar <muthu32@users.noreply.github.com> Co-authored-by: Jeff Hertzler <jeffhertzler@gmail.com> Co-authored-by: Philippe Serhal <philippe.serhal@gmail.com> Co-authored-by: Andrei <andreiradoidev@gmail.com> Co-authored-by: Volodymyr <vpanov.me@gmail.com> Co-authored-by: Vladimir Panov <vladimir.panov@motorsport.com> Co-authored-by: pierluigigiancola <pierluigi.giancola@gmail.com> Co-authored-by: Tom Ballinger <tom@convex.dev> Co-authored-by: Ben Durrant <ben.durrant@marketdojo.com> Co-authored-by: Lukas Frey <lukas.frey03@outlook.de> Co-authored-by: John <12599934+jorroll@users.noreply.github.com>
2.7 KiB
2.7 KiB
TanStack Server Functions Plugin
Configuration
Create a new instance of the plugin with the following options:
const TanStackServerFnsPlugin = createTanStackServerFnPlugin({
// This is the ID that will be available to look up and import
// our server function manifest and resolve its module
manifestVirtualImportId: 'tsr:server-fn-manifest',
client: {
getRuntimeCode: () =>
`import { createClientRpc } from '@tanstack/start/client-runtime'`,
replacer: (opts) => `createClientRpc(${JSON.stringify(opts.functionId)})`,
},
ssr: {
getRuntimeCode: () =>
`import { createSsrRpc } from '@tanstack/start/ssr-runtime'`,
replacer: (opts) => `createSsrRpc(${JSON.stringify(opts.functionId)})`,
},
server: {
getRuntimeCode: () =>
`import { createServerRpc } from '@tanstack/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}/_server-fn/${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 'tsr: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)