{"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,"search_vec":"'0.0.8':26 'across':17 'allow':30 'api':18,41,48 'cadenc':61 'chain':4,14 'chainabl':40,52,72,74 'chainable-method':71 'compos':36 'data':23 'depend':9 'differenti':42 'earli':28 'early-stag':27 'fetch':24 'function':16,34 'getserversideprop':22 'global':54 'infrequ':62 'interfac':45 'javascript':67 'librari':12 'lightweight':6 'method':53,73 'middlewar':3,11,15,33,44,70 'next':68 'next.js':10 'nextj':2,69 'nextjs-middleware-chain':1 'option':59 'per':57 'per-rout':56 'releas':60,65 'reus':31 'rout':19,51,58 'ssr':21,50 'stabl':64 'stage':29 'unifi':43 'version':25 'yet':66 'zero':8 'zero-depend':7","created_at":"2026-04-25T07:39:09.810653+00:00","updated_at":"2026-04-25T07:39:09.810653+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/BenjaminWFox/nextjs-middleware-chain","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/nextjs-middleware-chain","openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}