# Integration Guide: Builder.io + MedusaJS Storefront

**URL:** <https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882>\
**Category:** Technical Questions\
**Tags:** integration, medusajs, medusa, app-router, nextjs\
**Created:** [February 11, 2025, 2:24pm UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882 "2025-02-11T14:24:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [February 11, 2025, 2:24pm UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/1 "2025-02-11T14:24:03Z")

</div>

**[MedusaJS](https://medusajs.com/)** is a powerful open-source e-commerce backend that provides flexibility for modern storefronts, while **[Builder.io](https://www.builder.io/)** is a visual CMS that enables no-code page creation and A/B testing. By integrating these two tools, you can build a dynamic, customizable storefront with the power of Medusa’s backend and [Builder.io](http://Builder.io)’s visual editing capabilities.

In this guide, I’ll walk you through how to integrate **[Builder.io](http://Builder.io) with MedusaJS** to create custom e-commerce pages seamlessly.

* * *

## 🛠 Prerequisites

Before getting started, ensure you have the following installed:

✅ **[Node.js v20+](https://nodejs.org/)**  
✅ **[Git CLI](https://git-scm.com/)**  
✅ **[PostgreSQL](https://www.postgresql.org/)** (Required for Medusa’s database)  
✅ **[Builder.io React SDK](https://www.npmjs.com/package/@builder.io/react)**

* * *

## 🚀 Step 1: Set Up MedusaJS

If you don’t already have a Medusa application installed, run the following command to install both the Medusa backend and the Next.js storefront starter:

```sh
npx create-medusa-app@latest --with-nextjs-starter

```

During setup, you’ll be prompted to enter a project name. Once the installation completes:

- The **Medusa backend** will be in the `{project-name}` directory.
- The **Next.js storefront** will be in `{project-name}-storefront`.

After installation, you can access:

🔹 **Medusa Admin Dashboard** : `http://localhost:9000/app`  
🔹 **Next.js Storefront** : `http://localhost:8000`

For additional setup details, refer to the [Medusa installation guide](https://docs.medusajs.com/learn/installation).

* * *

## 🏗 Step 2: Install [Builder.io](http://Builder.io) in Your Next.js Storefront

Assuming you are using **Next.js** for the storefront, install the required [Builder.io](http://Builder.io) packages:

```sh
npm install @builder.io/react @builder.io/sdk

```

For additional details, check out [Builder.io’s Next.js integration guide](https://www.builder.io/c/docs/integrating-builder-pages).

* * *

## 🧩 Step 3: Add a Builder Component

Create a new file at:  
📁 **`/src/modules/common/components/builder/index.tsx`** (Create the directory if it doesn’t exist).

Paste the following code inside:

```tsx
"use client";
import { ComponentProps } from "react";
import { BuilderComponent, useIsPreviewing } from "@builder.io/react"; 
import { builder } from "@builder.io/sdk";
import DefaultErrorPage from "next/error";

type BuilderPageProps = ComponentProps<typeof BuilderComponent>;

// Replace with your Builder.io Public API Key
builder.init("Your-Builder-Public-API-Key");

export function RenderBuilderContent(props: BuilderPageProps) { 
  const isPreviewing = useIsPreviewing(); 

  return props.content || isPreviewing ? (
    <BuilderComponent {...props} />
  ) : (
    <DefaultErrorPage statusCode={404} />
  );
}

```

* * *

## 📌 Step 4: Enable Dynamic Pages with [Builder.io](http://Builder.io)

### Add Builder Page Support

Create a file at:  
📁 **`/src/app/[countryCode]/(main)/[...page]/page.tsx`**

Paste the following code inside:

```tsx
import { Metadata } from "next";
import { builder } from "@builder.io/sdk";
import { RenderBuilderContent } from "@modules/common/components/builder";

// Initialize Builder.io
builder.init("Your-Builder-Public-API-Key");

export const metadata: Metadata = {
  title: "Builder.io + Medusa Next.js Starter Template",
  description: "A performant e-commerce starter template with Next.js 14 and Medusa.",
};

interface BuilderPageProps {
  params: {
    page?: string[];
    countryCode: string;
  };
}

export default async function BuilderPage({ params }: BuilderPageProps) {
  const { countryCode, page = [] } = params;
  const urlPath = `/${countryCode}/${page.join("/")}`.replace(/\/$/, "");

  const content = await builder
    .get("page", {
      userAttributes: { urlPath },
      options: { countryCode },
      locale: countryCode,
      prerender: false,
    })
    .toPromise();

  return <RenderBuilderContent content={content} model="page" locale={countryCode} />;
}

```

### Set Up Builder for the Home Page

If you want your **home page** (e.g., `http://localhost:8000/dk`) to be editable via Builder, modify:  
📁 **`/src/app/[countryCode]/(main)/page.tsx`**

Paste the following:

```tsx
import { Metadata } from "next";
import { builder } from "@builder.io/sdk";
import { RenderBuilderContent } from "@modules/common/components/builder";

import FeaturedProducts from "@modules/home/components/featured-products";
import { listCollections } from "@lib/data/collections";
import { getRegion } from "@lib/data/regions";

// Initialize Builder.io
builder.init("Your-Builder-Public-API-Key");

export const metadata: Metadata = {
  title: "Builder.io + Medusa Next.js Starter Template",
  description: "A performant e-commerce starter template with Next.js 14 and Medusa.",
};

interface HomeProps {
  params: {
    countryCode: string;
  };
}

export default async function Home({ params }: HomeProps) {
  const { countryCode } = params;
  const region = await getRegion(countryCode);
  const { collections } = await listCollections({ fields: "id, handle, title" });

  if (!collections || !region) {
    return null;
  }

  const content = await builder
    .get("page", {
      userAttributes: { urlPath: "/" },
      options: { countryCode },
      locale: countryCode,
      prerender: false,
    })
    .toPromise();

  return (
    <>
      <RenderBuilderContent content={content} model="page" locale={countryCode} />
      <div className="py-12">
        <ul className="flex flex-col gap-x-6">
          <FeaturedProducts collections={collections} region={region} />
        </ul>
      </div>
    </>
  );
}

```

* * *

## 🔗 Step 5: Configure [Builder.io](http://Builder.io) Preview

To enable **[Builder.io](http://Builder.io)’s Visual Editor**, follow these steps:

1. Go to [Builder.io Models](https://builder.io/models) and select the **Page** model.
2. Set the **Preview URL** to `http://localhost:<your-port>` (replace `<your-port>` with your app’s port).
3. Click **Save**.

This allows [Builder.io](http://Builder.io) to preview and edit your Next.js pages in real-time.

* * *

## 🎉 Conclusion

By integrating **MedusaJS with [Builder.io](http://Builder.io)**, you now have a flexible e-commerce storefront with **powerful backend capabilities** and **no-code page editing**. You can now:

✅ Manage products and orders via Medusa.  
✅ Customize page layouts with [Builder.io](http://Builder.io)’s drag-and-drop editor.  
✅ Implement dynamic content updates without touching code.

If you have any questions or run into issues, drop them in the comments! 🚀

---

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [February 11, 2025, 2:32pm UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/2 "2025-02-11T14:32:12Z")

</div>

If you’re experiencing **preview issues within the [Builder.io](http://Builder.io) app**, it’s likely due to Medusa Storefront’s `middleware.ts`, which includes certain redirect conditions. Here’s a breakdown of how the middleware works and how to fix the issue.

#### Why is the Preview Not Working?

The middleware in Medusa Storefront performs redirects based on specific conditions:

- It **redirects users** if a valid country code is missing or if they need to be directed to a country-specific region.
- If the `cacheId` cookie is missing but the **country code is present** , it will **redirect and set the cookie**.
- If no country code is specified in the URL, the middleware will **append the country code and redirect** the user.

### ✅ How to Fix It

To ensure [Builder.io](http://Builder.io)’s **Visual Editor preview** works correctly, you need to **modify the middleware** to **skip redirects** when previewing inside [Builder.io](http://Builder.io).

#### 🔹 Solution: Modify `middleware.ts`

Use `builder.preview` and ` __builder_editing__ ` to detect when the page is in **preview mode** and prevent unnecessary redirects. Below is a working example:

```auto
// Check if the URL has 'builder.preview' or ' __builder_editing__'
const isPreviewing =
  request.nextUrl.searchParams.has("builder.preview") ||
  request.nextUrl.searchParams.has(" __builder_editing__");

// If the page is in preview mode, skip redirect logic
if (isPreviewing) {
  return NextResponse.next();
}

```

**📌 Full working example:**  
Here’s the complete `middleware.ts` with the necessary fix applied: 👉 [Click to View Full Middleware Code](https://github.com/Manish-Builder-io/medusa-builder-storefront/blob/main/src/middleware.ts)

* * *

### 🔗 Additional Resources

For a hands-on reference, you can check out this **[Builder.io](http://Builder.io) + MedusaJS Starter Template** on GitHub:

> **[GitHub - Manish-Builder-io/medusa-builder-storefront](https://github.com/Manish-Builder-io/medusa-builder-storefront)**
>
> Contribute to Manish-Builder-io/medusa-builder-storefront development by creating an account on GitHub.

This should resolve your preview issues inside [Builder.io](http://Builder.io) while keeping Medusa’s region-based redirects intact. 🚀

Hope this helps! Let me know if you have any questions. 😊

---

<div class="post-metadata">

**Author:** ![Nando\_C](https://avatars.discourse-cdn.com/v4/letter/n/bbe5ce/32.png) [@Nando\_C](https://forum.builder.io/u/Nando_C)\
**Post date:** [February 12, 2025, 7:15am UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/3 "2025-02-12T07:15:15Z")

</div>

Hello @manish-sharma ,

I followed all the steps and seem to be getting an error about a deprecated feature. I’m unable to even open this in the visual editor.  
I have made sure to replace `"Your-Builder-Public-API-Key"` with my API key in the code examples.

Attached is the dev console photo

 ![Screenshot 2025-02-11 at 18.54.37](https://us1.discourse-cdn.com/flex020/uploads/builder/original/2X/2/203a40bc5b62b17e819da4aacaa25352e85b6e11.png)

Thanks,

---

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [February 12, 2025, 9:34am UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/4 "2025-02-12T09:34:25Z")

</div>

Hello @Nando_C,

Could you please share the integration code from **`/src/app/[countryCode]/(main)/page.tsx`**?

Additionally, if possible, please share a screen recording of your setup. This would help us better understand the issue.

---

<div class="post-metadata">

**Author:** ![Nando\_C](https://avatars.discourse-cdn.com/v4/letter/n/bbe5ce/32.png) [@Nando\_C](https://forum.builder.io/u/Nando_C)\
**Post date:** [February 13, 2025, 3:12am UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/5 "2025-02-13T03:12:03Z")

</div>

Hello @manish-sharma ,

For the code inside `/src/app/[countryCode]/(main)/page.tsx` I just replaced it with the code from the corresponding step in the original post above, as well as supplying my public API key.

Attached is a video of following the steps.

I am using node.js v22.13.1 and macOS Sequoia 15.3

> **[Screen Recording 2025-02-12 at 20.31.23.mov](https://drive.google.com/file/d/1r4DM32k5kTruJuWr-SUUQ1X7TfZ3OmyC/view?usp=sharing)**
>
> Google Drive file.

Thanks,

---

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [February 13, 2025, 3:57pm UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/6 "2025-02-13T15:57:10Z")

</div>

Hello @Nando_C,

We are unable to reproduce the issue you’re experiencing. Could you please share your app repository?

https://www.loom.com/embed/348ecc4ba3c44861b1058f37bc9e1f71?sid=faa0a1f1-62b6-4dd8-931d-f7937522fe21

---

<div class="post-metadata">

**Author:** ![Nando\_C](https://avatars.discourse-cdn.com/v4/letter/n/bbe5ce/32.png) [@Nando\_C](https://forum.builder.io/u/Nando_C)\
**Post date:** [February 13, 2025, 4:35pm UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/7 "2025-02-13T16:35:11Z")

</div>

Hello @manish-sharma ,

Here is the uploaded repository of what I tested:

> **[GitHub - Nando-Cordeiro/TestEcommerce](https://github.com/Nando-Cordeiro/TestEcommerce)**
>
> Contribute to Nando-Cordeiro/TestEcommerce development by creating an account on GitHub.

Thanks,

---

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [February 13, 2025, 6:02pm UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/8 "2025-02-13T18:02:26Z")

</div>

Hello @Nando_C,

Thanks for sharing the GitHub repo. I was able to recreate the issue you were experiencing, and it was caused by the way the URL was set in Builder.

You were adding `/dk` as part of the page URL, which is incorrect. For landing pages, the URL should be `/`, and for other pages, it should follow the format `/xyz`. The `/dk` in the URL represents the country code, and you don’t need to add it manually. Instead, you should use the **[dynamic preview URL](https://www.builder.io/c/docs/dynamic-preview-urls)** to handle country codes automatically.

```auto
return `http://localhost:8000/dk${targeting?.urlPath}`;

```

> **[Dynamic Preview URLs - Builder.io](https://www.builder.io/c/docs/dynamic-preview-urls)**
>
> Builder is the only Visual Development Platform that offers an AI-powered design-to-code tool, a visual editor, and an enterprise CMS.

To help clarify, I’ve recorded a Loom video demonstrating the issue and the fix. You can check it out here:

https://www.loom.com/embed/5d5e47ea9a7a412fb481d7082a8bfdb5?sid=51205c85-e71d-4b7a-9055-cba1b97a60f9

Hope this helps! Let me know if you have any questions.

Thanks,

---

<div class="post-metadata">

**Author:** ![manish-sharma](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@manish-sharma](https://forum.builder.io/u/manish-sharma)\
**Post date:** [February 13, 2025, 6:13pm UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/9 "2025-02-13T18:13:31Z")

</div>

@Nando_C,

Also, since you are not using builder locale feature, you may need to update your code **`/src/app/[countryCode]/(main)/[...page]/page.tsx`** as shown below and remove locale

```auto
export default async function BuilderPage({ params }: BuilderPageProps) {
    const { countryCode, page = [] } = params;
    const urlPath = `/${page.join("/")}`.replace(/\/$/, "");

    const content = await builder
        .get("page", {
            userAttributes: { urlPath },
            prerender: false,
        })
        .toPromise();

    return <RenderBuilderContent content={content} model="page" />;
}

```

---

<div class="post-metadata">

**Author:** ![Nando\_C](https://avatars.discourse-cdn.com/v4/letter/n/bbe5ce/32.png) [@Nando\_C](https://forum.builder.io/u/Nando_C)\
**Post date:** [February 23, 2025, 12:53am UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/10 "2025-02-23T00:53:25Z")

</div>

Forgot to follow up on this. It now all works as intended following all the steps above.

Thanks again,

---

<div class="post-metadata">

**Author:** ![Chetan](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@Chetan](https://forum.builder.io/u/Chetan)\
**Post date:** [September 25, 2025, 8:47am UTC](https://forum.builder.io/t/integration-guide-builder-io-medusajs-storefront/10882/11 "2025-09-25T08:47:49Z")

</div>

I am seeing below error when trying to deploy on vercel:

Error: ENOENT: no such file or directory, lstat ‘/vercel/path0/.next/server/app/[countryCode]/(main)/account/@dashboard/orders/details/[id]/page\_client-reference-manifest.js’

Potential cause seems to be Next.js version update to 15.3.1 from 15.0.3 used in yours.  
Can you please guide me how to fix it as I don’t want to loose functionalities by downgrading(eventhough I have tried downgrading Next.js to 15.0.3 along with [builder.io](http://builder.io) dependencies but it didn’t work still and I got the same error)??
