The middleware above runs identically in one dev and one serve — the same OPTIONS branch answers preflight requests in both. One configures Vite’s dev server with server.cors.preflightContinue: true so that Vite’s built-in cors middleware doesn’t intercept preflight before your middleware sees it. If you override server.cors in your vite.config.ts, keep preflightContinue: true. See API Routes → Dev and prod parity for details.
Or modify the response directly:
app/_middleware.ts
import{ createMiddleware }from'one'
exportdefaultcreateMiddleware(async({ request, next })=>{
Request to /blog/post/123
↓
1. app/_middleware.ts (runs first)
↓
2. app/blog/_middleware.ts (runs second)
↓
3. app/blog/post/_middleware.ts (runs third)
↓
4. Route handler executes
↓
Responses bubble back up through each middleware
Each middleware can:
Intercept: Return a response early without calling next()
Pass through: Call next() to continue the chain
Post-process: Modify the response after calling next()
Platform Support
Platform
Support
Node.js server
Full support
Vercel
Full support
Cloudflare Workers
Full support
Static export
Not applicable
React Native
Not supported (server-only)
Middlewares are server-only and run on every request. They do not run during static site generation or on native platforms.
Middleware vs Loader Redirects
Middleware redirects and loader redirects serve different purposes:
Middleware Redirects
Loader Redirects
Best for
URL rewrites, HTTPS enforcement, path-level access control
Protecting page data, auth guards for SSR pages
Runs on
Every request before the route
When loader data is fetched
Client-side nav
Not involved (middleware only runs on server requests)
Server intercepts redirect and sends metadata to client, preventing flash of protected content
Data protection
Returns early before route processes
Prevents sensitive loader data from reaching the client
For protecting SSR routes with auth, prefer loader redirects — they handle both direct page loads and client-side <Link> navigation, ensuring no unauthorized data leaks. Use middleware redirects for request-level concerns like URL rewrites, HTTPS enforcement, and API route protection.