iTranslated by AI
Building a Website with a CMS from Scratch in 2024
tl;dr
- Built a website with CMS from scratch using a tech stack including Next.js and Sanity.
- Sanity offers an excellent content editing experience, extensive documentation, and high customizability.
- Decided by comparing no-code vs. scratch options, balancing cost and scalability.
What I Built
This is the homepage for Active Club (commonly known as "Acti-bu"), a technical group formed by four university classmates. Although we all work at various companies now, we use our spare time to create things and share information about technology in general, regardless of whether it's hardware or software.
The structure is simple, consisting of a top page, blog content, an "About" page, and a contact page.
The tech stack I adopted includes Next.js, Sanity, shadcn/ui, and Vercel.
The code is available on GitHub.
Requirements
Ability to host web pages and edit/manage blog content
In other words, a website with CMS functionality.
Curate articles managed on other platforms
Content previously posted by members individually on other platforms (such as Zenn) will also be curated and displayed on the blog list page.
Design Points
Whether to Use No-code or Build from Scratch
Nowadays, many no-code tools with CMS functionality exist, and candidates like STUDIO, Webflow, WordPress, Ghost, and Squarespace were considered. The key selection criteria were as follows:
- Low operational cost
- Scalability
Low Operational Cost
For this project, we were conscious of not managing the infrastructure layer ourselves, including the database.
Changes to the front-end UI design are often necessary whether using no-code or scratch, so we felt the basic effort would be similar. However, managing infrastructure ourselves would significantly increase the monitoring burden and the scope of impact during design changes.
While choosing no-code would avoid these issues, I thought there might be a way to do it from scratch these days. Specifically, by offloading CMS management to Sanity and reducing hosting effort with PaaS like Vercel, we established a system where we can leave the backend to providers and concentrate on app development while keeping costs down.
Scalability
It's common for new functional requirements to arise during operation. While no-code tools lower initial development costs, forcing additional features into them often leads to compatibility issues and bugs caused by unexpected behavior. Specifically, glitches frequently occur due to tool limitations when implementing complex customizations. While no-code is easy to start with, it comes with the trade-off of being difficult to scale.
In this project, there was a requirement to curate articles managed on other platforms. This required listing articles created in the CMS and those from other platforms together at the same level. While existing CMSs can handle this to some extent, it would have necessitated adding text like "Read the original article here [Link to article]" in the body of the blog detail page. I wanted to avoid inserting unnecessary pages into the user experience as much as possible, so I ruled out the no-code option.
Despite everything I've written, in the end, since it's a technical site and I'm a front-end developer by trade, part of the decision was simply the personal desire to "build our own website from scratch after all" 🤣
Which Headless CMS is Best?
I ultimately chose Sanity, and these were the deciding factors:
- Content editing experience
- Extensive documentation
- The sense of security that anything seems customizable
Content Editing Experience
This refers to what's known as a WYSIWYG editor, where content entered by the user is immediately previewed with styling. In Sanity, this is called Visual Editing, and a surprisingly significant benefit is that editing and previewing can be done within the same screen.

While it was possible to preview with other headless CMSs, it often required opening a separate window. (Perhaps they've become more convenient recently?)
Seeing pre-built content updated in real-time in a remote environment looks like magic, which made me curious about how it's achieved and if it's secure. I've included my findings on that at the end.
Extensive Documentation
Beyond explanations of Sanity-specific features, you can find many articles on integration with modern frameworks like Next.js, Gatsby, and React.
The front-end landscape changes rapidly, but I had the impression that articles tagged "Official" generally followed the latest versions of frameworks. This was very welcoming and helpful for a newcomer like me.
For reference, this article was helpful for Next.js users. It covers everything from a clean slate to creating schemas, generating types, and fetching data from the front end.
The Peace of Mind That Anything Seems Customizable
I've only known about Sanity for about a week, so I can't speak in detail about all the customization possibilities, but I got the impression that there is plenty of room for extensibility, not just in the front-end UI but also on the content management side.
For example, the dashboard for managing content is called Sanity Studio, and it features "Studio Tools" that allow you to add custom buttons and actions within the dashboard. You can easily add operations tailored to your specific business processes, such as adding a custom button to execute an action that calls a specific external API.
As an example, if you are fetching blog content via SSR at build time, a rebuild is required whenever there's an update. Thus, simply clicking the "Update" button in the dashboard won't reflect the changes on the site immediately (setting aside ISR for a moment). In such cases, you can customize the dashboard by adding a custom "Rebuild" button to trigger a build action in your CI during an update.
How Sanity Realizes Real-time Updates of Edited Content
I was curious about two points. I will include the code for the part where content is fetched.
How is it achieved?
It refetches content upon detecting edits in Sanity Studio, and it seems the following Loaders feature provides the live update functionality.
While detailed technical specifications were not provided, it was mentioned that Next.js users can benefit from Loaders by using the client provided by next-sanity.
Is it secure?
When Next.js Draft Mode is enabled, content is fetched by including a token issued on Sanity in the request. Since only entities with the token can retrieve draft content, keeping it in environment variables should be sufficient for now.
import { createClient, QueryOptions, type QueryParams } from "next-sanity";
import { draftMode } from "next/headers";
import { apiVersion, dataset, projectId, token } from "./env";
export const client = createClient({
projectId,
dataset,
apiVersion,
useCdn: false,
perspective: "published",
resultSourceMap: "withKeyArraySelector",
stega: {
enabled: true,
studioUrl: `${process.env.NEXT_PUBLIC_TEST_BASE_PATH || ""}/studio#`,
},
});
export async function sanityFetch<QueryResponse>({
query,
params = {},
revalidate = 60, // default revalidation time in seconds
tags = [],
}: {
query: string;
params?: QueryParams;
revalidate?: number | false;
tags?: string[];
}) {
const isDraftMode = (await draftMode()).isEnabled;
if (isDraftMode && !token) {
throw new Error("Missing environment variable SANITY_API_READ_TOKEN");
}
const queryOptions: QueryOptions = {};
let maybeRevalidate = revalidate;
if (isDraftMode) {
queryOptions.token = token;
queryOptions.perspective = "previewDrafts";
queryOptions.stega = true;
maybeRevalidate = 0; // Do not cache in Draft Mode
} else if (tags.length) {
maybeRevalidate = false; // Cache indefinitely if tags supplied
}
return client.fetch<QueryResponse>(query, params, {
...queryOptions,
next: {
revalidate: maybeRevalidate,
tags,
},
});
}
Summary
Before I knew it, this post became primarily about Sanity, but in this article, I’ve explained the project of building a website with a CMS from scratch. By comparing the options between no-code tools and scratch development and ultimately choosing Sanity and Vercel, I believe we achieved a good balance between operational costs and extensibility.
The reasons for choosing Sanity include its excellent content editing experience, extensive documentation, and high level of customizability. These factors allowed us to move forward with development efficiently while flexibly adding necessary features.
I hope this project serves as a useful reference for those considering similar CMS development.
See you again!
Discussion