Created
January 7, 2024 19:03
-
-
Save z4nr34l/d6d10e5115b9bedfc9503e22ecb4b15b to your computer and use it in GitHub Desktop.
Next.js default middleware with path matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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