---
title: "Adding Cookies"
description: "Read and write cookies directly at the edge."
url: "https://vercel.com/templates/next.js/cookies"
links:
  repository: "https://github.com/vercel/examples/tree/main/edge-middleware/cookies"
  demo: "https://edge-functions-cookies.vercel.app"
  deploy: "https://vercel.com/new/clone?demo-title=Adding+Cookies&demo-description=Read+and+write+cookies+directly+at+the+edge.&demo-url=https%3A%2F%2Fedge-functions-cookies.vercel.app&project-name=cookies&repository-name=cookies&repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fedge-middleware%2Fcookies&demo-image=%2F%2Fimages.ctfassets.net%2Fe5382hct74si%2F1xRRZgpsC5eAaiQhGIOQOt%2Fd0a4530c81e1c978ed3ad44f1882b730%2Fedge-functions-cookies.vercel.app_.png"
---

# Adding Cookies

Read and write cookies directly at the edge.

# Cookies Example

You can read and write cookies directly on the edge and action as needed.

## Demo

https://edge-functions-cookies.vercel.app

Since you can read cookies on the edge and rewrite the user to a statically generated page, eliminates the need of using getServerSideProps + redirect just to access the cookie, hence improving your site's performance and maintaining the same url.

The magic happens in the [`middleware.ts` file](https://github.com/vercel/examples/tree/main/edge-middleware/cookies/middleware.ts):

```javascript
import { NextRequest, NextResponse } from 'next/server'

export const config = {
  matcher: '/',
}

export default function middleware(req: NextRequest) {
  // Parse the cookie
  const isInBeta = JSON.parse(req.cookies.get('beta')?.value || 'false')

  // Update url pathname
  req.nextUrl.pathname = `/${isInBeta ? 'beta' : 'non-beta'}`

  // Rewrite to url
  return NextResponse.rewrite(req.nextUrl)
}
```

## How to Use

You can choose from one of the following two methods to use this repository:

### One-Click Deploy

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=vercel-examples):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/examples/tree/main/edge-middleware/cookies&project-name=cookies&repository-name=cookies)

### Clone and Deploy

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [pnpm](https://pnpm.io/installation) to bootstrap the example:

```bash
pnpm create next-app --example https://github.com/vercel/examples/tree/main/edge-middleware/cookies cookies
```

Next, run Next.js in development mode:

```bash
pnpm dev
```

Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=edge-middleware-eap) ([Documentation](https://nextjs.org/docs/deployment)).

## Related Templates

### [A/B Testing Simple](https://vercel.com/templates/next.js/ab-testing-simple)

By A/B testing at the edge, you'll reduce CLS from client-loaded experiments and improve your site's performance with smaller JS bundles.

---

## Additional documentation

The [Vercel Knowledge Base](https://vercel.com/kb) contains guides and answers to common questions about Vercel, including deployment, framework configuration, domains, caching, and troubleshooting.
