TanStack Start's middleware feature enables executing code on both client and server before and after server-side operations, with data passing between them. The article demonstrates building a basic observability system using middleware to track server function execution with trace IDs, measuring end-to-end latency, and leveraging Node's AsyncLocalStorage to maintain context across nested server function calls. The middleware pattern allows splitting execution into client and server phases, each with before/after hooks, making it powerful for cross-cutting concerns like logging, authentication, and performance monitoring.
Table of contents
Why SSR?Prelude: Server FunctionsGetting StartedOur Middleware Use CaseOur First Server FunctionOur First MiddlewareBuilding Real MiddlewareThe ProblemGoing DeeperParting ThoughtsSort: