Make Personalized Content Fast and Easier with Vercel Edge Functions and Ninetailed
Tags: webdev, tutorial, nextjs, webperf
Published: 2021-10-26 16:40:13




Before diving into the edge functions, let's start talking about edge computing and how we came to edge computing. 

For the last years, everyone is started talking about the edge. It became the next big thing after cloud, IoT, and blockchain.

In the beginning, the first computers were big, isolated computers that people could only access directly. Then we had personal computers. Personal computers could do computing in a much more distributed manner. At that time, data was saved on a user's computer locally.

Next, cloud computing came into our lives. [Cloud services](https://www.redhat.com/en/topics/cloud-computing/what-are-cloud-services) are infrastructure, platforms, or software that are hosted by third-party providers and made available to users through the internet.

However, due to the distance between clients and the data centers where cloud services are hosted, cloud computing can cause delays. For this reason, edge computing entered our lives.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zmsrkt8uixbigzrtf5m.png)

> [Edge computing](https://www.cloudflare.com/en-gb/learning/serverless/glossary/what-is-edge-computing/) is a networking philosophy focused on bringing computing as close to the source of data as possible in order to reduce latency and bandwidth use. In simpler terms, edge computing means running fewer processes in the cloud and moving those processes to local places, such as on a user's computer, an IoT device, or an edge server. Bringing computation to the network's edge minimizes the amount of long-distance communication that has to happen between a client and server.
> 

In summary:

1. **First Computing:** One centralized computer running applications and storing data locally
2. **Personal Computing:** Decentralized computers running applications and storing data locally
3. **Cloud Computing:** Centralized computers running applications and storing data in data centers
4. **Edge Computing:** Centralized computers running applications and storing data close to users - either on the device itself or on the network edge

## What are Edge Functions

Edge Functions are the solution to bring edge computing to run JavaScript code at the edge before a request is processed, streamlining continuous delivery between backend developers and frontend developers to improve time to market.

This enables development teams to more easily deliver a number of features such as A/B testing, custom authentication, dynamic routing, personalization, and more.

Edge Functions are aimed to make the developer experience easier by allowing you to:

- simplify experimentation
- customize authentication
- personalize web content
- enhance security

## The Main Benefits of Edge Functions:

### Lower Latency

Lower latency is the major benefit of edge functions when it comes to improving the overall performance of your products.

Users of all web apps will experience delays when they face processes requiring communication with an external server. The length of these delays will vary depending on the server's available bandwidth and location, but they can be eliminated entirely by moving more operations to the edge.

Because Edge Functions run on the content delivery network edge node closest to the user, they have lower latency than other options.

### Edge SEO

> [Edge SEO](https://dantaylor.online/edge-seo/) is a field of technical SEO that utilizes serverless (edge) technologies to execute changes to code through using the CDN as a form of middle-ground in which SEO hotfixes and recommendations can be implemented. These changes are agnostic of the website infrastructure (origin source code) and are supported by multiple CDNs through "edge functions" technology.
> 

As it's described earlier, edge functions close the gap between the user and the data source. 

Using the edge functions in the website will bring a couple of benefits, such as: 

- speed
- improved site security
- possibility to perform A/B testing

## Use Cases for Edge Functions

Basically, you can do anything and everything with edge functions. Since you'll be writing code, possibilities will be infinite. Here are just a few use cases for edge functions:

1. **Experimentation:** Use edge functions to process and analyze customers' data at the edge in real-time. ****
2. **Personalization:** Increase conversion rates by delivering content that automatically tailors based on each user's location and other signals.
3. **Security:** Distribute your data across the edge locations with edge functions to distribute and reduce the risk as well.
4. **Authentication:** Deliver fast authentication with edge functions by caching and verifying credentials at the edge.

## Personalization With Vercel Edge Functions and Ninetailed

Ninetailed is an [API-first optimization platform](https://ninetailed.io) that works with Contentful and other headless CMS platforms. It includes SDKs for Next.js and Vercel Edge Functions to make it simple to integrate personalization on the edge.

This short guideline walks you through setting up and integrating Ninetailed and Next.js with Edge Functions. In this guide, you'll find:

- How to install the SDK for Next.js with Edge Functions
- Steps you need to follow to personalize components in Next.js

### Step 1: Install Ninetailed SDKs for Next.js and Edge Functions

With the Next.js SDK, you can add dynamic content for personalization to any component. In this step, you need to install Ninetailed SDKs for Next.js

**Install module via yarn:**

```tsx
yarn add @ninetailed/experience-sdk-nextjs @ninetailed/experience-sdk-nextjs-ssr @ninetailed/experience-sdk-nextjs-esr
```

### Step 2. Add Ninetailed to the middleware

In the folder pages add the SDKs to the `_middleware.ts` file.

```tsx
import { EdgeRequest, EdgeResponse } from 'next'
import {
NinetailedPersonalizationMiddleware
} from '@ninetailed/experience-sdk-nextjs-esr'

// Add the Personalization Middleware

export default async (req: EdgeRequest, res: EdgeResponse) => {
await NinetailedPersonalizationMiddleware({ apiKey: process.env.NINETAILED_API_KEY })(req, res)
}
```

### Step 3. Add Ninetailed to the app

Now, in the same folder, let's add the SDKs to the `app.ts` file.

```tsx
import type { AppProps } from 'next/app'
import {
PersonalizationProvider
} from '@ninetailed/experience-sdk-nextjs'
import { ninetailedSSRTrackerPlugin } from '@ninetailed/experience-sdk-ssr'
import { selectNinetailedProfile } from '@ninetailed/experience-sdk-nextjs-esr'

// Add the Personalization Provider

export default function MyApp({ Component, pageProps }: AppProps) {
const profile = selectNinetailedProfile(pageProps)

return <PersonalizationProvider
plugins={[ninetailedSSRTrackerPlugin()]}
profile={profile}
apiKey={process.env.NINETAILED_API_KEY}>
<Component {...pageProps} />
</PersonalizationProvider>
}
```

### Additional Steps

In the documentation of Ninetailed, you can also find a more detailed *[Quick Start Guide for Developers](https://docs.ninetailed.io/getting-started/quick-start-for-developers-next)* with additional steps such as:

- How to [set up Ninetailed App on Contentful](https://docs.ninetailed.io/cms/contentful/installation)
- How to use Ninetailed [Profiles in Next.js](https://docs.ninetailed.io/frameworks/next/profile)
- How to integrate [Google Analytics with Ninetailed](https://docs.ninetailed.io/integrations/google-analytics)

## The Bottom Line

Vercel Edge Functions provide a completely new way to run JavaScript code at the edge prior to processing a request. They make it easier to deliver features like A/B testing, custom authentication, dynamic routing, and personalization without affecting latency, improving the web core vitals and SEO.

This is why personalization on the edge with Edge Functions and Ninetailed is the way to go to create personalized experiences and improve the overall user experience and SEO.