
Security News
Google’s OSV Fix Just Added 500+ New Advisories — All Thanks to One Small Policy Change
A data handling bug in OSV.dev caused disputed CVEs to disappear from vulnerability feeds until a recent fix restored over 500 advisories.
@rescale/nemo
Advanced tools
A middleware composition library for Next.js applications that allows you to organize and chain middleware functions based on URL patterns.
A middleware composition library for Next.js applications that allows you to organize and chain middleware functions based on URL patterns.
npm install @rescale/nemo
pnpm add @rescale/nemo
bun add @rescale/nemo
MiddlewareFunction
Can be either a legacy Next.js middleware (NextMiddleware
) or the new middleware format (NewMiddleware
).
MiddlewareConfig
Record<string, MiddlewareFunction | MiddlewareFunction[]>
MiddlewareFunctionProps
interface MiddlewareFunctionProps {
request: NextRequest;
context: MiddlewareContext;
event: NextFetchEvent;
forward: (response: MiddlewareReturn) => void;
}
createMiddleware
function createMiddleware(
pathMiddlewareMap: MiddlewareConfig,
globalMiddleware?: {
before?: MiddlewareFunction | MiddlewareFunction[];
after?: MiddlewareFunction | MiddlewareFunction[];
}
): NextMiddleware
Creates a composed middleware function that:
forward
function forward(response: MiddlewareReturn): void
Function that allows passing response from legacy middleware functions to the next middleware in the chain. This enables compatibility between legacy Next.js middleware and the new middleware format.
To make it easier to understand, you can check the below examples:
Matches /dashboard
route and returns no params.
/dashboard
General structure of the params is :paramName
where paramName
is the name of the param that will be returned in the middleware function.
Matches /dashboard/anything
route and returns team
param with anything value
.
/dashboard/:team
You can also define segments in the middle of URL with is matching /team/anything/dashboard
and returns team
param with anything
value.
/dashboard/:team/delete
Matches /dashboard
and /dashboard/anything
routes and returns team
param with anything
value if there is value provided in url.
/dashboard{/:team}
/dashboard{/*team}
Matches /dashboard
and /dashboard/anything/test
routes and returns team
param with [anything, test]
value if there is value provided in url.
/dashboard/*team
To debug your matchers and params parsing you can use the following tool:
Rescale path-to-regexp debugger
import { createMiddleware } from '@rescale/nemo';
export default createMiddleware({
'/api{/*path}': async ({ request }) => {
// Handle API routes
},
'/protected{/*path}': async ({ request, context }) => {
// Handle protected routes
}
});
You can test your's matchers using this tool.
import { createMiddleware } from '@rescale/nemo';
export default createMiddleware({
'/api{/*path}': apiMiddleware,
},
{
before: [loggerMiddleware, authMiddleware],
after: cleanupMiddleware,
});
import { createMiddleware } from '@rescale/nemo';
export default createMiddleware({
'/*path': [
async ({ context }) => {
context.set('user', { id: 1 });
},
async ({ context }) => {
const user = context.get('user');
// Use the user data
}
]
});
context
Map is shared between all middleware functions in the chainI'm working with Next.js project for a few years now, after Vercel moved multiple /**/_middleware.ts
files to a single /middleware.ts
file, there was a unfilled gap - but just for now.
After a 2023 retro I had found that there is no good solution for that problem, so I took matters into my own hands. I wanted to share that motivation with everyone here, as I think that we all need to remember how it all started.
Hope it will save you some time and would make your project DX better!
FAQs
A middleware composition library for Next.js applications that allows you to organize and chain middleware functions based on URL patterns.
We found that @rescale/nemo demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A data handling bug in OSV.dev caused disputed CVEs to disappear from vulnerability feeds until a recent fix restored over 500 advisories.
Research
/Security News
175 malicious npm packages (26k+ downloads) used unpkg CDN to host redirect scripts for a credential-phishing campaign targeting 135+ organizations worldwide.
Security News
Python 3.14 adds template strings, deferred annotations, and subinterpreters, plus free-threaded mode, an experimental JIT, and Sigstore verification.