3 min read

Getting started with Next.js Edge Functions

Enes Akar

Cofounder @Upstash

What are Next.js Functions? Why are they important?
Getting Started with Next.js Functions
`1` Create a Next.js application and install Upstash Redis:
`2` Create a Redis database:
`3` Create the edge function:
`4` Run and Deploy:

What are Next.js Functions? Why are they important?

Today, the Vercel team announced the Next.js Edge functions. Edge functions allow developers to run their code at the servers distributed globally. This means your code will be executed at the location that is closest to your user. You can think of edge functions as the serverless functions which are run at the CDN infrastructure. Edge functions have the following advantages:

  • Global low latency: Because the code is replicated to many global locations (PoP: points of presence), a user anywhere in the world will experience low latency. Each client will fetch the response from the nearest server.
  • No cold start: Edge infrastructure providers use V8 Isolates which eliminates the cold starts. This means much faster startups.

Getting Started with Next.js Functions

Here we will write a Next.js edge function which will show a custom greeting depending on the location of the client. We will load the greeting message from Upstash Redis.

Check the repo and the demo.

1 Create a Next.js application and install Upstash Redis:

npx create-next-app@latest --typescript
cd nextjs-edge
npm install @upstash/redis

2 Create a Redis database:

Create a Global database for the best edge latency in Upstash Console. Connect to your database with redis-cli and add some greeting data as below:> set GB "Ey up?"
OK> set US "Yo, what’s up?"
OK> set TR "Naber dostum?"
OK> set DE "Was ist los?"
OK> set IN "Namaste"

3 Create the edge function:

Create pages/api/_middleware.ts as below:

import type { NextFetchEvent, NextRequest } from "next/server";
import { Redis } from "@upstash/redis";

export async function middleware(req: NextRequest, ev: NextFetchEvent) {
  const country = || "US";
  const redis = new Redis({
  const result = await redis.get<string>(country);
  const greeting = result || "Hello World!";
  return new Response(greeting);

Copy and replace UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN from your database page in Upstash Console.

4 Run and Deploy:

Run your application locally:

npm run dev

Check: http://localhost:3000/api/hello

You will see that req.geo is not defined. Deploy your app to Vercel to see how it is working at the Edge infrastructure:

vercel deploy


You can check the greeting from different locations using a VPN.


Next.js Edge is great news for developers who are building globally fast applications. Upstash Redis is a data service which is designed and optimized for edge functions.

Here useful links:

Let us know your thoughts on Twitter or Discord.

© 2023 Upstash, Inc. Based in California.

* Redis is a trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Any use by Upstash is for referential purposes only and does not indicate any sponsorship, endorsement or affiliation between Redis and Upstash.

** Cloudflare, the Cloudflare logo, and Cloudflare Workers are trademarks and/or registered trademarks of Cloudflare, Inc. in the United States and other jurisdictions.