Skip to content

@vercel/edge

Learn about the @vercel/edge package and its available helpers for use in Edge Functions and Edge Middleware.

The @vercel/edge package is a Node.js module that provides a set of helper functions for use in Edge Functions and Edge Middleware. These helpers are Vercel-specific, and take advantage of functionality built into the Vercel platform.

If you want to take action in your Edge Functions and Middleware based on a user's location, or IP address, we highly recommend you use these helpers. They enable you to do things like show localized content or block malicious IPs.

Read the following section to learn how to get started.

Available helpers for use in Edge Functions:

  • geolocation: Returns the location information for the incoming request
  • ipAddress: Returns the IP address of the request from the headers

The following helpers are available exclusively in Edge Middleware:

  • rewrite: Returns a response that rewrites the request to a different URL - Edge Middleware specific
  • next - Edge Middleware specific

Add the @vercel/edge to your project with:


terminal
npm i @vercel/edge

Installing @vercel/edge using the npm command.

The geolocation helper returns the location information for the incoming request. The function receives a request object and returns the following properties:

  • city: The city that the request originated from
  • country: The country that the request originated from
  • latitude: The latitude of the client
  • longitude: The longitude of the client
  • region: The Vercel Edge Network region that received the request

Each property returns a string, or undefined.

index.ts
import { geolocation } from '@vercel/edge';
export const config = {
  runtime: 'edge',
};
export default function (request: Request) {
  const { city } = geolocation(request);
  // You can also get the city using dot notation on the function
  // const city = geolocation(request).city;
  return new Response(`<h1>Your location is ${city}</h1>`, {
    headers: { 'content-type': 'text/html' },
  });
}

The ipAddress helper returns the IP address of the request from the headers. The function receives a request object, and the returned value is a string with the IP address, or undefined.

index.ts
import { ipAddress } from '@vercel/edge';
export const config = {
  runtime: 'edge',
};
export default function (request: Request) {
  const ip = ipAddress(request) || 'unknown';
  return new Response(`<h1>Your IP is ${ip}</h1>`, {
    headers: { 'content-type': 'text/html' },
  });
}

The following helpers are only available in Edge Middleware.

The rewrite helper returns a response that rewrites the request to a different URL. The following example shows how to rewrite a request to a different URL:

middleware.ts
import { rewrite } from '@vercel/edge';
export function middleware(request: Request) {
  return rewrite(new URL('/about-2', request.url));
}

The next helper returns a Response that instructs the function to continue processing the request. The function receives a request object and returns a Response object.

The following example adds a custom header:

middleware.js
import { next } from '@vercel/edge';
export default function middleware() {
  return next({
    headers: { 'x-from-middleware': 'true' },
  });
}

A no-op example. This will return a 200 OK response with no further action:

middleware.js
import { next } from '@vercel/edge';
export default function middleware() {
  return next();
}