πŸš€ A Better Way to Manage Middlewares in Next.js

πŸ“Œ Why Rethink Middleware Management?

Next.js provides a simple and efficient middleware system, but it comes with a major limitation: there is only one middleware.ts file for the entire application.

⚠️ Limitations of Next.js Middle…


This content originally appeared on DEV Community and was authored by Elliot Sutton

πŸ“Œ Why Rethink Middleware Management?

Next.js provides a simple and efficient middleware system, but it comes with a major limitation: there is only one middleware.ts file for the entire application.

⚠️ Limitations of Next.js Middleware

  • ❌ A single global middleware β†’ Impossible to have multiple independent middlewares.
  • ❌ Growing complexity β†’ All logic must be merged into a single file.
  • ❌ Difficulties integrating some libraries β†’ Hard to use multiple external middlewares without manual merging.

This is where Next MW comes in.

πŸ‘‰ Next MW is a lightweight and flexible library that allows you to compose multiple middlewares and execute them independently and sequentially, while maintaining full compatibility with Next.js.

Instead of struggling with a bloated middleware.ts file, you can define your middlewares separately and combine them cleanly.

βœ… Next MW: Modular and Flexible Middleware Management

With Next MW, you can:

  • βœ… Define multiple independent middlewares, each with its own configuration.
  • βœ… Execute them sequentially, without hacks or workarounds.
  • βœ… Maintain full compatibility with Next.js and its matchers.

πŸš€ Quick Installation

Install Next MW with your preferred package manager:

npm install next-mw
# or
yarn add next-mw
# or
pnpm add next-mw

πŸ”” Note: Requires Next.js >=13.1.0.

πŸ› οΈ Example Use Case

This example illustrates how Next MW allows you to organize multiple middlewares cleanly in a Next.js project by combining NextAuth v5 and next-intl.

πŸ“ File Organization

Instead of keeping everything in middleware.ts, you can structure your files like this:

πŸ“‚ middlewares
 β”œβ”€β”€ auth.ts         (NextAuth v5 middleware)
 β”œβ”€β”€ next-intl.ts    (next-intl middleware)
πŸ“„ middleware.ts

Then, in middleware.ts, simply compose them:

// middleware.ts
import { middlewares } from 'next-mw';

import * as nextIntl from './middlewares/next-intl';
import * as authMiddleware from './middlewares/auth';

export const middleware = middlewares(nextIntl, authMiddleware);

πŸ—οΈ Illustrative Middlewares

Here are two commonly used middlewares in a Next.js project, which Next MW allows you to neatly combine.

πŸ”‘ NextAuth v5 Middleware

// middlewares/auth.ts
import authConfig from '../auth.config';
import NextAuth from 'next-auth';

export const { auth: middleware } = NextAuth(authConfig);

🌍 next-intl Middleware

// middlewares/next-intl.ts
import createMiddleware from 'next-intl/middleware';
import { routing } from '../i18n/routing';

export const middleware = createMiddleware(routing);

export const config = {
  matcher: ['/', '/(fr|en)/:path*'],
};

🎯 Why Use Next MW?

  • βœ… Full modularity β†’ Each middleware remains independent.
  • βœ… Sequential execution β†’ Maintains a controlled execution order.
  • βœ… Fully compatible with Next.js β†’ Uses the native system.
  • βœ… Easy integration β†’ No hacks or complex setups required.
  • βœ… Works with both App and Pages directories β†’ Universal compatibility.

With Next MW, middleware management becomes cleaner, more flexible, and easier to maintain.

πŸ“Œ Useful Links

πŸ“¦ NPM Package
πŸ–₯️ GitHub Repository
🌍 Documentation & Website

Try Next MW today and simplify your Next.js middleware management! πŸš€

πŸ—¨οΈ Your Feedback Matters!

Do you use multiple middlewares in your Next.js project? Would Next MW help you? Share your experience in the comments! πŸ’¬


This content originally appeared on DEV Community and was authored by Elliot Sutton


Print Share Comment Cite Upload Translate Updates
APA

Elliot Sutton | Sciencx (2025-02-18T20:46:13+00:00) πŸš€ A Better Way to Manage Middlewares in Next.js. Retrieved from https://www.scien.cx/2025/02/18/%f0%9f%9a%80-a-better-way-to-manage-middlewares-in-next-js/

MLA
" » πŸš€ A Better Way to Manage Middlewares in Next.js." Elliot Sutton | Sciencx - Tuesday February 18, 2025, https://www.scien.cx/2025/02/18/%f0%9f%9a%80-a-better-way-to-manage-middlewares-in-next-js/
HARVARD
Elliot Sutton | Sciencx Tuesday February 18, 2025 » πŸš€ A Better Way to Manage Middlewares in Next.js., viewed ,<https://www.scien.cx/2025/02/18/%f0%9f%9a%80-a-better-way-to-manage-middlewares-in-next-js/>
VANCOUVER
Elliot Sutton | Sciencx - » πŸš€ A Better Way to Manage Middlewares in Next.js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/18/%f0%9f%9a%80-a-better-way-to-manage-middlewares-in-next-js/
CHICAGO
" » πŸš€ A Better Way to Manage Middlewares in Next.js." Elliot Sutton | Sciencx - Accessed . https://www.scien.cx/2025/02/18/%f0%9f%9a%80-a-better-way-to-manage-middlewares-in-next-js/
IEEE
" » πŸš€ A Better Way to Manage Middlewares in Next.js." Elliot Sutton | Sciencx [Online]. Available: https://www.scien.cx/2025/02/18/%f0%9f%9a%80-a-better-way-to-manage-middlewares-in-next-js/. [Accessed: ]
rf:citation
» πŸš€ A Better Way to Manage Middlewares in Next.js | Elliot Sutton | Sciencx | https://www.scien.cx/2025/02/18/%f0%9f%9a%80-a-better-way-to-manage-middlewares-in-next-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.