Skip to content

Hostname Rewrites using Edge Functions

Learn how to programmatically create unique content pages for your users with a multi-tenant infrastructure using Edge Middleware.

Framework Next.js
Use Case Edge Functions, Documentation
CSS Tailwind
Publisher ▲ Vercel

Hostname Rewrites

If you're building a Platforms on Vercel, this example is for you.

In this example, you'll learn how to programmatically create unique content pages for your users with a multi-tenant infrastructure using Edge Middleware. Each user gets assigned a unique subdomain when they create their account, with the (usually paid) option to add a custom domain.

For context, here are some example pages:

All of these generated sites are powered by ISR (no SSR at all) so they load pretty much instantly + the inter-page transitions are lightning fast.

The example above is generated based on the following mock database:

const mockDB = [
    {name: "Site 1", description: "Subdomain + custom domain", subdomain: "subdomain-1", customDomain: "custom-domain-1.com"},
    {name: "Site 2", description: "Subdomain only", subdomain: "subdomain-2", customDomain: null},
    {name: "Site 3", description: "Subdomain only", subdomain: "subdomain-3", customDomain: null},
]

When deploying your own clone of this example, you will need to replace the data fetching methods in getStaticPaths and getStaticProps with your own database of choice (BYOD, Bring-Your-Own-Database).

To give a bit of context of how this can be applied in a real-world context, we recently launched the Platforms Starter Kit – a comprehensive template for site builders, multi-tenant platforms, and low-code tools:

For more info on the Platforms Starter Kit:

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:

Clone and Deploy

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example https://github.com/vercel/examples/tree/main/edge-functions/hostname-rewrites hostname-rewrites
# or
yarn create next-app --example https://github.com/vercel/examples/tree/main/edge-functions/hostname-rewrites hostname-rewrites

.env has the following defaults:

ROOT_DOMAIN=vercel.app
DEFAULT_DEV_HOST=subdomain-1

Feel free to change ROOT_DOMAIN to your domain if your hostnames are not under .vercel.app. DEFAULT_DEV_HOST is the default hostname for development in localhost.

Next, run Next.js in development mode:

npm install
npm run dev

# or

yarn
yarn dev

Deploy it to the cloud with Vercel (Documentation).

💡 Do note that you will need to replace the ROOT_DOMAIN variable in .env.local with your domain of choice and add that domain as a wildcard domain to your Vercel project.

Hostname Rewrites using Edge Functions

Learn how to programmatically create unique content pages for your users with a multi-tenant infrastructure using Edge Middleware.

Framework Next.js
Use Case Edge Functions, Documentation
CSS Tailwind
Publisher ▲ Vercel

Hostname Rewrites

If you're building a Platforms on Vercel, this example is for you.

In this example, you'll learn how to programmatically create unique content pages for your users with a multi-tenant infrastructure using Edge Middleware. Each user gets assigned a unique subdomain when they create their account, with the (usually paid) option to add a custom domain.

For context, here are some example pages:

All of these generated sites are powered by ISR (no SSR at all) so they load pretty much instantly + the inter-page transitions are lightning fast.

The example above is generated based on the following mock database:

const mockDB = [
    {name: "Site 1", description: "Subdomain + custom domain", subdomain: "subdomain-1", customDomain: "custom-domain-1.com"},
    {name: "Site 2", description: "Subdomain only", subdomain: "subdomain-2", customDomain: null},
    {name: "Site 3", description: "Subdomain only", subdomain: "subdomain-3", customDomain: null},
]

When deploying your own clone of this example, you will need to replace the data fetching methods in getStaticPaths and getStaticProps with your own database of choice (BYOD, Bring-Your-Own-Database).

To give a bit of context of how this can be applied in a real-world context, we recently launched the Platforms Starter Kit – a comprehensive template for site builders, multi-tenant platforms, and low-code tools:

For more info on the Platforms Starter Kit:

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:

Clone and Deploy

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example https://github.com/vercel/examples/tree/main/edge-functions/hostname-rewrites hostname-rewrites
# or
yarn create next-app --example https://github.com/vercel/examples/tree/main/edge-functions/hostname-rewrites hostname-rewrites

.env has the following defaults:

ROOT_DOMAIN=vercel.app
DEFAULT_DEV_HOST=subdomain-1

Feel free to change ROOT_DOMAIN to your domain if your hostnames are not under .vercel.app. DEFAULT_DEV_HOST is the default hostname for development in localhost.

Next, run Next.js in development mode:

npm install
npm run dev

# or

yarn
yarn dev

Deploy it to the cloud with Vercel (Documentation).

💡 Do note that you will need to replace the ROOT_DOMAIN variable in .env.local with your domain of choice and add that domain as a wildcard domain to your Vercel project.

Unleash New Possibilities

Deploy your app on Vercel and unlock its full potential