Middleware
Middleware in WebJs lets you intercept requests before they reach your pages, API routes, or server actions. Use it for authentication, logging, rate limiting, CORS, header injection, or any cross-cutting concern. WebJs supports two levels of middleware: a single root middleware and per-segment middleware scoped to subtrees of your route hierarchy.
Root Middleware
Place a middleware.ts at the root of your project (next to app/, not inside it). This middleware runs on every app request before WebJs routes it to a page, API route, or server action. Any of middleware.ts, .js, .mts, or .mjs works, and .ts wins if you somehow have more than one.
Some requests are answered before it and so never reach it. The rule is worth holding onto rather than a list: anything the listener shell or the framework's pre-analysis stage answers bypasses root middleware, and everything routed with your app reaches it. In practice that means WebSocket upgrades bound for a route.ts exporting WS, the dev live-reload SSE stream at /__webjs/events, and the framework's own /__webjs/* runtime assets and health probes, which your app needs in order to boot at all. In development only, static files from public/ (served at the url root) are answered there too, so a stylesheet is never queued behind the dev server's startup analysis.
One case is worth calling out on its own, because the rule gives the right answer and intuition does not: webjs.redirects and webjs.trailingSlash are configured by you, but they are resolved by the framework before your middleware runs. So a request that a redirect rule answers with a 308 never reaches root middleware, and a logging or auth middleware will not see it. If you need middleware to observe those requests, do the redirect in the middleware rather than in config.
Two things that look like exceptions are not. In production those public/ files go through root middleware normally, so a middleware that protects an asset still protects it where it counts. And server actions are routed with your app, not answered early, so root middleware DOES run for an action call: that is what lets you gate actions with auth or rate limiting.
my-app/
middleware.ts # root middleware: runs on every app request
app/
page.ts
api/
hello/
route.ts // middleware.ts
export default async function middleware(
req: Request,
next: () => Promise<Response>,
): Promise<Response> {
const started = Date.now();
const resp = await next();
const elapsed = Date.now() - started;
console.log(`${req.method} ${new URL(req.url).pathname} -> ${resp.status} (${elapsed}ms)`);
resp.headers.set('x-response-time', `${elapsed}ms`);
return resp;
} Next.js conventions: proxy, named exports, and matcher
Middleware written for Next.js works as is:
- The file may be
proxy.ts(Next 16's name) instead ofmiddleware.ts. Having both is an error: production refuses to start and development answers every request naming the two files. - The function may be the default export, a named
middlewareexport, or a namedproxyexport. - Returning nothing continues, like
NextResponse.next(). Return aResponseto answer, or callnext()to run the rest of the chain and post-process its response.next()runs the chain once however many times you call it, andnext.waitUntil(promise)keeps background work (an analytics write) going after the response is sent. export const config = { matcher }scopes it. A matcher is a path pattern ('/dashboard/:path*', or a regex group such as'/((?!api|_next).*)'), an array of them, or objects{ source, has, missing }whose conditions read aheader,cookie,queryparameter orhost, exactly as in Next. A request it does not match skips the middleware. Paths are matched with thewebjs.basePathremoved, and a trailing slash is tolerated.request.cookiesis Next.js'sRequestCookies:request.cookies.get('session')?.valuereads, andrequest.cookies.set(...)/delete(...)change only the cookies the rest of this request reads (the page'scookies()included), sending nothing to the browser, as in Next. Set a cookie on the response withcookies().set(...), where Next.js code writesresponse.cookies.set(...). Type the request asMiddlewareRequestfrom@webjsdev/server, where Next.js code writesNextRequest.request.nextUrldoes not exist: middleware receives the platformRequest. In development, reading it throws naming the fix,new URL(request.url).
// proxy.ts
import type { MiddlewareRequest } from '@webjsdev/server';
export function proxy(request: MiddlewareRequest) {
if (!request.cookies.get('session')?.value) {
return Response.redirect(new URL('/login', request.url), 307);
}
// returning nothing continues to the page
}
export const config = {
matcher: ['/dashboard/:path*', { source: '/api/:path*', missing: [{ type: 'header', key: 'authorization' }] }],
}; A middleware that cannot load fails closed. Middleware is usually a guard, so a syntax error, a missing export, or a bad matcher never lets requests through unguarded. A broken root middleware or proxy stops the app serving (the dev overlay shows the cause, and production readiness stays 503), and a broken per-segment middleware answers 500 for its subtree while the rest of the app keeps serving.
Per-Segment Middleware
Place a middleware.ts inside any directory under app/ to scope it to that subtree. It runs only for requests whose URL matches that segment and its children.
my-app/
middleware.ts # root: every app request
app/
page.ts # /: root + no segment middleware
dashboard/
middleware.ts # only /dashboard/* requests
page.ts # / dashboard
settings/
page.ts # /dashboard/settings
api/
auth/
middleware.ts # only /api/auth/* requests
login/
route.ts # POST /api/auth/login
signup/
route.ts # POST /api/auth/signup Signature
Every middleware function has the same signature, whether root or per-segment:
export default async function middleware(
req: Request,
next: () => Promise<Response>,
): Promise<Response | void> - req: a standard Request object. Read headers, cookies, URL, method, body.
- next(): calls the next middleware in the chain, or the final handler (page render, API route, action). Returns a
Promise<Response>. - Return value: a
Response, either the one fromnext()(optionally modified) or your own to short-circuit the chain. Returning nothing continues, as if you returnednext(). Any other value is an error.
The function is the file's default export, or a named middleware or proxy export (see Next.js conventions).
Chain Order
When a request arrives, middleware executes in this order:
- Root middleware (
middleware.tsat project root) - Outermost segment middleware (
app/middleware.tsif it exists) - Next segment (
app/dashboard/middleware.ts) - Innermost segment (deepest
middleware.tson the matched route) - Handler (page SSR, API route, or server action)
Each middleware calls next() to proceed. Responses bubble back up through the chain in reverse order, so outer middleware can inspect or modify the final response.
// Execution flow for GET /dashboard/settings:
//
// root middleware
// -> app/dashboard/middleware.ts
// -> SSR app/dashboard/settings/page.ts
// <- Response
// <- Response (dashboard middleware can modify)
// <- Response (root middleware can modify) Short-Circuiting
Return a Response without calling next() to stop the chain early. The request never reaches downstream middleware or the route handler.
// app/api/middleware.ts: require API key for all /api/* routes
export default async function apiAuth(
req: Request,
next: () => Promise<Response>,
): Promise<Response> {
const key = req.headers.get('x-api-key');
if (key !== process.env.API_KEY) {
return Response.json(
{ error: 'Invalid API key' },
{ status: 401 },
);
}
return next();
} Use Case: Auth Gate on /dashboard
A common pattern: require authentication for an entire subtree by placing a middleware in the segment directory.
// app/dashboard/middleware.ts
import { cookies } from '@webjsdev/server';
import { getUserByToken, SESSION_COOKIE } from '#lib/session.server.ts';
export default async function requireAuth(
req: Request,
next: () => Promise<Response>,
): Promise<Response> {
const user = await getUserByToken(cookies().get(SESSION_COOKIE)?.value);
if (!user) {
const to = encodeURIComponent(new URL(req.url).pathname);
return new Response(null, {
status: 302,
headers: { location: `/login?then=${to}` },
});
}
return next();
} Every page and API route under app/dashboard/ is now protected. Unauthenticated users are redirected to /login with a then query param so they can be sent back after signing in.
Use Case: Logging and Timing
// middleware.ts (root)
export default async function logger(
req: Request,
next: () => Promise<Response>,
): Promise<Response> {
const url = new URL(req.url);
const start = Date.now();
const resp = await next();
const ms = Date.now() - start;
console.log(`${req.method} ${url.pathname} ${resp.status} ${ms}ms`);
return resp;
} Use Case: CORS Headers
// app/api/middleware.ts: add CORS to all /api/* routes
export default async function cors(
req: Request,
next: () => Promise<Response>,
): Promise<Response> {
// Preflight
if (req.method === 'OPTIONS') {
return new Response(null, {
status: 204,
headers: {
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',
'access-control-allow-headers': 'content-type, authorization',
'access-control-max-age': '86400',
},
});
}
const resp = await next();
resp.headers.set('access-control-allow-origin', '*');
return resp;
} WebJs also ships a ready-made cors() middleware (from @webjsdev/server) you can wrap around a single route.ts handler. Use middleware CORS when you need blanket coverage across all routes in a segment.
Rate Limiting
WebJs ships a built-in rate limiter as a middleware factory. Import rateLimit from @webjsdev/server:
// app/api/auth/middleware.ts
import { rateLimit } from '@webjsdev/server';
export default rateLimit({ window: '10s', max: 5 }); That single line protects every route under /api/auth/ (login, signup, password reset) with a limit of 5 requests per 10 seconds per IP address.
rateLimit() Options
rateLimit({
window: '1m', // time window: number (ms) or string: '30s', '1m', '1h'
max: 60, // max requests per window per key
key: req => { // custom key function (default: the socket IP; forwarded headers only with trustProxy: true)
return `login:${req.headers.get('x-forwarded-for') || 'anon'}`;
},
message: 'Slow down' // custom 429 error message
}) The rate limiter is in-memory and uses a fixed-window algorithm. Response headers are set automatically:
x-ratelimit-limit: the max for this windowx-ratelimit-remaining: requests left in the current windowx-ratelimit-reset: unix timestamp when the window resetsretry-after: seconds until the window resets (on 429 responses only)
For multi-instance deployments, rate-limit at the edge (nginx, Cloudflare, AWS WAF) or use the key function to integrate with a shared store like Redis.
cookies() and headers() Helpers
WebJs provides request-scoped helpers via @webjsdev/server that let you read cookies and headers from anywhere in your server-side code (middleware, pages, server actions, API routes) without explicitly threading the request object:
import { cookies, headers } from '@webjsdev/server';
// In any server-side function (Next.js's next/headers shape):
const token = cookies().get('session_token')?.value; // { name, value } | undefined
const hasToken = cookies().has('session_token');
const all = cookies().getAll(); // { name, value }[]
const theme = (await cookies()).get('theme')?.value; // also awaitable, as in Next 15
// Writing, from a server action, a route handler, or middleware:
cookies().set('theme', 'dark', { httpOnly: true, sameSite: 'lax', maxAge: 60 * 60 * 24 });
cookies().delete('session_token');
const auth = headers().get('authorization');
const userAgent = headers().get('user-agent'); These are backed by AsyncLocalStorage. The request context is established before your middleware runs, so they work everywhere in the request lifecycle. Calling them outside a request scope (e.g., at module top level) throws an error.
cookies() has the same API as Next.js's: get(name) returns { name, value } (or undefined), so read a value with ?.value. set(name, value, options) (or set({ name, value, ...options })) and delete(name) (or delete(name, { path, domain }) for a cookie set on a path or domain, which the browser only clears when the same ones are named) queue a Set-Cookie on this request's response, which WebJs adds for you, redirects included, and a later read in the same request sees the new value. The options are Next's: maxAge is in seconds, path defaults to /, and httpOnly, secure, sameSite, domain, expires, partitioned and priority map to their cookie attributes. Inside middleware, request.cookies reads the same cookies, and its writes stay on the request. A write has to happen before the response headers go out: in a server action, a route handler, middleware, or the page render itself. A set() or delete() from a component that renders inside a streamed Suspense region runs after that point, so it throws naming the fix rather than being silently lost, much as Next.js refuses a cookie write during render.
Middleware and Server Actions
Root middleware runs on server action RPC calls (POST /__webjs/action/:hash/:fn) just like any other request. Per-segment middleware does not apply to server actions (they bypass the file-based route tree). For action-level guards, check auth inside the action itself, declare per-action middleware on the action, or call the action from a route.ts under a middleware-protected segment.
Middleware and API Routes
Per-segment middleware applies to API routes (route.ts) within the same subtree. If app/api/middleware.ts exists, it runs before app/api/hello/route.ts, app/api/auth/login/route.ts, and every other route under /api/.
Middleware chains nest: a request to /api/auth/login runs the root middleware, then app/api/middleware.ts, then app/api/auth/middleware.ts, then the route handler.
Tips
- Keep middleware fast. It runs on every request in its scope. Defer heavy work to the route handler when possible.
- Avoid mutating the request. The Web
RequestAPI is largely immutable. If you need to pass data downstream (e.g., a resolved user object), store it in a module-scopedAsyncLocalStorageor use a header. - One function per file. Export it as the default, or as a named
middlewareorproxyfunction, as in Next.js. Multiple middleware in one file are not supported. If you need composition, chain them manually inside your export. - Use
rateLimit()from@webjsdev/serverrather than writing your own. It handles cleanup, header injection, and per-bucket IP resolution that defaults to the framework-stamped socket address (spoof-safe) and only honoursX-Forwarded-For/CF-Connecting-IP/X-Real-IPwhen you opt in withtrustProxy: trueandWEBJS_NO_TRUST_PROXY=1is not set (that env var outranks the option). See Rate limiting for the threat model.