Help: Does middleware run only on the edge, or can I have it run solely on the nodejs/next server?
Unanswered
Mugger Crocodile posted this in #help-forum
Mugger CrocodileOP
Hi folks,
I have an application running on Next.js v12.3.1, and I need to add functionalities that are typically implemented in middleware, such as:
Handling user sessions with a Redis store,
Caching certain application parameters on a Redis server,
Performing security validations on both Next.js SSR Pages and Next.js API.
This application doesn't utilize Edge functions. After going through the documentation, I was under the impression that middleware is meant for these types of validations. However, when I attempt to use any Node.js library for tasks like database or Redis connections, I encounter errors.
I tried incorporating user sessions using the next-session library (version 4.0.5) and a Redis store via ioredis. The two most frequent errors I've faced are "The edge runtime does not support the Node.js 'events' module." or something akin to redis-error throwing errors regarding the Node.js environment being present. Interestingly, these errors seem to be raised on the frontend during my development, as they are caught within the browser.
I'm a tad perplexed about how middlewares operate within a Next.js application.
Do middlewares in Next.js exclusively run on the edge?
If not, how should I configure my Next.js application to ensure the middleware is compiled and executed on the Node.js server?
If they are restricted to Edge functions, is there a standard place within Next.js where I can process all requests (both pages and API) before they're forwarded to the intended page or API?
Thanks in advance for any assistance and clarification!
I have an application running on Next.js v12.3.1, and I need to add functionalities that are typically implemented in middleware, such as:
Handling user sessions with a Redis store,
Caching certain application parameters on a Redis server,
Performing security validations on both Next.js SSR Pages and Next.js API.
This application doesn't utilize Edge functions. After going through the documentation, I was under the impression that middleware is meant for these types of validations. However, when I attempt to use any Node.js library for tasks like database or Redis connections, I encounter errors.
I tried incorporating user sessions using the next-session library (version 4.0.5) and a Redis store via ioredis. The two most frequent errors I've faced are "The edge runtime does not support the Node.js 'events' module." or something akin to redis-error throwing errors regarding the Node.js environment being present. Interestingly, these errors seem to be raised on the frontend during my development, as they are caught within the browser.
I'm a tad perplexed about how middlewares operate within a Next.js application.
Do middlewares in Next.js exclusively run on the edge?
If not, how should I configure my Next.js application to ensure the middleware is compiled and executed on the Node.js server?
If they are restricted to Edge functions, is there a standard place within Next.js where I can process all requests (both pages and API) before they're forwarded to the intended page or API?
Thanks in advance for any assistance and clarification!
9 Replies
European sprat
i don't know if things are different with the very old version of nextjs you're on but in current nextjs the runtime for middleware is edge and is unchangeable
i have no idea why they force that runtime on middleware when you can change it in other places
Mugger CrocodileOP
@European sprat thanks for such quick reply! I was not aware that 12.3.1 has become obselete so fast. Is there a standard place where I should handle requests and redirects for things like security and user sessions, or is this up to the developers to handle in each page/api as they prefer?
European sprat
i don't know if it's "obsolete" but we're now up to 13.4.19. i use middleware to like you describe as well as check auth at time of doing API requests
one way to get around the issue of edge runtime in middleware is you use an API endpoint inside the middleware that you fetch
it's kind of dirty but it works and it's what i'm doing because i use prisma and it doesn't work in middleware directly
Mugger CrocodileOP
I see. the problem in my case is that I need to perform security validations to prevent bots and other forms of abuse particulary on the API routes so in the end this inevitably leads to the need of connecting into some sort of server to handle authentication tokens. So in it seems to me that fastest solution in my case is to create a service class to handle those validations and call it on each SSR page or API requests, rigth?
@Mugger Crocodile I see. the problem in my case is that I need to perform security validations to prevent bots and other forms of abuse particulary on the API routes so in the end this inevitably leads to the need of connecting into some sort of server to handle authentication tokens. So in it seems to me that fastest solution in my case is to create a service class to handle those validations and call it on each SSR page or API requests, rigth?
European sprat
yeah for API routes that's what i do
Mugger CrocodileOP
great! I'll do that. thanks for sharing your knowledge and for helping me so quickly! I been looking for this confirmation for days! it seems that Next.js is changing fast and I was banging my head due to articles pointing me the other way that no longer work! Have a nice weekend!