Blog System with Sanity CMS
The OnlyAutomator blog is built using Sanity CMS, a headless content management system that provides a flexible and customizable content editing experience. This documentation covers the structure, integration, and implementation details of the blog system.Architecture Overview
The blog system follows a headless CMS architecture:- Content Management: Sanity Studio provides a customizable editing interface for content creators
- Content Storage: Content is stored in Sanity’s cloud database
- Content Delivery: Content is fetched via Sanity’s CDN and API and rendered in the Next.js frontend
Sanity Configuration
The Sanity project configuration is defined insanity.config.ts
:
- Project name and title
- Sanity project ID and dataset
- Plugins for the Sanity Studio interface
- Schema types definition import
Content Schema
The blog content is structured with the following schema types:Post Schema
Author Schema
Category Schema
Schema Registration
All schemas are registered in theschemaTypes/index.ts
file:
Integration with Next.js
The blog system integrates with Next.js through a client utility that provides typed data fetching functions.Sanity Client
The client is configured inlib/sanity.ts
:
Data Types
The client file also defines TypeScript types for blog content:Data Fetching Functions
The client provides several utility functions for fetching blog content:Next.js Routes
The blog is implemented with the following Next.js routes:- Blog Listing:
/blog
- Displays all blog posts - Blog Detail:
/blog/[slug]
- Displays a single blog post - Sanity Studio:
/studio/[[...tool]]
- The admin interface for content management
Image Handling
Sanity images are managed through a utility inlib/sanity-image.ts
, which configures the Sanity image URL builder for optimal delivery and transformations.
Using Sanity Studio
Sanity Studio is the content management interface where you can create, edit, and manage all blog content. The following section provides detailed instructions on how to access and use Sanity Studio.Accessing Sanity Studio
-
Local Development:
- Run your Next.js application locally using
npm run dev
oryarn dev
- Navigate to
http://localhost:3000/studio
in your browser
- Run your Next.js application locally using
-
Production Environment:
- Access Sanity Studio at
https://onlyautomator.com/studio
- You will need to log in with your Sanity credentials
- Access Sanity Studio at
Authentication
When accessing Sanity Studio, you’ll be prompted to log in:- Enter your Sanity credentials (email/username and password)
- If you don’t have credentials, contact the project administrator to receive an invitation
- Sanity Studio uses secure OAuth authentication and ensures content creators have appropriate permissions
Studio Interface Overview
The Sanity Studio interface consists of:- Desk Tool: The main content management area where you’ll create and edit content
- Navigation Bar: Left sidebar showing different content types (Post, Author, Category)
- Document List: Center area showing a list of existing documents of the selected type
- Document Editor: Right area for editing the selected document
Managing Blog Content
Creating a New Blog Post
-
Start a New Post:
- In Sanity Studio, select “Post” from the left navigation bar
- Click the “Create new document” button (+ icon) in the top right of the document list
-
Add Basic Information:
- Title: Enter the post title
- Slug: The URL-friendly version of the title will be auto-generated, but you can edit it if needed
- Author: Select an existing author from the dropdown (you must create an author first if none exist)
-
Add Content:
- Main Image: Upload a featured image for the post
- Categories: Select one or more categories for the post
- Published At: Set the publication date and time
- Excerpt: Write a short summary of the post (used for SEO and previews)
-
Write the Post Body:
- Use the rich text editor to create the post content
- Format text using the toolbar (headings, bold, italic, lists, etc.)
- Add images by clicking the ”+” button in the editor
- Use the “Style” dropdown to apply different text styles
-
Preview your Post:
- Click the “Preview” button in the top right to see how the post will look on the website
- This opens a new tab with a live preview of your content
-
Publish the Post:
- Click the “Publish” button in the bottom right when you’re ready to make the post live
- You can also save drafts without publishing by clicking “Save draft”
Editing an Existing Blog Post
-
Find the Post:
- Select “Post” from the left navigation
- Browse the list or use the search bar to find the post you want to edit
-
Make Changes:
- Click on the post to open it in the editor
- Make your changes to any field
- Use the history panel (clock icon) to view previous versions if needed
-
Save Changes:
- Click “Publish” to update the live post
- Or “Save draft” to save changes without publishing
Creating and Managing Authors
-
Access Authors:
- Select “Author” from the left navigation
-
Create a New Author:
- Click the ”+” button to create a new author
- Fill in the required fields:
- Name: The author’s full name
- Slug: Auto-generated from the name
- Image: Upload an author photo
- Bio: Write a short biography using the rich text editor
-
Edit Authors:
- Select an existing author from the list
- Make changes and publish to update
Managing Categories
-
Access Categories:
- Select “Category” from the left navigation
-
Create a New Category:
- Click the ”+” button
- Enter the category title and optional description
- Publish to create the category
-
Edit Categories:
- Select an existing category
- Make changes and publish to update
Advanced Features
Scheduling Posts
- To schedule a post for future publication:
- Create or edit a post as normal
- Set the “Published At” date to a future date and time
- Click “Publish”
- The post will only appear on the website after the scheduled date/time
Managing Drafts
-
View Drafts:
- In each content type section, you can filter to show only drafts
- Drafts are indicated with a special badge
-
Working with Drafts:
- You can continue editing drafts until they’re ready
- Drafts are not visible to visitors on the website
Collaborative Editing
- Sanity Studio supports real-time collaborative editing:
- Multiple editors can work on the same document simultaneously
- You’ll see other editors’ cursors and changes in real-time
- The system handles conflict resolution automatically
Best Practices for Blog Content
-
Consistent Formatting:
- Use headings (H2, H3) to organize content
- Keep paragraphs concise for readability
- Use lists where appropriate
-
Images:
- Use high-quality images (recommended size: 1200x630px for main images)
- Always include alt text for accessibility
- Compress images before uploading for better performance
-
SEO Optimization:
- Write descriptive, keyword-rich titles
- Create detailed excerpts that summarize the content
- Use categories effectively to organize content
-
Content Planning:
- Use drafts to prepare content in advance
- Schedule posts strategically for consistent publication
- Maintain a content calendar outside of Sanity
Environment Variables
The blog system relies on the following environment variables:Working with the Blog System
Creating/Editing Content
- Navigate to
/studio
and log in with Sanity credentials - Create or edit posts, authors, and categories
- Use the rich text editor for post content
- Preview changes before publishing
Fetching Content in Components
Rendering Rich Text Content
Sanity’s “portable text” format for rich content requires special handling to render properly in React:Extending the Blog System
To extend the blog system with new content types or fields:- Add a new schema file in
sanity/schemaTypes/
- Register the schema in
sanity/schemaTypes/index.ts
- Update the appropriate TypeScript types in
lib/sanity.ts
- Add or modify data fetching functions as needed
- Implement the UI components for displaying the new content