Logo
HomeAboutServicesBlogsContact

Why Headless CMS is the Future of Content Management

By Xubrtech Team
Xubrtech Team's picture

In the era of modern web development, Headless CMS solutions are gaining immense popularity due to their flexibility, scalability, and developer-friendly approach. One such powerful option is Sanity CMS, which provides real-time collaboration, structured content modeling, and API-first content management. In this article, we will explore the benefits of using Sanity CMS for your blog and how to integrate it seamlessly with a front-end framework like Next.js or React.

What is Sanity CMS?

Sanity is a headless CMS that enables developers to manage content efficiently and deliver it via APIs. Unlike traditional CMS platforms like WordPress, a headless CMS separates content management from content presentation, allowing for greater customization and multi-platform distribution.

Key Features of Sanity CMS

  • Real-time collaboration – Multiple users can work simultaneously.
  • Structured content – Uses JSON-based schema for better organization.
  • Content versioning – Track changes and revert to previous versions.
  • GraphQL & REST API support – Fetch content seamlessly.
  • Customizable Studio – Create tailored editorial experiences.
  • Image Optimization – Built-in asset pipeline for efficient media management.

Setting Up a Blog with Sanity CMS

Step 1: Install Sanity CLI

To start, you need to install Sanity’s CLI tool. Open your terminal and run:

1npm install -g @sanity/cli

Step 2: Initialize a Sanity Project

Create a new project and set up your Sanity Studio:

1sanity init

Follow the prompts to configure your project, including selecting a dataset and defining a name for your CMS.

Step 3: Define a Blog Post Schema

Inside the schemas folder in your Sanity project, create a post.js file:

1import blockContent from './blockContent';
2
3export default {
4  name: 'post',
5  title: 'Post',
6  type: 'document',
7  fields: [
8    {
9      name: 'title',
10      title: 'Title',
11      type: 'string',
12    },
13    {
14      name: 'slug',
15      title: 'Slug',
16      type: 'slug',
17      options: { source: 'title', maxLength: 96 },
18    },
19    {
20      name: 'body',
21      title: 'Body',
22      type: 'blockContent',
23    },
24    {
25      name: 'mainImage',
26      title: 'Main Image',
27      type: 'image',
28    },
29    {
30      name: 'publishedAt',
31      title: 'Published at',
32      type: 'datetime',
33    }
34  ],
35};

Step 4: Define Block Content Schema

Create a blockContent.js file inside the schemas folder:

1export default {
2  title: 'Block Content',
3  name: 'blockContent',
4  type: 'array',
5  of: [
6    {
7      type: 'block',
8    },
9    {
10      type: 'image',
11      options: { hotspot: true },
12    },
13    {
14      type: 'object',
15      name: 'customQuote',
16      title: 'Custom Quote',
17      fields: [
18        {
19          name: 'text',
20          title: 'Quote Text',
21          type: 'string',
22        },
23        {
24          name: 'author',
25          title: 'Author',
26          type: 'string',
27        },
28      ],
29    },
30  ],
31};

Step 5: Deploy Sanity Studio

Once your schema is defined, start the Sanity Studio:

1sanity start

This launches a local CMS dashboard where you can create and manage blog posts.

Step 6: Fetch Content in Next.js

To display the content in a Next.js application, install the Sanity client:

1npm install @sanity/client

Then, create a utility file to connect with Sanity’s API:

1import sanityClient from '@sanity/client';
2
3export const client = sanityClient({
4  projectId: 'your_project_id',
5  dataset: 'production',
6  useCdn: true,
7  apiVersion: '2023-01-01',
8});

Fetch blog posts in a Next.js page:

1import { client } from '../sanityClient';
2
3export async function getStaticProps() {
4  const posts = await client.fetch(`*[_type == "post"]{title, slug, mainImage}`);
5  return { props: { posts } };
6}
7
8export default function Blog({ posts }) {
9  return (
10    <div>
11      <h1>Blog</h1>
12      {posts.map((post) => (
13        <div key={post.slug.current}>
14          <h2>{post.title}</h2>
15          <img src={post.mainImage?.asset?.url} alt={post.title} />
16        </div>
17      ))}
18    </div>
19  );
20}

Conclusion

Sanity CMS is a powerful and flexible solution for building modern blogs. By leveraging its real-time editing, structured content approach, and seamless integration with front-end frameworks like Next.js, developers can create dynamic, scalable, and highly customizable blogs. Try it today and experience the future of content management!