6290689281
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)
24 lines
609 B
TypeScript
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'
|
|
],
|
|
}
|