Livestream: Best practices for building with GenUI | 5/22

Announcing Visual Copilot - Figma to production in half the time

Builder logo
builder.io

Livestream: Best practices for building with GenUI | 5/22

Announcing Visual Copilot - Figma to production in half the time

Blog

×

Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required

Resources

Blog

Get StartedLogin

As a developer, you can integrate Builder into your codebase and give other team members, such as content editors, marketers, and designers, the ability to build and manage pages without ever having to ping you.

This guide is detailed. If you're using React, Next.js, Remix, Angular with standalone components, or Qwik and you'd prefer a more automated process, visit Developer Quickstart, which uses Devtools to integrate for you.

We highly recommend that you integrate Builder into an existing app, and these instructions assume you already have one. However, if you need to create an app, and would like instructions, visit Creating an App.

At the command line, use npm to install the Builder SDK:

npm install @builder.io/sdk-vue

If you're using Windows, be sure to use quotes when you install dependencies that use the @ operator; for example, npm install "@builder.io/sdk-vue".

Add the following to nuxt.config.js:

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  modules: ['@builder.io/sdk-vue/nuxt'],
});

Start the development server:

npm run dev

If your app doesn't have it, create pages/[...app].vue and paste in the following code.

<!-- pages/[...app].vue -->

<script setup>
import { Content, fetchOneEntry, isPreviewing } from '@builder.io/sdk-vue';
import { ref } from 'vue';

const route = useRoute();

// TO DO: Add your Public API Key here
const apiKey = /* your API key here */;
const canShowContent = ref(false);
const model = 'page';

const { data: content } = await useAsyncData('builderData', () =>
  fetchOneEntry({
    model,
    apiKey,
    userAttributes: {
      urlPath: route.path,
    },
  })
);

canShowContent.value = content.value ? true : isPreviewing(route.path);
</script>

<template>
  <div v-if="canShowContent">
    <Content :api-key="apiKey" :model="model" :content="content" />
  </div>
  <div v-else>Content not Found</div>
</template>

A later section of this tutorial covers getting your Public API Key.

The template conditionally renders content based on the value of canShowContent. If canShowContent is true, it displays the page title and the <Content> component with the provided props. If canShowContent is false, it displays a Content not Found message.

The key to the template is <Content>, which takes several props:

  • :model: the name of the content’s Builder model
  • :content: bound to the content data
  • :apikey: bound to the apiKey data

If the value of $route.path matches a the URL path of a Builder page, <Content> renders that page. Otherwise, the template renders a not found error.

For more details on <Content>, visit Using the Content Component.

Builder adds the ability for your team members–even those who don't code–to create and iterate on ideas with a drag-and-drop interface.

Go to Builder.io to sign up for an account if you don't already have one. Come back when you're logged in.

This next video covers demonstrates the entire process of following this tutorial. The end result is a local application connected to Builder. This example uses Next.js but all frameworks use the same process.

To connect your Builder.io space to your application, add the Public API key to your code.

Find your Public API Key within Builder.io in one of two ways:

  • Press Cmd/Ctrl+k in Builder to bring up the command palette and search for API Key. Clicking the Public API Key copies it to your clipboard.
  • Go to Settings and copy your Public API Key.

For more detail about the Public API Key, see Using Builder API Keys.

Paste the Public API Key into your app by replacing the YOUR_API_KEY placeholder in your code. This location varies depending on your framework. Make sure to wrap your Public API Key in quotes.

The following video demonstrates how to copy your Public API Key from Settings and paste it into a Next.js app.

To enable Builder to open your site in the Visual Editor, provide a URL that Builder can open which has the Builder rendering component in it.

  1. Go to Models and choose the Page model.
  2. Set the Preview URL to http://localhost:<your-port>, where <your-port> is the port your app is using. Be sure to include the http://.
  3. Click Save.

The following video shows these steps:

Create a Builder Page to use with your integrated app.

  1. Go to Content.
  2. Click the + New Entry button near the top right of the screen.
  3. Create a new Page in Builder.
  4. The Editor for your new Page loads automatically.
  5. In your new Page, drag in Builder blocks.
  6. Click the Publish button in the upper right of the browser.
  7. After a few moments, your page will be available on localhost. Click the right arrow icon next to the Publish button and choose View live page to visit your page.

The next video shows creating a Page in a Builder-integrated app and dragging in an Image and Text block.

After you deploy your updates, be sure to update this to a public URL, such as your live site or your staging site; for example, https://your-site.com, so anyone on your team can connect to your site for visual editing.

If you're getting a 404 but aren't sure why, check these things:

  • Make sure you've published your page in Builder by clicking the Publish button on the upper right corner.
  • Check the URL. If you name the page test2 for example, Builder adds a hyphen, so that the URL segment is test-2.
  • Make sure that you've set the preview URL on the Page Model.
  • For some frameworks, you might have to restart the dev server.

For more information regarding special use cases, visit Integration Tips.

We recommend that you place your Builder pages between your header and footer. A common use case is developers keeping headers and footers in their codebase while integrating page building. In this way, non-developer team members can iterate on pages, without having to rely on developers.

To use Builder pages between your header and footer, follow the guidance below for your framework:

In _.vue, import your header and footer along with the other JavaScript imports in page.jsx.

Add the header and footer components before and after Content.

  <template>
  <div id="home">
    <div>Hello world from your Vue project. Below is Builder Content:</div>

    <div v-if="canShowContent">
      <div>
        Page title:
        {{ (content && content.data && content.data.title) || 'Unpublished' }}
      </div>
+     <MyHeader />
      <Content
        model="page"
        :content="content"
        :api-key="apiKey"
      />
    </div>
    <div v-else>Content not Found</div>
+     <MyFooter />
  </div>
</template>

<script>
import Vue from 'vue'
import { Content, getContent, isPreviewing } from '@builder.io/sdk-vue'
+ import MyHeader from '../components/my-header'
+ import MyFooter from '../components/my-footer'


// TODO: enter your public API key
const BUILDER_PUBLIC_API_KEY = 'YOUR_API_KEY' 

export default Vue.extend({
  name: 'DynamicallyRenderBuilderPage',
- components: { Content },
+ components: { Content, MyHeader, MyFooter },
  data: () => ({
    canShowContent: false,
    content: null,
    apiKey: BUILDER_PUBLIC_API_KEY,
  }),
  mounted() {
    // Re-run this check on the client in case of SSR
    this.canShowContent = this.content || isPreviewing()
  },
  async fetch() {
    const content = await getContent({
      model: 'page',
      apiKey: BUILDER_PUBLIC_API_KEY,
      userAttributes: {
        urlPath: this.$route.path,
      },
    })
    this.canShowContent = content || isPreviewing()
    this.content = content

    if (!this.canShowContent) {
      if (this.$nuxt.context?.ssrContext?.res) {
        this.$nuxt.context.ssrContext.res.statusCode = 404
      }
    }
  },
})
</script>

With your app and Builder working together, the next step is the fun part–add some pages in Builder and drag in some elements. Play with styles and explore the UI.

Was this article helpful?

Get the latest from Builder.io