Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReact

Call server functions from TanStack Start loaders

Read a server-only file with createServerFn and use the typed result in a TanStack Start route loader.

You want a status route to read a file on the server. A route loader looks like a place to call readFile until you reach the same route through a link and the loader runs in the browser. Put the file read in a TanStack Start server function and call it from the loader.

Keep the file read in the handler

In a TanStack Start app running on Node, create release.txt at the app root and give it the contents June release ready. Then add src/release.functions.ts.

import { createServerFn } from '@tanstack/react-start'

export const getRelease = createServerFn({ method: 'GET' })
  .handler(async () => {
    const { readFile } = await import('node:fs/promises')
    const text = await readFile('release.txt', 'utf8')
    return { label: text.trim() }
  })

The handler reads the file from the server's working directory and returns a plain object with a string field. Open the file with the Node import inside the handler so the browser has no filesystem code to run.

Call it from the route loader

Create src/routes/release.tsx and let Start regenerate the route tree as usual.

import { createFileRoute } from '@tanstack/react-router'
import { getRelease } from '../release.functions'

export const Route = createFileRoute('/release')({
  loader: () => getRelease(),
  component: ReleasePage,
})

function ReleasePage() {
  const { label } = Route.useLoaderData()
  return <h1>{label}</h1>
}

Open /release and the heading reads “June release ready.” Your editor infers label as a string from the server handler through the loader. Rename the handler's return field and the type error points at the route that still reads label.

Check the navigation path

Add a router link to /release from another page. Open that page first and follow the link with the Network panel open. On a fresh navigation without preloading, you can see the server-function request and the heading update. On that navigation the browser calls a stub and receives data from the server. An initial page load can call the same function on the server during rendering. TanStack Start's server functions guide describes both call paths.

If the server reports ENOENT, check its working directory and confirm the deployed artifact contains release.txt. For this Node example, start the server from the directory containing that file. The route can type-check with a missing file, so check the heading in a deployed environment as well as locally.

For a real status page, return the fields the browser needs. If those fields depend on who's signed in, enforce that check in the server function or its middleware. A route guard controls navigation to a page, and the server function has its own request boundary.