There is an obvious appeal to a server function you can call from anywhere. The old version of the same idea was not pleasant. You wrote an endpoint, then a client helper for that endpoint, then some shared schema to keep the two sides honest, then error handling in both places, and eventually a small pile of files whose main job was to move one value from the browser to the server and another value back again.
So when a framework lets you write the server part as a normal function and call it as a normal function, it feels like the right kind of progress.
export const getUser = createServerFn (). handler ( async () => { return db . user . findCurrent () })
Enter fullscreen mode
Exit fullscreen mode
Somewhere else:
const user = await getUser ()
Enter fullscreen mode
Exit fullscreen mode
This is much nicer than wiring an endpoint by hand. The function is typed. The caller is typed. Refactors have a path through the codebase instead of disappearing into a string URL.…