bun add @rescale/nemo
OSS Program Member
First, let's let the code get the word out...
beforemiddleware.ts
import { NextRequest } from 'next/server'; export const middleware = async (req: NextRequest) => { let user = undefined; let team = undefined; const token = req.headers.get('token'); if(req.nextUrl.pathname.startsWith('/auth')) { user = await getUserByToken(token); if(!user) { return NextResponse.redirect('/login'); } return NextResponse.next(); } if(req.nextUrl.pathname.startsWith('/team/') || req.nextUrl.pathname.startsWith('/t/')) { user = await getUserByToken(token); if(!user) { return NextResponse.redirect('/login'); } const slug = req.nextUrl.query.slug; team = await getTeamBySlug(slug); if(!team) { return NextResponse.redirect('/'); } return NextResponse.next(); } return NextResponse.next(); } export const config = { matcher: ['/((?!_next/|_static|_vercel|[\\w-]+\\.\\w+).*)'], };
aftermiddleware.ts
import { createNEMO, type MiddlewareFunctionProps } from '@rescale/nemo'; import { auth } from '@/app/(auth)/auth/_middleware'; import { team } from '@/app/(team)/team/_middleware'; const globalMiddlewares = { before: auth, // OR: [auth, ...] }; const middlewares = { '/auth/:path*': auth, '/(team|t)/:slug/:path*': team, // OR: [team, ...] }; export const middleware = createNEMO(middlewares, globalMiddlewares); export const config = { matcher: ['/((?!_next/|_static|_vercel|[\\w-]+\\.\\w+).*)'], };
Do you feel the difference?
Simplify your middlewares now!
Get startedSupports
Dynamic segments
Run your middleware for any route or dynamic segment
Functions Chaining
Chain multiple middlewares for single group
Shared context
Functions can share context between each other
Open-source
Something is missing? Just add it or post an issue on GitHub
Developer Experience
Developers are most important to me, this package will be as easy and useful as possible
Want to help with this project?
Review existing issues, make an PR's with what's missing!
Handcrafted by ZanReal