What are the best practices for using Next.js Middleware with Vercel?

When building a web app with Next.js and deploying it on Vercel, how should I correctly use Next.js Middleware (special functions that run before a request is completed) to make my app secure, fast, and reliable?

Hi @suheb, this seems like a very broad question. Can you share what part of your app are you trying to secure? Are you using any authentication providers like Auth.js or Clerk or Supabase? Usually Middleware is part of the setup for these providers.

If you are rolling your own auth, I’d encourage you to read the Guides: Authentication | Next.js guide, which discusses the best practices and techniques in depth, including use of Middlewares.