Web applications and websites have several types of rendering methods for their HTML.
In particular, modern web application frameworks like Next.js allow for control over the rendering method on a per-page basis.
In this environment surrounding web frontend development, it is important to understand the advantages and disadvantages of each rendering method and choose the one that fits the nature of the page.
This page will explain the overview, advantages, and disadvantages of each rendering method, as well as how to integrate them when using microCMS.
The sample code is an implementation example using Next.js (Pages Router).
CSR is a method of dynamically generating web pages using JavaScript.
In CSR, an empty HTML file, CSS, and JavaScript are first retrieved from the server. Then, initial data is fetched from an API server (or a headless CMS like microCMS), and the HTML is rendered.

import { useEffect, useState } from 'react';
import { createClient } from 'microcms-js-sdk';
const client = createClient({
serviceDomain: 'YOUR_DOMAIN',
apiKey: 'YOUR_API_KEY',
});
const Announcements = () => {
const [announcements, setAnnouncements] = useState([]);
useEffect(() => {
const getAnnouncements = async () => {
const data = await client.get({ endpoint: 'announcements' });
setAnnouncements(data.contents);
};
getAnnouncements();
}, []);
return (
<div>
{announcements.map((announcement) => (
<div key={announcement.id}>
<h2>{announcement.title}</h2>
<p>{announcement.content}</p>
</div>
))}
</div>
);
};
export default Announcements;SSR is a method of generating HTML on the server side.
When the server receives a request from the client, it retrieves the necessary content through API requests and then generates HTML to return to the client.
The client receives the rendered HTML, loads the CSS and JavaScript, and displays the page.
This method contrasts with CSR, where HTML is generated on the client side.

import { createClient } from 'microcms-js-sdk';
const client = createClient({
serviceDomain: 'YOUR_DOMAIN',
apiKey: 'YOUR_API_KEY',
});
export const getServerSideProps = async () => {
const data = await client.get({ endpoint: 'announcements' });
return {
props: {
announcements: data.contents,
},
};
};
const Announcements = ({ announcements }) => {
return (
<div>
{announcements.map((announcement) => (
<div key={announcement.id}>
<h2>{announcement.title}</h2>
<p>{announcement.content}</p>
</div>
))}
</div>
);
};
export default Announcements;SSG is a method of generating static HTML in advance.
In SSG, when content or templates are updated, the build process is executed to pre-generate all pages. These static files are then deployed to a hosting server to respond to client requests.
To leverage the characteristics of SSG, it is common to place and distribute them via a CDN.

import { createClient } from 'microcms-js-sdk';
const client = createClient({
serviceDomain: 'YOUR_DOMAIN',
apiKey: 'YOUR_API_KEY',
});
export const getStaticProps = async () => {
const data = await client.get({ endpoint: 'announcements' });
return {
props: {
announcements: data.contents,
},
};
};
const Announcements = ({ announcements }) => {
return (
<div>
{announcements.map((announcement) => (
<div key={announcement.id}>
<h2>{announcement.title}</h2>
<p>{announcement.content}</p>
</div>
))}
</div>
);
};
export default Announcements;ISR is a hybrid rendering method that combines the features of SSG and SSR.
In ISR, instead of pre-building the entire site like SSG, only the necessary pages are generated at the required time. The generated pages are cached and served as static content for subsequent requests.
Additionally, a validity period can be specified for the cache, and if access occurs after the validity period has expired, the page will be regenerated. This mechanism allows for the provision of relatively fresh content.
Currently, ISR is supported by some frameworks, such as Next.js.
import { createClient } from 'microcms-js-sdk';
const client = createClient({
serviceDomain: 'YOUR_DOMAIN',
apiKey: 'YOUR_API_KEY',
});
export const getStaticPaths = async () => {
const data = await client.get({ endpoint: 'announcements' });
const paths = data.contents.map((announcement) => ({
params: { id: announcement.id.toString() },
}));
return {
paths,
fallback: true,
};
};
export const getStaticProps = async ({ params }) => {
const data = await client.get({ endpoint: `announcements/${params.id}` });
return {
props: {
announcement: data,
},
revalidate: 10,
};
};
const Announcement = ({ announcement }) => {
return (
<div>
<h2>{announcement.title}</h2>
<p>{announcement.content}</p>
</div>
);
};
export default Announcement;