Getting started
Supercharge your Next.js middleware with NEMO
Installation
Import the package inside middleware.ts file.
(or create it if you don't have one yet)
Define your middlewares config and update the createMiddleware function parameters.
After that step, you should see an There is NEMO
message in your console for every request made to your application.
Optimize your middleware execution to not execute it on every signle request.
That will prevent your middleware from executing on routes:
/_next/
(Next.js internals)/_static
(inside /public)/_vercel
(Vercel internals)- Static files (e.g.
/favicon.ico
,/sitemap.xml
,/robots.txt
, etc.)
Reed more about Next.js middleware configuration
Finally, let's put it all together.
That's how should your middleware.ts
file looks like after all steps.
Motivation
I'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!