This repo is a focused demo of using React Router middleware with the Vercel runtime.
The main goal is to show how middleware can run before loaders/actions, attach request-scoped data to a type-safe context, and keep auth/session logic centralized.
- Showcase
future.v8_middlewarein React Router - Use middleware to inject auth data into route context
- Read that data in a loader without duplicating auth checks
- Demonstrate a clean setup for deploying this pattern on Vercel
- The route exports middleware in
app/routes/home.tsx. authMiddlewareinapp/middleware/auth.tsruns for matched requests.- Middleware sets a mock user on
userContext(app/context.ts). - The route loader reads
context.get(userContext)and returns it to the UI.
This simulates how you would verify a cookie/JWT and load a real user in production.
react-router.config.ts– enablesfuture.v8_middlewareand Vercel presetapp/middleware/auth.ts– middleware that injects the userapp/context.ts– typed context containerapp/routes/home.tsx– middleware + loader usageapp/components/welcome.tsx– UI showing middleware-injected user data
Install dependencies:
pnpm installStart development:
pnpm devApp runs at http://localhost:5173.
pnpm build
pnpm startThis project already includes @vercel/react-router and uses the Vercel preset.
pnpm dlx vercelOr connect the repo in the Vercel dashboard and deploy.
Middleware keeps cross-cutting concerns (auth, logging, feature flags, tracing) out of individual loaders/actions. In this demo, auth logic is written once and reused consistently wherever the middleware is attached.