Comment by rozumbrada

1 month ago

If you finally decided to support proper server-side middleware, why is there still a limitation for only one middleware function and not a chain of middleewares as every other sane server implementation offers?

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.

  • That doesn't answer parent's question.

    People expect "middleware" to mean a certain thing and work a certain way.

    •   middleware = 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.

      2 replies →