How to use multiple middlewares in Next.js using the middleware.ts file?

You can use middleware chaining for this purpose. Here’s how you can achieve this: Create a folder called middlewares in your src folder. Create a file called stackHandler.ts in the middlewares folder and paste this content into it: import { NextMiddleware, NextResponse } from “next/server”; export function stackMiddlewares(functions: MiddlewareFactory[] = [], index = 0): NextMiddleware … Read more

NextJS 13 folder structure best practice

I think reading this section in Next docs helps you organize the project folders: https://nextjs.org/docs/app/building-your-application/routing/colocation I tried with many different structures and I finally chose this one: Everything (all folders and files) will be in the /app directory because the /app directory accepts colocation and it’s different from /pages directory which was only for routing … Read more