#is it possible to create multiple middlewares in nextjs ?

13 messages · Page 1 of 1 (latest)

leaden latch
#

i was using the single middleware file created at the root of the app and then i figure out that it will contains too much code and i want to know if there is a way to create multiple middlewares and not just one single file with thousands of code and if statements

gloomy gate
#

I have an npm module for that exact purpose, I planned on releasing it later this week

#

But what you can do right now is just pull code into modules and import it in your middleware file

leaden latch
#

oh really

#

that's nice fr !

#

i've seen someone using something like stackedMiddlewares but didn't understand it that much

gloomy gate
#

the module i made adds a few file conventions to the app directory and then generates a single middleware file in the root that combines those files

#

so a middleware in your /app folder will run on every request, a middleware in a /app/profile folder will run on every request starting with /profile etc.

leaden latch
#

oh that sounds great

#

do you have docs on how can i integrate it ?

gloomy gate
#

it will have docs on how to integrate, im really excited to release it

graceful bone
gloomy gate
# graceful bone hey any lucky yet on the multiple middleware issue

sorry for the delayed response. i have published the package here https://github.com/whopio/next-app-middleware - its still in beta for as long as the appDir is in experimental but we are actively implementing it and therefore maintaining it. the request and response objects passed to handlers are very sporadic right now, feel free to request any features you need or would make sense to have. if you have any issues or questions feel free to message me on discord or open an issue in the repo

GitHub

This next.js extension allows for middleware to live in the app directory by bundling all special file conventions in the app directory into a single middleware file in your project root. - GitHub ...