{"id":18600,"library":"nextjs-middleware-chain","title":"nextjs-middleware-chain","description":"A lightweight, zero-dependency Next.js middleware library for chaining middleware functions across API routes and SSR (getServerSideProps) data fetching. Version 0.0.8 (early-stage). Allows reuse of middleware functions by composing them in a chainable API. Differentiators: unified middleware interface for both API and SSR routes, chainable methods, global and per-route options. Release cadence: infrequent, no stable release yet.","status":"active","version":"0.0.8","language":"javascript","source_language":"en","source_url":"https://github.com/BenjaminWFox/nextjs-middleware-chain","tags":["javascript","next","nextjs","middleware","chainable-methods","chainable"],"install":[{"cmd":"npm install nextjs-middleware-chain","lang":"bash","label":"npm"},{"cmd":"yarn add nextjs-middleware-chain","lang":"bash","label":"yarn"},{"cmd":"pnpm add nextjs-middleware-chain","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-only package; CommonJS require will not work.","wrong":"const createMiddleware = require('nextjs-middleware-chain')","symbol":"createMiddleware","correct":"import { createMiddleware } from 'nextjs-middleware-chain'"},{"note":"The package exports only 'createMiddleware' as a named export, NOT a default export. The example in README shows importing a default from a local file, not from the package.","wrong":"import { default as mwFactory } from './middleware'","symbol":"default export","correct":"import mwFactory from './middleware'"},{"note":"Chaining requires calling the middleware functions before finish(). Finish() expects a route handler and an optional name.","wrong":"mwFactory().finish(routeFunction)","symbol":"finished route","correct":"mwFactory().mwFunction().finish(routeFunction, 'Route Name')"}],"quickstart":{"code":"// middleware.js\nimport { createMiddleware } from 'nextjs-middleware-chain';\n\nconst logger = (req, res, next) => {\n  console.log('Request:', req.method, req.url);\n  return next();\n};\n\nconst mwFactory = createMiddleware([logger], { useChainOrder: true });\nexport default mwFactory;\n\n// pages/api/hello.js\nimport mwFactory from '../../middleware';\n\nconst handler = (req, res) => {\n  res.status(200).json({ message: 'Hello' });\n};\n\nexport default mwFactory().logger().finish(handler, 'Hello API');","lang":"javascript","description":"Shows setup with createMiddleware, then chaining logger and finish in an API route."},"warnings":[{"fix":"Always return next() at the end of each middleware function.","message":"Middleware functions must call next() and return its result, or the chain breaks.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Pin exact version and test upgrades carefully.","message":"Package is in early stage (v0.0.8); API may change without major semver bump.","severity":"breaking","affected_versions":"<=0.0.8"},{"fix":"Always chain .finish(handler, name) after middleware functions.","message":"The 'finish' method is required at the end of the chain; otherwise the route handler is never called.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Pass full options object each time; do not rely on partial overrides.","message":"Options passed to createMiddleware or the factory are not deeply merged; they override defaults entirely.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Wrap the function in an array: createMiddleware([myFn])","cause":"createMiddleware expects an array but a single function was passed.","error":"TypeError: middlewareFunctionsArray is not iterable"},{"fix":"Ensure chaining syntax: mwFactory().middlewareFn().finish(handler)","cause":"Middleware factory returned undefined because no chainable method was called before finish.","error":"TypeError: Cannot read properties of undefined (reading 'finish')"},{"fix":"Add 'return next();' at the end of each middleware function.","cause":"Middleware function did not call next() or return next().","error":"Error: middleware function must return a promise or call next"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}