ContentBuilder.js · Guide

Using ContentBuilder.js in a Next.js Project

ContentBuilder.js is a JavaScript library for creating elegant, editorial-style web content with simple drag and drop. In this guide we add it to a Next.js app. First we embed the editor, then we add saving, uploads and a page that shows the saved content to visitors.

ContentBuilder comes in two parts:

ContentBuilder

The editor, used on the page where content is created.

ContentBuilder Runtime

Renders the saved content on your site. It makes interactive blocks (sliders, counters, charts, accordions and more) work, both while editing and when viewing.

In this guide

  1. Set up the project
  2. Embed the editor and the runtime
  3. Add saving and uploads
  4. Show the content to visitors
  5. Try it

Step 1

Set up the project

Create a Next.js project and install ContentBuilder and the Runtime:

npx create-next-app@latest my-app --ts --app --src-dir --eslint --no-tailwind --import-alias "@/*" --yes
cd my-app
npm install @innovastudio/contentbuilder @innovastudio/contentbuilder-runtime

The editor needs its assets (snippets, icons, fonts, plugins). They are in the ContentBuilder.js ZIP package, which you can download from your user area. Copy these into your project's public/ folder:

public/
├── assets/      # snippets, fonts, plugins
├── runtime/     # runtime files, used by the Live Preview page
└── preview.html # Live Preview page

Step 2

Embed the editor and the runtime

One thing first: the starter's globals.css removes the margin and padding of every element, which would change how your content looks. In src/app/layout.tsx, remove this line:

import "./globals.css";

Now create src/app/edit/page.tsx:

'use client'

import { useEffect } from 'react'

// Editor
import ContentBuilder from '@innovastudio/contentbuilder'
import '@innovastudio/contentbuilder/public/contentbuilder/contentbuilder.css'

// Runtime
import ContentBuilderRuntime from '@innovastudio/contentbuilder-runtime'
import '@innovastudio/contentbuilder-runtime/dist/contentbuilder-runtime.css'

export default function EditPage() {
    useEffect(() => {
        // Create the editor on the .container element
        const builder = new ContentBuilder({
            container: '.container',
        })

        // Load the blocks users can drag in
        builder.loadSnippets('/assets/minimalist-blocks/content-plugins.js')

        // Load the content to edit
        builder.loadHtml(`
            <div class="row">
                <div class="column">
                    <h1>Hello World</h1>
                    <p>Start editing your content here.</p>
                </div>
            </div>`)

        // Start the runtime, so interactive blocks work while editing
        const runtime = new ContentBuilderRuntime({
            pluginFolder: window.location.origin + '/assets/plugins/',
        })
        runtime.init()

        // Make the runtime available to the editor
        ;(window as Window & { builderRuntime?: ContentBuilderRuntime }).builderRuntime = runtime

        return () => {
            builder.destroy()
            runtime.destroy()
            delete (window as Window & { builderRuntime?: ContentBuilderRuntime }).builderRuntime
        }
    }, [])

    return <div className="container" style={{ maxWidth: '840px', margin: '140px auto', padding: '0 20px' }}></div>
}

The runtime makes interactive blocks (sliders, counters, charts, accordions and more) work while you edit. pluginFolder tells it where the plugins are. A plugin's files are loaded only when the content uses it.

'use client' makes the page run in the browser, where the editor works.

Run it:

npm run dev

Open http://localhost:3000/edit. You have a working editor: click the text to edit it, and drag blocks in from the panel on the right, including interactive ones like a countdown or a chart.

The ContentBuilder.js editor in a Next.js app after step 2, with Hello World

The editor after step 2. Click the text to edit it, and drag blocks in from the panel.

Step 3

Add saving and uploads

Now let's add saving and uploads. The editor saves to the server through three small API routes. In this guide the content is kept in a JSON file, data/content.json, so you can run it without a database.

Create src/app/api/load/route.ts:

import { readFile } from 'fs/promises'

export async function GET() {
    try {
        const data = JSON.parse(await readFile('data/content.json', 'utf8'))
        return Response.json({ html: data.html })
    } catch {
        return Response.json({ html: '' }) // nothing saved yet
    }
}

Create src/app/api/save/route.ts:

import { mkdir, writeFile } from 'fs/promises'

export async function POST(request: Request) {
    const { html } = await request.json()
    await mkdir('data', { recursive: true })
    await writeFile('data/content.json', JSON.stringify({ html }))
    return Response.json({ ok: true })
}

Create src/app/api/upload/route.ts, and an empty public/uploads/ folder for the files:

import path from 'path'
import { writeFile } from 'fs/promises'

export async function POST(request: Request) {
    const formData = await request.formData()
    const file = formData.get('file') as File | null
    if (!file) return Response.json({ error: 'No file received.' }, { status: 400 })

    const filename = path.basename(file.name).replaceAll(' ', '_')
    await writeFile(path.join('public/uploads', filename), Buffer.from(await file.arrayBuffer()))

    return Response.json({ url: '/uploads/' + filename }) // ContentBuilder needs { url }
}

Then replace src/app/edit/page.tsx:

'use client'

import { useEffect, useRef } from 'react'

// Editor
import ContentBuilder from '@innovastudio/contentbuilder'
import '@innovastudio/contentbuilder/public/contentbuilder/contentbuilder.css'

// Runtime
import ContentBuilderRuntime from '@innovastudio/contentbuilder-runtime'
import '@innovastudio/contentbuilder-runtime/dist/contentbuilder-runtime.css'

const DEFAULT_CONTENT = `
<div class="row">
    <div class="column">
        <h1>Hello World</h1>
        <p>Start editing your content here.</p>
    </div>
</div>`

// Sends the file to the upload route, which returns { url }
async function upload(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    const response = await fetch('/api/upload', { method: 'POST', body: formData })
    return response.json()
}

export default function EditPage() {
    const builderRef = useRef<ContentBuilder | null>(null)

    useEffect(() => {
        let cancelled = false

        // Create the editor
        const builder = new ContentBuilder({
            container: '.container',
            previewURL: 'preview.html', // enables the Live Preview button
            upload,                     // handles image, video, audio and file uploads
        })
        builderRef.current = builder

        // Load the blocks users can drag in
        builder.loadSnippets('/assets/minimalist-blocks/content-plugins.js')

        // The runtime makes interactive blocks work while editing
        const runtime = new ContentBuilderRuntime({
            pluginFolder: window.location.origin + '/assets/plugins/',
        })

        // Make the runtime available to the editor
        ;(window as Window & { builderRuntime?: ContentBuilderRuntime }).builderRuntime = runtime

        // Load saved content, or the default the first time
        fetch('/api/load')
            .then((response) => response.json())
            .then((data) => {
                if (cancelled) return
                builder.loadHtml(data.html || DEFAULT_CONTENT)
                runtime.init()
            })

        return () => {
            cancelled = true
            builder.destroy()
            builderRef.current = null
            runtime.destroy()
            delete (window as Window & { builderRuntime?: ContentBuilderRuntime }).builderRuntime
        }
    }, [])

    const save = async () => {
        if (!builderRef.current) return
        await fetch('/api/save', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ html: builderRef.current.html() }),
        })
    }

    return (
        <>
            <div className="container" style={{ maxWidth: '840px', margin: '140px auto', padding: '0 20px' }}></div>

            <div style={{ position: 'fixed', top: 20, right: 20, display: 'flex', gap: 8 }}>
                <button onClick={() => builderRef.current?.viewHtml()}>HTML</button>
                <button onClick={async () => { await save(); window.open('/view', '_blank') }}>Preview</button>
                <button onClick={async () => { await save(); alert('Content saved!') }}>Save</button>
            </div>
        </>
    )
}

What's new

  • The runtime now starts after the saved content is loaded.
  • Saving: html() returns the edited content, which is sent to the save route. loadHtml() loads it back from the load route the next time.
  • Uploads: upload is one function for all media. It sends the file to the upload route, which returns { url }.
  • Buttons: HTML opens the code view, Save saves, and Preview saves and opens the viewing page, which we add next.
  • Live Preview: previewURL adds a preview button to the editor, which uses public/preview.html.
The ContentBuilder.js editor in a Next.js app, with saved content loaded back in

Saved content loads back into the editor the next time you open it.

Step 4

Show the content to visitors

The viewing page is rendered on the server, so the content is already in the HTML that browsers and search engines receive. This is good for SEO. The page needs only the runtime, not the editor.

Create src/app/view/page.tsx:

import { Suspense } from 'react'
import { connection } from 'next/server'
import { readFile } from 'fs/promises'
import Runtime from './runtime'
import '@innovastudio/contentbuilder-runtime/dist/contentbuilder-runtime.css'

async function Content() {
    await connection() // render on every request, so the latest saved content shows

    let html = ''
    try {
        html = JSON.parse(await readFile('data/content.json', 'utf8')).html
    } catch {
        // nothing saved yet
    }

    return (
        <>
            <div
                className="is-container" // required for content styling
                style={{ maxWidth: '840px', margin: '80px auto', padding: '0 20px' }}
                dangerouslySetInnerHTML={{ __html: html || '<p>No content available.</p>' }}
            />
            <Runtime />
        </>
    )
}

export default function ViewPage() {
    return (
        <Suspense>
            <Content />
        </Suspense>
    )
}

The runtime runs in the browser, so it goes in a small client component. Create src/app/view/runtime.tsx:

'use client'

import { useEffect } from 'react'
import ContentBuilderRuntime from '@innovastudio/contentbuilder-runtime'

// Activates interactive blocks in the content rendered by the server
export default function Runtime() {
    useEffect(() => {
        const runtime = new ContentBuilderRuntime({
            pluginFolder: window.location.origin + '/assets/plugins/',
        })
        runtime.init()
        return () => runtime.destroy()
    }, [])

    return null
}

The server puts the content in the page, and then the runtime makes the interactive blocks work.

Important: the viewing container must have the is-container class.

Step 5

Try it

With npm run dev running, open http://localhost:3000/edit, drag in some blocks and click Save. Click Preview to see the saved content on the viewing page.

The Next.js viewing page showing content saved with ContentBuilder.js

The viewing page, rendered on the server, shows the saved content to visitors.

Going to production

  • Saving: replace the JSON file in the load and save routes and the viewing page with your database or CMS.
  • Uploads: send files to your storage (S3 or any other) and return { url }. After next build, Next.js serves only the files that were in public/ at build time, so new uploads saved there don't show.
  • Security: add authentication to the API routes, so only your editors can save and upload.

Resources

© 2026 InnovaStudio

This site is built with Site Builder Kit