PPawan Tamada
Back
Integrating Sanity CMS with Next.js for Blogs

Integrating Sanity CMS with Next.js for Blogs

By Pawan TamadaJune 13, 2025

In today’s fast-paced web development world, using a headless CMS gives you flexibility, scalability, and complete control over your content structure. Sanity.io is one of the most developer-friendly CMS platforms, and pairing it with Next.js makes building blogs and dynamic sites a smooth experience.

In this post, I’ll walk you through how I integrated Sanity with Next.js to create a dynamic blog for my portfolio.

Why Sanity + Next.js?

  • Sanity CMS provides real-time content editing, flexible schemas, and a powerful query language (GROQ).
  • Next.js enables server-side rendering (SSR), static site generation (SSG), and a great developer experience for React projects.
  • Together, they make content-rich websites fast and fully customizable.

Step 1: Setting Up Sanity

Install Sanity CLI
javascript code
1npm install -g @sanity/cli
Initialize Sanity Project
shell code
1sanity init 2
  • Choose Blog (schema) when prompted.
  • Follow the steps to link or create a Sanity project.
  • This creates a /studio folder with schemas for posts, authors, and more.
Start Sanity Studio Locally
shell code
1cd studio 2sanity dev 3

Now you have a running CMS where you can add blog posts and content.

Step 2: Connecting Next.js to Sanity

Install Dependencies
shell code
1npm install @sanity/client @portabletext/react 2
Create Sanity Client in Next.js
typescript code
1// lib/sanityClient.ts 2import sanityClient from '@sanity/client'



typescript code
1export const client = sanityClient({ 2 projectId: 'your_project_id', // Found in sanity.json or project dashboard 3 dataset: 'production', 4 useCdn: true, 5 apiVersion: '2023-01-01', // Use current date 6})


Step 3: Fetching Blog Posts with GROQ

typescript code
1// lib/queries.ts 2export const postsQuery = `*[_type == "post"]{ 3 _id, 4 title, 5 slug, 6 mainImage, 7 body, 8 publishedAt 9} | order(publishedAt desc)` 10 11 12// pages/blog.tsx 13import { client } from '@/lib/sanityClient' 14import { postsQuery } from '@/lib/queries' 15 16export async function getStaticProps() { 17 const posts = await client.fetch(postsQuery) 18 return { props: { posts } } 19}


Step 4: Rendering Content with Portable Text

typescript code
1import { PortableText } from '@portabletext/react' 2 3const BlogPost = ({ post }) => ( 4 <article> 5 <h1>{post.title}</h1> 6 <PortableText value={post.body} /> 7 </article> 8)


Step 5: Deploying

  • Sanity Studio → Deploy using:
shell code
1sanity deploy


  • Next.js App → Deploy to Vercel for seamless integration.

Conclusion

Integrating Sanity CMS with Next.js makes managing blog content effortless while keeping the frontend flexible and performant. Whether you’re building a personal blog, a client project, or a full content-driven app, this stack is a powerful combination.

Want to see this live? Check out my portfolio or get in touch .

Let me know if you want a codebase starter or help writing the next post in this series!