High performance personalization & a/b testing example using Next.js, Edge Middleware, and Builder.io

Overview

Next.js + Builder.io Personalization & A/B Testing with Edge Middleware

Deploy with Vercel

This is a fork of Next.js Commerce with Builder.io integrated and using Edge Middleware to personalize pages with targeting and a/b testing with great performance.

Try it here

Video demo of personalized site

Contents

Overview

View an in depth overview of Builder.io + Vercel Edge Functions here

Video Thumbnail for the in depth overview of Builder.io + Vercel Edge Functions

How this works

Diagram of the personalization strategy

In short, to personalize a page, we rewrite a basic path to a path + a hash of personalized attribtues.

For instance, if you have a pages/page.tsx, you can rename it to pages/page/[hash].tsx, and add it to be included into the rewriting middleware to add the personalized hash at the end.

You can now have your page return personalized content based on what is in the hash, and this is purely automatic via what personalization.* cookies are present

You can set them at any time to target off of, for instance:

Cookies.set('personalization.returnVisitor', 'true')

And now your /page will be rewritten to /page/returnVisitor=true when that cookie is present, and be handled accordingly

Then, you can visually create and edit pages, sections, and data on your site with Builder.io's visual editor, dragging and dropping with your React components and publish unlimited variations to your site without any code or deploys needed:

Example of editing in Builder.io

Get Started

Run Locally

Coone this repo

git clone https://github.com/BuilderIO/nextjs-edge-personalization-demo.git
cd nextjs-edge-personalization-demo

Run the dev server

Next, from the project root:

yarn # run this command in root folder of the mono repo
yarn dev

Connect Builder.io

To connect Builder.io, create a free account, from your account settings copy your Public API Key and paste it into site/config/builder.ts

Screenshot of copying your API key

Set your Preview URL

Next, head to builder.io/models and choose the page model and enter http://localhost:3000 as the preview URL, and then hit save at the top right of the page.

Screenshot of adding your Preivew URL in Builder.io

Configure personalization attributes

Now, in Builder.io add some targeting attributes to begin personalizing your content on. Head to builder.io/account and click on the pencil next to Custom targeting attributes and add some attributes, for instance like you see here:

Screenshots of the targeting UI configuration

Above, we create an attribute called returnVisitor that is a boolean that we set here so it can be read by edge middleware, as well as an attribute called audience that is an enum of a couple made up segments like shirt-shipper and jacket-shopper that we set here via browsing history (could alternatively be set upon add to cart or other actions).

Note: generally audiences should come from a CDP, like

myCdp.getAudiences().then((audiences) => {
  // assuming audiences is an array of strings, like ['shirt-shipper', 'jacket-shopper']
  Cookies.set('personalization.audience', JSON.stringify(audiences))
})

Create a page

Now, go to builder.io/content and choose the + new button, choose page, and drag and drop to create a page on your Next.js site with your React components!

Image of creating a page and editing visually

⚠️ If you are having trouble connecting: be sure you are running your dev server on http://localhost:3000 by running yarn dev from this project root like shown above

Target and publish your page

At the top of the editor, you can configure which audiences should see this page variant, and publish.

Screenshots of targeting your page

Deploy to Vercel

To get your site live, you can deploy it to Vercel. Be sure to use the following configuration:

  • Framework preset: Next.js
  • Build command: cd .. && yarn build

Screenshot of the Vercel configuration

Once you deploy to vercel, you can update your Preview URL to use the live URL so now others on your team can create content on your new site with Builder.io.

E.g. if your site is now live at https://my-site.vercel.app, you can go back to builder.io/models, choose the page model, and update your Preview URL to https://my-site.vercel.app and save.

Screenshot of updating your Preview URL

Explore the code

Some key places to look in the codebase:

Diagram of the tech stack used

Next Steps

Learn more about

Troubleshooting

Run into any issues? Chat with us in discord!

You might also like...

Example project using Miniflare, esbuild and AVA

Miniflare Example Project This is an example Cloudflare Workers project that uses Miniflare for local development, esbuild for bundling, and AVA for t

Dec 3, 2022

React UI Components for macOS High Sierra and Windows 10

React UI Components for macOS High Sierra and Windows 10. npm install react-desktop --save Help wanted! I am looking for developers to help me develop

Dec 24, 2022

Free Open Source High Quality Dashboard based on Bootstrap 4 & React 16: http://dashboards.webkom.co/react/airframe

Free Open Source High Quality Dashboard based on Bootstrap 4 & React 16: http://dashboards.webkom.co/react/airframe

Airframe React High Quality Dashboard / Admin / Analytics template that works great on any smartphone, tablet or desktop. Available as Open Source as

Jun 5, 2022

Vue-hero-icons - A set of free MIT-licensed high-quality SVG icons, sourced from @tailwindlabs/heroicons, as Vue 2 functional components.

vue-hero-icons For Vue3, install the official package @heroicons/vue A set of free MIT-licensed high-quality SVG icons, sourced from @tailwindlabs/her

Dec 16, 2022

Next-multipart - Easy & Simple File Uploads for Next.js

Next-Multipart Next-multipart is a small utility library to ease the process of file uploads with Next.js. It uses formidable under the hood, but with

Nov 11, 2022

Sandbox for developing and testing UI components in isolation

Sandbox for developing and testing UI components in isolation

React Cosmos Sandbox for developing and testing UI components in isolation. Visual TDD. Develop one component at a time. Isolate the UI you're working

Jan 3, 2023

JavaScript Testing utilities for React

Enzyme Enzyme is a JavaScript Testing utility for React that makes it easier to test your React Components' output. You can also manipulate, traverse,

Dec 28, 2022

UI for Keploy platform that does API testing by dependency mocking without writing any test cases.

UI for Keploy platform that does API testing by dependency mocking without writing any test cases.

Welcome to Keploy UI 👋 About Us Keploy is a no-code API testing platform. Keploy automatically generates unit test cases with dependency mocks from A

Dec 28, 2022

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

we are make our components in story book. So if we add some components you can find document and example of useage in storybook.

Aug 12, 2022
Comments
  • add query params blog post link to readme

    add query params blog post link to readme

    This PR:

    • Adds link to new blog post in the Next Steps section
    • Adds some punctuation
    • Fixes a typo
    • Replaces an e.g. with its English equivalent for clarity in cases of readers less familiar with Latin terms.
    opened by kapunahelewong 3
  • fix: now build works

    fix: now build works

    Description

    Had to explicitly add the Netlify Next.js runtime because for some reason, the monorepo wasn't detecting it was a Next.js project.

    You'll know it's working if you see @netlify/plugin-nextjs (onBuild event) in the build logs.

    opened by nickytonline 2
🐐 Simple and complete React DOM testing utilities that encourage good testing practices.

React Testing Library Simple and complete React DOM testing utilities that encourage good testing practices. Read The Docs | Edit the docs Table of Co

Testing Library 17.3k Jan 4, 2023
React + Redux starter kit / boilerplate with Babel, hot reloading, testing, linting and a working example app built in

A comprehensive starter kit for rapid application development using React. Why Slingshot? One command to get started - Type npm start to start develop

Cory House 9.8k Dec 22, 2022
A lightweight Apple Music client for Windows, built with MusicKit JS, Edge WebView2 and React.

Lito Music English | 中文 Lito (/laɪto/) Music is a lightweight Apple Music client for Windows, built with MusicKit JS, Edge WebView2 and React. System

Jiahao Lu 113 Dec 30, 2022
A Drag & Drop Form Builder base on Bootstrap v4.x

bsFormBuilder 一个基于 Bootstrap (v4.x) + JQuery 的、拖拽的表单构建工具。 特点 1、基于 Bootstrap (v4.x) + JQuery,简单易用 2、拖动的 html 组件,支持通过 Json 自定义扩展 3、组件的属性面板,支持通过 Json 自定义

Michael Yang 10 Aug 25, 2022
Next.js + Prisma + Supabase simple Blog example

Next.js + Prisma + Supabase Blog Project About the Project Prisma connects the PostgreSQL Database provided by Supabase by using postgres connection s

Soham Shah 4 Nov 19, 2022
IngredientRecipeFinder - Web app built with react using Edamam API to find any recipe for an ingredient search with nutrition filters (high protein, low carb,etc)

Ingredient Recipe Finder Web app This web app built with the use of Edamam API allows you to find any type of recipe for a specific ingredient you are

null 1 Jan 4, 2022
:fire: A highly scalable, offline-first foundation with the best developer experience and a focus on performance and best practices.

Start your next react project in seconds A highly scalable, offline-first foundation with the best DX and a focus on performance and best practices Cr

react-boilerplate 28.9k Jan 6, 2023
This compress library was made with Brotli and Gzip help, for React users who want to make website more performance and reduce JS bundle code

React-compress This compress library was made with Brotli and Gzip help, for React users who want to make website more performance and reduce JS bundl

Koma Human 30 Jan 6, 2023
Chat Loop is a highly scalable, low-cost, and high performant chat application built on AWS and React leveraging GraphQL subscriptions for real-time communication.

Chat Loop Chat Loop is a highly scalable, low cost and high performant chat application built on AWS and React leveraging GraphQL subscriptions for re

Smile Gupta 24 Jun 20, 2022
Further split the React Native code based on Metro build to improve performance, providing `Dll` and `Dynamic Imports` features

React-Native Code Splitting Further split the React Native code based on Metro build to improve performance, providing Dll and Dynamic Imports feature

Wuba 126 Dec 29, 2022