DEV Community

Cover image for How to Get Headless CMS in Next.js with One Line
Anmol Baranwal
Anmol Baranwal Subscriber

Posted on • Originally published at thebcms.com

32 13 11 15 16

How to Get Headless CMS in Next.js with One Line

Over the years, developers have realized the power of Headless CMS.

Using a solid CMS with frameworks like Next.js can make a lot of difference if you're managing a lot of content.

While big names dominate the space, today we will explore BCMS and how to use it with Next.js. Plus, some starter templates to get you started.

Let's jump in.


What is covered?

In a nutshell, we are covering these topics in detail.

  1. What is BCMS and why should you use it?
  2. A step-by-step guide on how to get started with BCMS and Next.js.
  3. Starter templates you can use.

If you want to explore yourself, please read the docs.


1. What is BCMS and why should you use it?

BCMS is one of the underrated options as a headless CMS for building Next.js projects. It even provides flexible content modeling for other tech stacks including Astro, Gatsby.js, Nuxt.js and Svelte.

You also get:

✅ Option for Global CDN.

✅ Component based content modeling.

✅ Flexible content for multilingual sites.

✅ 10+ input options with detailed permissions.

You can do all sorts of stuff like handling forms, HR tools, sending e-mails, custom reporting, all through BCMS using webhooks, functions and cron jobs.

bcms

 

🎯 Before we proceed, let's understand what Headless CMS means.

A headless CMS simply allows you to manage content in one place and deploy it on any digital channel.

In short, it's just any backend application, which is 100% built upon APIs. Headless means frontend less body which can be attached to any frontend or Head.

If you're interested to know more, you can check the article by StoryBlok and The Ultimate Guide to Headless CMS by the Builder.io team. It has visual diagrams, use cases, merits and a lot more.

headless cms vs traditional cms

 

Why should you use BCMS?

There are many useful features that make BCMS worth it. Let's explore some of them.

✅ BCMS lets you store content in flexible inputs like dropdowns, files, numbers and rich text, all built to work smoothly with Next.js applications.

input types

 

✅ You can easily upload and manage any media in your Next.js project using BCMS's organized folder system (much better than the others). Read the docs.

media manager

media in dashboard

media in dashboard

 

✅ You also get dynamic components for your blog or e-commerce site. They are called Widgets in BCMS which are reusable building blocks that are used inside of the entry's content area. Read the docs.

widgets

widgets in dashboard

widgets in dashboard

 

✅ There is also the concept of Groups which helps you to define reusable content structures, like author profiles or addresses, to be easily queried in your Next.js application.

You can use them in templates, widgets or even inside other groups. Read the docs.

groups

 

✅ To write multilingual content in BCMS, all you need to do is just click add and choose from nearly 100 languages.

English is the default and can’t be deleted, but other languages can be removed which will also remove all associated data with that language.

multilingual websites

 

✅ There is also the concept of entry which is like a single document or item created from a predefined template.

The template decides what details each entry should have. These details, called properties, appear in a section called meta. Every entry also has a content area where you can write text and add interactive elements.

For example, if you create a Blog template, you can set properties like author, category, tags, and publish date. Then, whenever you add a new blog post (entry), these fields will be ready to fill.

You can duplicate the entry, delete it and even set the entry status to indicate when a blog post is ready for publishing. Read the docs to know more.

entries

 

You can read about all the features on thebcms.com/nextjs-cms.

By the way, BCMS is open source with with hundreds of stars on GitHub. You can find jobs, plugins and more from the readme.

Now that we have understood the unique concepts of BCMS, it's time to integrate BCMS with Next.js in the next section.


2. A step-by-step guide on how to get started with BCMS and Next.js.

We will keep it simple and easy to understand even if you know nothing about the concepts. You can read the docs if you're interested in following everything from scratch.

The first thing you need to install is the BCMS CLI, by running the following command.

npx @thebcms/cli create next
Enter fullscreen mode Exit fullscreen mode

You will get the option to install the package and authenticate with BCMS in case you are not.

authorize bcms github

Once you log in, this is how the dashboard looks. It's available at app.thebcms.com/.

dashboard

This sets up a new Next.js project and lets you pick a starter template based on your needs. You can also start from scratch, but to keep things simple, I will go with the blog starter.

This will set up a new nextjs project and it will give you options to select a starter project (based on your use case). There is also the option to start from scratch but for the sake of easiness, I'm selecting the blog starter.

cli full

This is how the directory structure looks.

directory structure

In the root of your project, you will find bcms.config.cjs which is the main configuration file.


/**
* @type {import('@thebcms/cli/config').BCMSConfig}
*/

module.exports = {
    client: {
        orgId: process.env.BCMS_ORG_ID,
        instanceId: process.env.BCMS_INSTANCE_ID,
        apiKey: {
            id: process.env.BCMS_API_KEY_ID,
            secret: process.env.BCMS_API_KEY_SECRET,
        },
    },
};
Enter fullscreen mode Exit fullscreen mode

You will see the codebase pre-filled with some content, a .env file, and the necessary BCMS config.

In the dashboard, you can explore settings if you want to adjust functions, webhooks, dependencies or environment variables.

settings dashboard

settings dashboard

 

If you skip the starter template, you will need to create a new API Key under settings. You will get the option to allow necessary permissions once you create entries in the template. Don't worry if this sounds confusing, we will go over it soon.

api key

You just need to install the dependencies and run the server using the following commands.

npm i
npm run dev
Enter fullscreen mode Exit fullscreen mode

npm run dev

The bcms --pull types --lng ts command starts the BCMS CLI and pulls types from BCMS, saving them in /bcms/types/. These types work for both JavaScript and TypeScript.

Then, go to http://localhost:3000 to view the locally hosted version. Here's how it looks.

localhost deployed

Congrats! 🎉

You have just integrated BCMS with Next.js, it's that simple!

Let's study more about what is involved. Head over to the BCMS dashboard and look for the Templates option in the sidebar.

templates

You'll find a Blog template already created, with six properties: title, slug, date, description, cover_image, and category. Each property type is displayed as a badge (media, string, date).

six properties under template blog

You can easily update any properties based on your use case.

update any properties

You can also create a template from scratch as shown.

create template from scratch

create template from scratch

 

add properties in a template

add properties in a template

 

Once your template is ready, you will need to create entries for it. In this case, the Blog template already has four entries.

entries

Each entry contains all the necessary details.

each entry has necessary details

When creating a new entry, it will automatically include the required fields based on your template properties.

new entry

Similarly, for the cover image, just upload and select it accordingly.

media cover image

Once you refresh your local setup, the updates will appear.

new entry shown accordingly

The entry will be accessible at the route based on the slug URL you set in the dashboard.

glug blog inner page

Let's talk about the codebase.

First, the BCMS Client is initialized by creating bcms.ts inside the /src directory.

The main logic for fetching and displaying data from BCMS is in /src/app/page.tsx.

import React from 'react';
import { bcms } from './bcms-client';
import { BlogEntry, BlogEntryMetaItem } from '../../bcms/types/ts';
import { Metadata } from 'next';
import BlogCard from '@/components/blog/Card';
import Tag from '@/components/Tag';

const pageTitle = 'Blogs - Simple Blog';
export const metadata: Metadata = {
    title: pageTitle,
    openGraph: {
        title: pageTitle,
    },
    twitter: {
        title: pageTitle,
    },
};

const HomePage: React.FC = async () => {
    const blogs = (await bcms.entry.getAll('blog')) as BlogEntry[];

    const items = blogs.map((blog) => {
        return blog.meta.en as BlogEntryMetaItem;
    });
    return (
        <div className="py-24 md:py-32">
            <div className="container">
                <div className="flex flex-col gap-6 items-center text-center mb-20 md:mb-[120px]">
                    <Tag size="lg">Hi, Im Anmol 👋</Tag>
                    <h1 className="text-4xl font-bold leading-none md:text-5xl">
                        This is my blog
                    </h1>
                </div>
                <div>
                    <div className="grid grid-cols-1 gap-12 max-w-[1040px] mx-auto">
                        {items.map((item, index) => {
                            return <BlogCard key={index} blog={item} />;
                        })}
                    </div>
                </div>
            </div>
        </div>
    );
};

export default HomePage;
Enter fullscreen mode Exit fullscreen mode

The BlogPage route is defined in src/app/blog/[slug]/page.tsx.

Core components like ContentManager and Tag are inside the src/app/components directory.

It's simple to understand if you're aware of Next.js concepts.


3. Starter templates you can use.

Learning the concepts is great, but having everything pre-built for different use cases is amazing. Here are some starter templates that might save you time and effort.

Personal website

portfolio

This is one of the most decent portfolio templates I've come across. It’s clean, unique and well-structured, with some really good sections.

Built with Next.js and BCMS, this template offers a great starting point for developers. Read this tutorial if you want to explore everything that is involved.

You can check the GitHub Repository and live demo.

 

E-Commerce site

e-commerce

This is a next.js e-commerce template that comes with analytics, multilingual support and more.

If you want a detailed breakdown including templates, groups, elements involved and the step-by-step process to make this, read this guide.

You can check the GitHub Repository and live demo.

 

Podcast website

podcast website

If you're planning to launch a podcast or audio-focused platform, this template is a great choice.

The official tutorial is for Gatsby, but the GitHub repository provides a Next.js version as well.

You can check the GitHub Repository and live demo.

 

Job board

job board

This is for people looking to build a job board or a career page for an organization.

The official tutorial is for Nuxt but you can find the GitHub repository that includes a Next.js version as well.

You can check the GitHub Repository and live demo.

 

BCMS provides 10+ templates across different frameworks like Astro, Gatsby, Next and Nuxt. You can find all the samples on the official BCMS repository and the official website at thebcms.com/starters.

Other available templates include an agency website, job board, conference starter, recipe website and more. Each of these websites is really unique which makes things a lot better.

If you're exploring other options, you can check Sanity, Contentful, Payload and Strapi.


I'm aware that there are other decent options out there but BCMS has its own perks.

Underrated tools can sometimes be the best fit for your workflow.

Let me know if you have any other ideas or if you have used any Headless CMS options.

Have a great day! Until next time :)

You can check
my work at anmolbaranwal.com.
Thank you for reading! 🥰
profile of Twitter with username Anmol_Codes profile of GitHub with username Anmol-Baranwal profile of LinkedIn with username Anmol-Baranwal

Ending GIF waving goodbye

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (5)

Collapse
 
divya4879 profile image
Divya

This seems really helpful. Gotta try it out soon.
Thank you for this amazing article.

Collapse
 
anmolbaranwal profile image
Anmol Baranwal

Thanks for reading Divya! 🔥 BCMS is definitely powerful but you can also check out Payload which is next.js native and widely popular in this domain.

Collapse
 
divya4879 profile image
Divya

Thank you for sharing this one as well. I gotta check out both of them.

Collapse
 
komsenapati profile image
K Om Senapati

wow

Collapse
 
anmolbaranwal profile image
Anmol Baranwal

thanks man. time for you to code :)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay