npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nextjs-middleware-stack

v2.0.2

Published

Composable route-based middleware for Next.js Middleware.

Readme

🚣 middlewareStack – A Composable Middleware Router for Next.js

A lightweight, composable middleware stack for Next.js that supports:

  • string route patterns (with dynamic parameters & wildcards)
  • Raw RegExp
  • Sync/async predicate functions
  • Strict pipe() composition API

Perfect for authentication gates, logging, redirects, feature flags, i18n integration, and layered request logic in Next.js Edge or Node runtimes.


✨ Features

  • ✅ Clean pipe() API (no tuple arrays)

  • ✅ String pattern matching via compare-path

    • :param dynamic segments
    • ** greedy wildcards
  • ✅ Native RegExp support

  • ✅ Sync or async predicate matching

  • ✅ Middleware short-circuiting

  • ✅ Edge & Node compatible

  • ✅ Type-safe enforcement of pipe() usage


📦 Installation

npm install nextjs-middleware-stack

🧠 API

pipe(pattern, handler)

Creates a middleware pipe.

export type PatternFn = (req: Request) => boolean | Promise<boolean>
export type Pattern = PatternFn | string | RegExp

Parameters

  • pattern

    • string route shape (/users/:id)
    • RegExp
    • predicate function (req) => boolean | Promise<boolean>
  • handler

    • (req: Request) => Response | void | Promise<Response | void>

pipe() must be used. Raw [pattern, handler] tuples are not supported.


middlewareStack(pipes)

middlewareStack(pipes: Pipe[])

Creates a Next.js-compatible middleware handler from an ordered list of pipe() calls.

Returns:

;(req: Request) => Promise<Response | void>

Ready to export as default from middleware.ts.


🚀 Example (Authentication Middleware)

// apps/example-app/src/middleware.ts
import { NextResponse } from 'next/server'
import { middlewareStack, pipe } from 'nextjs-middleware-stack'
import { validateAuthToken } from './utils/validate-jwt'

export default middlewareStack([
  pipe(/^\/dashboard\/.*/, async (req) => {
    const token = req.cookies.get('AUTH_TOKEN')?.value
    const isValid = token && (await validateAuthToken(token))

    if (!isValid) {
      return NextResponse.redirect(new URL('/login', req.url))
    }
  }),

  pipe('/public/:page', async () => {
    console.log('Public page hit')
  }),

  // Always run last
  pipe(
    () => true,
    async () => {
      console.log('Final middleware')
    }
  ),
])

📊 Supported Path Shapes

String patterns are powered by compare-path.

| Pattern | Matches | | --------------- | ------------------------- | | /user/:id | /user/42 | | /users/[id] | /users/42 | | /docs/**/edit | /docs/api/v1/intro/edit | | /a/:x/**/b/:y | /a/1/foo/bar/b/2 |

Use RegExp for advanced matching.

Use a predicate function when matching depends on:

  • Cookies
  • Headers
  • Geo
  • Runtime conditions
  • Feature flags

🧪 Example: String Matching

import { middlewareStack, pipe } from 'nextjs-middleware-stack'

export default middlewareStack([
  pipe('/users/:id', async (req) => {
    // Handle specific user route
  }),

  pipe('cars/:id', async () => {
    // Match /cars/123
  }),

  pipe('hello/**', async () => {
    // Match anything under /hello/
  }),
])

🧪 Example: Predicate Function

import { middlewareStack, pipe } from 'nextjs-middleware-stack'

export default middlewareStack([
  pipe(
    async (req) => {
      return req.url.includes('/api/') && !!req.headers.get('authorization')
    },
    async () => {
      console.log('Authenticated API request')
    }
  ),
])

🧹 Next.js Integration

You must also export config to control where middleware runs:

export const config = {
  matcher: [
    '/((?!_next|.*\\..*).*)', // Skip static assets
    '/(api|trpc)(.*)', // Always include API routes
  ],
}

🧠 How It Works

For each request:

  1. Iterate through pipes in order.
  2. Evaluate the pattern.
  3. If matched → execute the handler.
  4. If the handler returns a Response, execution stops.
  5. Otherwise → continue to the next pipe.

Middleware order matters.


💡 Tips

  • Place authentication gates before public routes.
  • Put always-run middleware last:
pipe(() => true, someMiddleware)
  • Prefer string shapes for readability.
  • Use predicate functions for request-aware logic.
  • Use RegExp only when necessary.

⚠️ Breaking Change (v2+)

middlewareStack now accepts only pipe() entries.

Old style:

middlewareStack([[/regex/, handler]])

is no longer supported.

Migration:

middlewareStack([pipe(/regex/, handler)])

Islam Yamor.