start-middleware

Compose cross-cutting server middleware in TanStack Start with createMiddleware chaining and context.

1|Updated Jul 29, 2026
One-click install
npx skills add https://github.com/fusengine/kimi-code --skill start-middleware-fusengine
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: start-middleware
Source: https://github.com/fusengine/kimi-code/tree/main/plugins/tanstack-start-expert/skills/start-middleware
Command: npx skills add https://github.com/fusengine/kimi-code --skill start-middleware-fusengine

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Building authentication, logging, and authorization logic for TanStack Start server functions requires correctly composing middleware chains, passing typed context between client and server, and avoiding security pitfalls like trusting client-sent identifiers. This Skill provides the rules, patterns, and templates to implement middleware correctly. ## Core Features & Use Cases - Middleware Composition: Chain request and server-function middleware with the enforced method order (.middleware() → .validator() → .client() → .server()) and dependency resolution. - Context & Security Patterns: Pass typed context via next({ context }), transfer data with sendContext, and enforce the rule that shape validation is not authorization. - Ready-Made Templates: Copy-paste templates for an auth + permission-based authorization factory and client-side middleware for headers, custom fetch, and telemetry. - Use Case: You need per-function permission checks in a TanStack Start app. Use the authorization factory template to compose an authMiddleware that loads the session from a trusted cookie with a parameterized permission check, then attach it to your createServerFn handlers. ## Quick Start Ask the agent to create an auth middleware with a permission-based authorization factory for your TanStack Start server functions.

Frequently Asked Questions about start-middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create middleware in TanStack Start?▼

Use createMiddleware from @tanstack/react-start for request middleware, or createMiddleware({ type: 'function' }) for server function middleware. Chain methods in the enforced order: .middleware(), .validator(), .client(), then .server(), and always return next() to continue the chain.

How do I pass context between middleware in TanStack Start?▼

Pass an object to next({ context }) and it merges into the parent context for downstream middleware and the handler. For client-to-server transfer, opt in explicitly with next({ sendContext }), since client context is not sent by default.

What is the difference between request and server function middleware?▼

Request middleware runs on every server request including SSR and routes, while server function middleware runs only for createServerFn calls and adds .client() and .validator() phases. Request middleware cannot depend on server function middleware, but the reverse is allowed.

Is sendContext data safe to use for authorization in TanStack Start?▼

No, sendContext arrives as untrusted client input and shape validation is not authorization. Always derive the session from a server-trusted source like a cookie, then re-check membership or role before using any client-sent id as a query key or filter.

Why does my client middleware fail during SSR in TanStack Start?▼

The .client() phase also runs on the server during SSR, so browser-only APIs like localStorage or window throw errors. Guard them with a typeof window !== 'undefined' check before accessing browser-only APIs.

Where should staticFunctionMiddleware go in the middleware chain?▼

staticFunctionMiddleware from @tanstack/start-static-server-functions must always be the last middleware in the .middleware([]) array. It caches a server function's result at build time for prerendering and is experimental.