rsnext/examples/middleware-matcher/middleware.ts
Max Proske 6290689281
Convert middleware-matcher example to TypeScript (#42520)
Converted example to TypeScript to match Contribution docs.
- Set cookies using `NextResponse` instead of the `Set-Cookie` header

## Documentation / Examples

- [X] Make sure the linting passes by running `pnpm build && pnpm lint`
- [X] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-11-06 06:08:06 +00:00

24 lines
609 B
TypeScript

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export default function middleware(req: NextRequest) {
const path = req.nextUrl.pathname
const slug = path.slice(1)
// Set a cookie on the response using the `ResponseCookies` API
const response = NextResponse.next()
response.cookies.set({
name: 'middleware-slug',
value: slug,
path,
})
return response
}
export const config = {
matcher: [
'/disclaimer', // match a single, specific page
'/((?!public|static).*)', // match all paths not starting with 'public' or 'static'
],
}