How to Create a Component in Next.js for XM Cloud

Let’s create a component in the Next.js app. 

If you want to know how that component is created in XM Cloud, please refer to my previous blog for more information.

https://sitecoresathiya.blogspot.com/2024/10/how-to-create-component-in-nextjs-for-xmc.html

I have pulled down the startup branch to my local environment.

https://github.com/sitecorelabs/xmcloud-foundation-head

Refer to the README file to learn how to run the Next.js app locally. 

Navigate to the headapps/nextjs-starter folder and execute the following command to scaffold a new component: 

jss scaffold "TextCard".


Let's check if the component is rendered correctly. Run the command below to start your Next.js app locally in connected mode.

npm run start:connected

After that, your app will run on http://localhost:3000/ with the default port if the port is not in use.


 

Yes, it shows what I had in my TextCard.tsx file.


How can we display the content from XMC?

To map the component fields into my TextCardFields object and pass that as props.


Do you know how data from XM Cloud is retrieved in Next.js?

The ComponentRendering JSS SDK delivers data through either GraphQL or the Layout Service. 

import { ComponentParams, ComponentRendering,} from '@sitecore-jss/sitecore-jss-nextjs'


Here’s a guide on how to use Sitecore fields in Next.js.

https://doc.sitecore.com/xp/en/developers/hd/latest/sitecore-headless-development/jss-components-for-rendering-sitecore-fields-in-next-js-applications.html


The content from XMC is currently displayed, but the design looks unpolished. To improve the design, I’ve created a CSS file in the assets folder and added the necessary styles. Now, we need to import this file into index.scss.

 

After applying the CSS changes, my component looks much better.

Let’s take a look at how it is displayed in the XMC page editor.


Comments

Popular posts from this blog

How to Enable Voice Search in Sitecore Content Editor

How to Create a Custom Component in XM Cloud.