rsnext/examples/middleware/middleware.ts
Eduardo Sigrist Ciciliato 7664a40064
feat(examples): adds middleware example (#38240)
## Documentation / Examples

- [X] Make sure the linting passes by running `pnpm lint`
- [X] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


Co-authored-by: Lee Robinson <9113740+leerob@users.noreply.github.com>
2022-07-04 03:45:56 +00:00

15 lines
455 B
TypeScript

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*'],
}