TanStackT
TanStack4mo ago
4 replies
worthy-rose

Server function middleware's aren't receiving the request object

Could someone help me out here, think I might be missing something obvious. I would expect this middleware to log out the request object so it can be used for authentication etc.

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

const logRequestMiddleware = createMiddleware({ type: 'request' }).server(async (args) => {
  const { next, request } = args;
  console.log('request:', request);
  return next();
});

export const demoNames = createServerFn()
  .middleware([logRequestMiddleware])
  .handler(() => {
    return ['Alice', 'Bob', 'Charlie'];
  });


But instead the log is always
undefined
being invoked from both route loader and useQuery:

import { createFileRoute } from '@tanstack/react-router';
import { demoNames } from '@/server/functions/demoNames';
import { useQuery } from '@tanstack/react-query';

export const Route = createFileRoute('/demo/start/api-request')({
  component: Home,
  loader: async () => {
    return {
      names: await demoNames(),
    };
  },
});

function Home() {
  const { names } = Route.useLoaderData();

  const otherNames = useQuery({
    queryKey: ['names'],
    queryFn: () => demoNames(),
  });

  return (
    <ul>
      {names.map((name) => (
        <li key={name}>{name}</li>
      ))}
    </ul>
  );
}


On version 1.132.31
Was this page helpful?