Content rendering
Learn how to fetch and render content for your slots.
This guide provides practical examples of how to use the Next.js SDK to fetch and render a Slot on the server side.
Basic usage
To fetch content on the server side, you can use the fetchContent function. This function receives the ID of the slot you want to fetch and returns a promise that resolves to the content.
Go to the Integration page, select the slot you want to implement, and get a fully functional integration code to copy and paste.
Let's say you have a slot called home-hero which represents the content for the hero section of your homepage.
You can retrieve the content for this slot in a component as follows:
import {fetchContent} from '@croct/plug-next/server';export async function HomeHero() {const {content} = await fetchContent('home-hero');return (<div><strong>{content.title}</strong><p>{content.subtitle}</p><a href={content.button.link}>{content.button.label}</a></div>);}
For more information about the available options, refer to the documentation of the fetchContent function.
Fault tolerance
You should always provide a fallback content to make your application resilient to unexpected errors, downtime, and network failures.
All you have to do is specify the content you want to use as a fallback:
import {fetchContent} from '@croct/plug-next/server';export async function HomeHero() {const content = await fetchContent('home-hero', {fallback: {title: 'Welcome to Croct!',subtitle: 'The easiest way to personalize your application.',button: {label: 'Get started',link: '/signup',}},});return (<div><strong>{content.title}</strong><p>{content.subtitle}</p><a href={content.button.link}>{content.button.label}</a></div>);}
The SDK takes care of the rest, ensuring that your application will always have content to render, even if the fetch fails.
Version control
You can lock a specific slot version to keep the content structure aligned with your application's expectations. This gives your team the freedom to evolve the structure over time without the risk of breaking things.
You can specify the version of the slot by passing a versioned ID in the form <id>@<version>. For example, passing home-hero@2 will fetch the content for the home-hero slot in version 2. Not specifying a version number is the same as passing home-hero@latest, which will load the latest content.
import {fetchContent} from '@croct/plug-next/server';export async function HomeHero() {const content = await fetchContent('home-hero@2');return (<div><strong>{content.title}</strong><p>{content.subtitle}</p><a href={content.button.link}>{content.button.label}</a></div>);}
For more information, see Slot versioning.
Localization
To support multiple locales, you can use the preferredLocale option to specify the locale of the content you want to retrieve. This is usually the locale of the user's browser or account.
If you are using Internationalized Routing, the SDK will automatically use the locale from the router. If you are not using internalization, or if there is no content available in that locale, the SDK will fall back to the default locale of your workspace.
You can always specify a different locale as needed:
import {fetchContent} from '@croct/plug-next/server';export async function HomeHero() {const content = await fetchContent('home-hero', {preferredLocale: 'en-ca',});return (<div><strong>{content.title}</strong><p>{content.subtitle}</p><a href={content.button.url}>{content.button.label}</a></div>);}
For more information, refer to the preferredLocale documentation.
Context variables
Sometimes you need to provide additional information to personalize or segment your users.
For example, if you are working on a SaaS application, you may want to personalize the content based on the subscription plan, quota usage, features, or any other application-specific information. You can achieve this by passing any relevant information to the attributes option:
import {fetchContent} from '@croct/plug-next/server';export async function UpgradeBanner() {const content = await fetchContent('upgrade-banner', {context: {attributes: {plan: 'premium'}},});return (<div><strong>{content.title}</strong><p>{content.subtitle}</p><a href={content.button.url}>{content.button.label}</a></div>);}
These values are then accessible as custom attributes in the context variable:
context's plan is "premium"
Keep in mind that the context has some constraints on the number of attributes and levels of nesting. For more information, please refer to the attributes documentation.