Middleware does work with self-hosting[1]. It’s a more limited runtime that’s based on web standard APIs, which creates optionality for running it in high performance / resource constrained scenarios.
[1] https://nextjs.org/docs/app/building-your-application/deploy...