TanStack Start
Serve markdown twins from a TanStack Start request middleware.
Register a global request middleware in src/start.ts. It runs for every request, including server routes, so .md URLs work without adding routes.
import { createMiddleware } from "@tanstack/react-start";
import { twins } from "@/lib/agentside";
export const agentsideMiddleware = createMiddleware().server(async ({ request, next }) => {
let result: Awaited<ReturnType<typeof next>> | undefined;
const response = await twins.handle(request, async () => {
result = await next();
return result.response;
});
return result ? { ...result, response } : response;
});
import { createStart } from "@tanstack/react-start";
import { agentsideMiddleware } from "@/middleware/agentside";
export const startInstance = createStart(() => ({
requestMiddleware: [agentsideMiddleware],
}));
When Agentside answers with markdown it returns the Response directly and the page never renders. Otherwise it calls next(), adds the Link and Vary headers to the page response and passes the result on.
Put agentsideMiddleware before middleware that redirects to a login page, and add your API prefix to skip so server functions are never negotiated.
import { agentside } from "@usenotra/agentside";
export const twins = agentside({
routes: {
"/": "# Acme\n\nAcme sends invoices your customers actually pay.",
},
skip: ["/api", "/_serverFn"],
});