Unverified Commit 5ec7933f authored by Aryan Malik's avatar Aryan Malik Committed by GitHub

Update 11-middleware.mdx: Added Switcher (#53977)

Added a switcher on a code snippet under heading -> `Using Cookies`, just like the other code snippets on this page got one..

(For those who don't know what a switcher is -> It's just a simple functionality for changing `typescript` code to `javascript` and vice-versa, kind of a UI feature for better experience`
Co-authored-by: 's avatarBalázs Orbán <18369201+balazsorban44@users.noreply.github.com>
parent 4c84ea55
...@@ -148,7 +148,7 @@ Cookies are regular headers. On a `Request`, they are stored in the `Cookie` hea ...@@ -148,7 +148,7 @@ Cookies are regular headers. On a `Request`, they are stored in the `Cookie` hea
1. For incoming requests, `cookies` comes with the following methods: `get`, `getAll`, `set`, and `delete` cookies. You can check for the existence of a cookie with `has` or remove all cookies with `clear`. 1. For incoming requests, `cookies` comes with the following methods: `get`, `getAll`, `set`, and `delete` cookies. You can check for the existence of a cookie with `has` or remove all cookies with `clear`.
2. For outgoing responses, `cookies` have the following methods `get`, `getAll`, `set`, and `delete`. 2. For outgoing responses, `cookies` have the following methods `get`, `getAll`, `set`, and `delete`.
```ts filename="middleware.ts" ```ts filename="middleware.ts" switcher
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server' import type { NextRequest } from 'next/server'
...@@ -180,6 +180,37 @@ export function middleware(request: NextRequest) { ...@@ -180,6 +180,37 @@ export function middleware(request: NextRequest) {
} }
``` ```
```js filename="middleware.js" switcher
import { NextResponse } from 'next/server'
export function middleware(request) {
// Assume a "Cookie:nextjs=fast" header to be present on the incoming request
// Getting cookies from the request using the `RequestCookies` API
let cookie = request.cookies.get('nextjs')
console.log(cookie) // => { name: 'nextjs', value: 'fast', Path: '/' }
const allCookies = request.cookies.getAll()
console.log(allCookies) // => [{ name: 'nextjs', value: 'fast' }]
request.cookies.has('nextjs') // => true
request.cookies.delete('nextjs')
request.cookies.has('nextjs') // => false
// Setting cookies on the response using the `ResponseCookies` API
const response = NextResponse.next()
response.cookies.set('vercel', 'fast')
response.cookies.set({
name: 'vercel',
value: 'fast',
path: '/',
})
cookie = response.cookies.get('vercel')
console.log(cookie) // => { name: 'vercel', value: 'fast', Path: '/' }
// The outgoing response will have a `Set-Cookie:vercel=fast;path=/test` header.
return response
}
```
## Setting Headers ## Setting Headers
You can set request and response headers using the `NextResponse` API (setting _request_ headers is available since Next.js v13.0.0). You can set request and response headers using the `NextResponse` API (setting _request_ headers is available since Next.js v13.0.0).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment