/nemo:nemo-middleware-patternsfrom NEMO for Next.js
Writes and organizes @zanreal/nemo middleware: chains, nested/parameterized routes, global before/after, shared storage, redirects, rewrites, header/cookie forwarding, auth gating, i18n, and event.skip. Use when adding or changing logic inside a createNEMO config or debugging middleware execution.
Ships in NEMO for Next.js. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Everything here describes @zanreal/nemo 3.0.x as the source and tests implement it. If the docs or README disagree, trust this file and the source.
Signature of every middleware:
import type { NextMiddleware } from "@zanreal/nemo";
// (request: NextRequest, event: NemoEvent) => NextResponse | Response | null | undefined | void (or a Promise of it)
event is a NemoEvent: a NextFetchEvent plus event.storage, event.params, event.getParams(), event.skip() and event.log/warn/error.
| Return value | Chain |
|---|---|
undefined, null, nothing | continues |
NextResponse.next() (with or without { headers }) | continues, its headers are merged |
NextResponse.redirect(url) | stops |
NextResponse.rewrite(url) | stops |
NextResponse.json(...) | stops |
new Response(...), Response.redirect(...) | stops |
| thrown error | goes to errorHandler, see nemo-debugging |
The rule in the source: a NextResponse continues unless it has x-middleware-rewrite, a Location header or a JSON content type. Any other kind of result stops.
Trap: new NextResponse("Unauthorized", { status: 401 }) has none of those, so it CONTINUES and the 401 is lost. For a blocking response use new Response("Unauthorized", { status: 401 }) or NextResponse.json({ error: "Unauthorized" }, { status: 401 }).
Trap: NextResponse.redirect and rewrite need absolute URLs. Use new URL("/login", request.url) or request.nextUrl.clone().
"/foo" does not run for /foo/bar).middleware key holds the parent's own function(s). Nested children are prefix matches too."/" only runs for /.:, *, () use path-to-regexp v6, case insensitive. Plain paths are compared exactly (case sensitive).before, matched routes (by nesting depth, parents first; at the same depth, longer patterns first), global after. Within an array, in array order.references/matching.md.import { createNEMO } from "@zanreal/nemo";
import { NextResponse } from "next/server";
export const proxy = createNEMO({
"/dashboard": {
middleware: (request) => {
if (!request.cookies.has("session")) {
const url = new URL("/login", request.url);
url.searchParams.set("next", request.nextUrl.pathname);
return NextResponse.redirect(url);
}
},
"/admin": (request) => {
if (request.cookies.get("role")?.value !== "admin") {
return new Response("Forbidden", { status: 403 });
}
},
},
});
The /dashboard function guards /dashboard and everything under it. /dashboard/admin (and /dashboard/admin/x, since nested children match as prefixes) runs the parent first, then the child. If the parent redirects, the child never runs.
Set request headers. NEMO diffs request headers at the end of the chain and forwards the changes to the page (readable with headers() from next/headers) and also puts them on the response.
import { createNEMO } from "@zanreal/nemo";
export const proxy = createNEMO(
{
"/:locale(en|pl)/:path*": (request, event) => {
const { locale } = event.params;
request.headers.set("x-locale", String(locale));
},
},
{
before: (request) => {
request.headers.set("x-request-id", crypto.randomUUID());
},
},
);
NextResponse.next({ request: { headers } }) and NextResponse.next({ headers }) from a continuing middleware are also merged for later middleware. Details: references/responses-and-headers.md.
event.storageimport { createNEMO } from "@zanreal/nemo";
import { MemoryStorageAdapter } from "@zanreal/nemo/storage";
export const proxy = createNEMO(
{
"/app/:path*": [
(request, event) => {
event.storage.set("userId", request.cookies.get("uid")?.value);
},
(request, event) => {
const userId = event.storage.get<string>("userId");
if (userId) request.headers.set("x-user-id", userId);
},
],
},
undefined,
{ storage: () => new MemoryStorageAdapter() },
);
Important: with no storage option, or with a storage INSTANCE, the same storage is shared by every request handled by that createNEMO. Data written in one request is visible in the next. Pass a factory storage: () => new MemoryStorageAdapter() for per-request isolation. event.storage is synchronous (get, set, has, delete, clear, keys, values, entries, size). Do not use the Storage interface exported from types, it is async and unused. See references/storage.md.
import { createNEMO } from "@zanreal/nemo";
export const proxy = createNEMO(
{
"/api/:path*": [
(request, event) => {
if (request.headers.get("x-internal") === "1") event.skip();
},
(request) => {
request.headers.set("x-checked", "1"); // skipped for internal calls
},
],
},
{ after: () => {} },
);
event.skip() skips the remaining middlewares of the current section (before, main or after). event.skip({ skipAfter: true }) also skips the global after chain.
import { createNEMO } from "@zanreal/nemo";
export const proxy = createNEMO(
{ "/": () => {} },
{
before: [(request) => { request.headers.set("x-start", String(Date.now())); }],
after: (request) => { request.headers.set("x-end", String(Date.now())); },
},
);
before and after accept one function or an array.
event.waitUntil(promise) hands the promise to the platform, so background work such as logging or analytics keeps running after the response is sent. Version note: this works in the first release after 3.0.2. On 3.0.2 and older the promise is not awaited and may be cut off; there, use after() from next/server (Next.js 15.1+) or move the work into the route handler.
references/matching.md: pattern syntax, what not to write, negative lookaheadreferences/responses-and-headers.md: headers, cookies, rewritesreferences/storage.md: adapter contract, per-request isolation, custom adapterreferences/recipes.md: auth gate, role check, i18n rewrite, Supabase session, other auth libraries, rate limit, loggingWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
4 skills in this plugin
/nemo-debuggingDebug and test @zanreal/nemo middleware - a middleware not running, a response ignored, a 401 or redirect lost, headers or cookies missing, storage leaking between requests, NemoMiddlewareError, event.log printing nothing, and unit tests with NextRequest. Use when NEMO behavior is unexpected or when writing tests for a createNEMO config./nemo-migrateMigrate to @zanreal/nemo from a single Next.js middleware.ts or proxy.ts, from the old @rescale/nemo package (with the nemo-codemod), or from the deprecated createMiddleware API. Use when the user mentions @rescale/nemo, the NEMO codemod, a Next.js 16 middleware-to-proxy rename, wants to split one large middleware into per-route functions, or sees a createMiddleware deprecation warning./nemo-setupSet up @zanreal/nemo in a Next.js app - install it, create proxy.ts (Next.js 16+) or middleware.ts (older) with createNEMO, add the matcher config and define the first routes. Use when the user wants multiple middlewares in Next.js, wants to split or compose middleware by path, or mentions NEMO, @zanreal/nemo, createNEMO or proxy.ts.npx claudepluginhub zanreal-labs/nemo --plugin nemoOther plugins' skills for the same job