In the modern web development landscape, decoupled architectures and headless CMS (Content Management Systems) have become increasingly popular due to their flexibility and scalability.
One of the leading headless CMS solutions is Xperience by Kentico, known for its robust API capabilities and developer-friendly environment. When combined with Next.js, a powerful React framework that enables server-side rendering (SSR) and static site generation (SSG), you can create highly performant, dynamic, and SEO-friendly web applications.
This blog post will walk you through how to integrate Xperience by Kentico's Headless API with a Next.js application. We’ll explore setting everything up, fetching and displaying content, and even some advanced tips to help you optimize your web app.
Xperience by Kentico headless API is a cloud-based headless CMS that allows developers to build applications with a focus on content rather than infrastructure. As a headless CMS, Xperience by Kentico decouples the content management back end from the presentation layer, providing content as a service through a powerful API. This approach offers several benefits:
Next.js is a React framework that simplifies the development of modern web applications. It provides features like server-side rendering (SSR) and static site generation (SSG) out of the box, making it an ideal choice for SEO-friendly and performant websites. Key benefits include:
Before getting started, ensure that you have the following installed:
First, you'll need to create a new Next.js project. You can do this running the following command in a CLI:
npx create-next-app kentico-xperience-nextjs-app
Navigate into your project directory:
cd kentico-xperience-nextjs-app
Install Axios for making HTTP requests:
npm install axios
To fetch content from Xperience by Kentico, you’ll use its REST API. Here’s how you can set up an API client using `axios`:
NEXT_PUBLIC_KENTICO_API_URL=https://your-kentico-xperience-site.com/rest
NEXT_PUBLIC_KENTICO_API_KEY=your_api_key
import axios from 'axios';
const apiClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_KENTICO_API_URL,
headers: {
'Authorization':`Bearer ${process.env.NEXT_PUBLIC_KENTICO_API_KEY}`,
},
});
export async function getContentByType(type) {
const response = await apiClient.get(`/content/${type}`);
return response.data.items;
}
The `getContentByType` function can now be used across your Next.js application to fetch data from Xperience by Kentico. For instance, you might fetch and display blog posts like this:
import { getContentByType } from '../lib/kentico';
export async function getStaticProps() {
const posts = await getContentByType('blog_post');
return {
props: {
posts,
},
};
}
export default function Blog({ posts }) {
return (
<div>
<h1>Blog Posts</h1>
<ul>
{posts.map(post => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</div>
);
}
This code assumes your API returns data in a structure where `items` is the key containing your content. You may need to adjust the parsing logic based on your specific API responses.
Static Site Generation is a core feature of Next.js, allowing you to generate static HTML at build time. This is ideal for content that doesn’t change frequently, like blogs or product pages or documentation.
In the example above, `getStaticProps` is used to fetch data at build time, which is then passed as props to the component.
For content that needs to be up-to-date with every request, you can use Server-Side Rendering (SSR). Next.js allows you to implement SSR using `getServerSideProps`.
export async function getServerSideProps() {
const posts = await getContentByType('blog_post');
return {
props: {
posts,
},
};
}
This approach ensures that the content is fetched fresh on every request.
Next.js supports dynamic routing based on file names. To create a dynamic route, use square brackets in the file name:
// pages/blog/[slug].js
import { getContentByType } from '../../lib/Xperience by Kentico';
export async function getStaticPaths() {
const posts = await getContentByType('blog_post');
const paths = posts.map(post => ({
params: { slug: post.slug },
}));
return { paths, fallback: false };
}
export async function getStaticProps({ params }) {
const posts = await getContentByType('blog_post');
const post = posts.find(p => p.slug === params.slug);
return {
props: {
post,
},
};
}
export default function Post({ post }) {
return (
<div>
<h1>{post.title}</h1>
<p>{post.content}</p>
</div>
);
}
Next.js introduces Incremental Static Regeneration (ISR), allowing you to update static content after the build without rebuilding the entire site. You can use ISR by specifying the `revalidate` property in `getStaticProps`.
export async function getStaticProps() {
const posts = await getContentByType('blog_post');
return {
props: {
posts,
},
revalidate: 10, // Revalidate every 10 seconds
};}
This allows your pages to be regenerated in the background when they are requested, providing up-to-date content while maintaining the performance benefits of static sites.
To enhance your web application, consider integrating third-party services like search engines (Algolia, ElasticSearch), analytics (Google Analytics, Segment), and more. This can be easily done within your Next.js application by installing the necessary SDKs and configuring them accordingly.
Building a web application with Xperience by Kentico and Next.js offers a powerful combination of content management and modern web development practices. By leveraging the strengths of both platforms, you can create a high-performance, scalable, and flexible web application.
Vincent Adeyemi, Sr. Full Stack Developer | Sagepath Reply
Vincent is a Senior Full Stack Developer with over 8 years of expertise in the .NET framework, VueJS, Angular, and Kentico. He specializes in custom web development and has a proven track record of delivering successful projects, including REST API integrations.
Reach out to discuss your digital transformation needs and see how we can help. We would love to start a long-term partnership with your company.
Get in Touch