Skip to content

Instantly share code, notes, and snippets.

@z4nr34l
Created January 7, 2024 19:03
Show Gist options
  • Save z4nr34l/d6d10e5115b9bedfc9503e22ecb4b15b to your computer and use it in GitHub Desktop.
Save z4nr34l/d6d10e5115b9bedfc9503e22ecb4b15b to your computer and use it in GitHub Desktop.
Next.js default middleware with path matching
// example code source: https://github.com/vercel/next.js/blob/canary/examples/middleware/middleware.ts
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname === '/about') {
return NextResponse.redirect(new URL('/redirected', request.url))
}
if (request.nextUrl.pathname === '/another') {
return NextResponse.rewrite(new URL('/rewrite', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/about/:path*', '/another/:path*'],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment