{"library":"next-compose-middleware","title":"Next Compose Middleware","description":"next-compose-middleware is a library designed to simplify the creation of complex and declarative middleware for Next.js applications, particularly leveraging Next.js's Edge Runtime middleware. It enables developers to construct highly readable and maintainable middleware logic by composing multiple functions. The current stable version is 2.0.4, indicating active development with incremental releases. Key differentiators include its path-based middleware execution, allowing for \"Nested Middleware\" behavior, and the ability to compose functions with early exit mechanisms (`breakAll`, `breakOnce`) for fine-grained control over execution flow. This approach helps in organizing middleware into logical, reusable units, enhancing maintainability for applications with intricate authorization, authentication, or request transformation requirements in a single `middleware.ts` file.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install next-compose-middleware"],"cli":null},"imports":["import { composeMiddleware } from 'next-compose-middleware';","import type { ComposableMiddleware } from 'next-compose-middleware';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { NextRequest, NextResponse } from 'next/server';\nimport { composeMiddleware, ComposableMiddleware } from 'next-compose-middleware';\n\n// Example middleware functions\nconst root1: ComposableMiddleware = async (req, res) => {\n  console.log('Executing root1 for path:', req.nextUrl.pathname);\n  // Example: Modify response headers or cookies\n  res.headers.set('X-Root-Middleware-1', 'processed');\n  return res;\n};\n\nconst root2: ComposableMiddleware = async (req, res) => {\n  console.log('Executing root2 for path:', req.nextUrl.pathname);\n  return res;\n};\n\nconst foo: ComposableMiddleware = async (req, res) => {\n  console.log('Executing foo for path:', req.nextUrl.pathname);\n  return res;\n};\n\nconst fooBar: ComposableMiddleware = async (req, res) => {\n  console.log('Executing fooBar for path:', req.nextUrl.pathname);\n  // Early exit example: if a condition is met, stop further middleware\n  if (req.nextUrl.searchParams.has('exit')) {\n    console.log('Early exiting from fooBar!');\n    return res; // Returning res without breakAll/breakOnce continues chain, but no further changes here.\n  }\n  return res;\n};\n\nconst fooId: ComposableMiddleware = async (req, res) => {\n  const id = req.nextUrl.pathname.split('/')[2];\n  console.log('Executing fooId for path:', req.nextUrl.pathname, 'ID:', id);\n  return res;\n};\n\nconst fooIdBaz: ComposableMiddleware = async (req, res) => {\n  console.log('Executing fooIdBaz for path:', req.nextUrl.pathname);\n  return res;\n};\n\nconst fooQux: ComposableMiddleware = async (req, res) => {\n  console.log('Executing fooQux for path:', req.nextUrl.pathname);\n  return res;\n};\n\nexport default async function middleware(req: NextRequest) {\n  console.log(`\\n--- Incoming Request for ${req.nextUrl.pathname} ---`);\n  \n  // Compose middleware based on paths\n  return composeMiddleware(req, NextResponse.next(), {\n    scripts: [root1, root2], // Applied to all matching paths\n    '/foo': {\n      scripts: [foo], // Applied to /foo and its children\n      '/bar': {\n        scripts: [fooBar], // Applied to /foo/bar and its children\n      },\n      '/[id]': { // Dynamic segment\n        scripts: [fooId], // Applied to /foo/:id and its children\n        '/baz': [fooIdBaz] // Applied to /foo/:id/baz\n      },\n      '/qux': [fooQux] // Applied to /foo/qux\n    }\n  });\n}","lang":"typescript","description":"Demonstrates how to set up `next-compose-middleware` with path-based execution for different routes and compose multiple middleware functions, including an example of conditional logic for early exit.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}