5 ms·
Consider middleware.ts as a root middleware. Nothing is stopping you from creating your own chain (which is trivial) in there. I mean, that would eventually wor
by bestest 1y ago
Consider middleware.ts as a root middleware. Nothing is stopping you from creating your own chain (which is trivial) in there. I mean, that would eventually work the same if nextjs implemented that feature — there would be a root somewhere.
- rs186 1y agoThat doesn't answer parent's question. People expect "middleware" to mean a certain thing and work a certain way.
- bestest 1y agomiddleware = fn(req) → next(req). express/koa give you the use() chain. next.js gives you one root, but nothing stops you from chaining yourself. same semantics, just manual wiring. type mw = (req: Request, next: () => Response) => Response; const logger: mw = (req, next) => { console.log(req.url); return next(); }; const auth: mw = (req, next) => { if (!req.headers.get("x-auth")) return new Response("forbidden", { status: 403 }); return next(); }; function chain(mws: mw[]) { return (req: Request) => mws.reduceRight((next, mw) => () => mw(req, next), () => new Response("ok"))(); } export function middleware(req: Request) { return chain([logger, auth])(req); } root is given, chain is trivial. that’s middleware.
- rafaelmn 1y agoNothing trivial about that implementation in my mind - need to keep track of where middleware is registered, reduceRight is non obvious. I expect these things to be standardized by the framework and all the sharp edges filed off - thats why I go to a framework in the first place.
- foldr 1y agoThe reduceRight is just a bit of cute FP code golf. All it’s saying is that chaining an empty list of middleware yields an ‘OK’ response, and that the first middleware is passed a function which, when called, executes the remaining middleware chain, and so on. It would be obvious enough if written out as a for loop, or via direct recursion. (My username has never been more appropriate!)