Create your first Sitecore Next.js Component
This will guide you in creating your first Sitecore component using Next.js. The approach we will be using is the Sitecore-first development workflow (opens in a new tab). You will learn how to create a json rendering, add placeholder settings, and create the next.js component attached to it.
If you do not have a local environment yet, see prerequisites and installing a Sitecore environment.
This guide assumes you've installed and configured your Sitecore environment with xmnextjs
as the site name.
Log into Sitecore
First log onto your Content Management environment https://cm.xmnextjs.localhost/sitecore
using user name: admin
and the password
you entered on step 4.1 of installing Sitecore.
Once you've logged on, you will see the launchpad. Click on the Content Editor.
Create a rendering
- Navigate to
/sitecore/layout/Renderings/Feature
- Create a rendering folder
- Right click, insert, and select
Rendering Folder
- Name it
Article
- Right click, insert, and select
- Create a json rendering
- Navigate to the
Article
folder you created - Right click, insert, and select
Json Rendering
- Name the rendering
Article
as well
- Navigate to the
At this point you should have something that looks like the below. Mentally note that the component name is also Article
as specified under the Component Name
field
Add rendering to Placeholder Settings
The Placeholder Settings is a place where you specify what renderings content editors are allowed to place in a placeholder. We will add our newly created Article
to the list of renderings that the Main
placeholder is allowed to have.
- Navigate to
/sitecore/layout/Placeholder Settings/Project/xmnextjs/jss-main
.
Note: you actually have to click on theMain
item in Sitecore. This is because even though the item name isjss-main
, the Display Name isMain
. - Under
Allowed Controls
click edit - Navigate to the
Article
component, and double click to select (alternatively, click the right arrow next to Selected) - Make sure
Article
is on the right side under Selected - Click OK
- You will see that
Article
is now in theAllowed Controls
field - On the main ribbon, click Save to save the
Main
Placeholder Setting item.
Add rendering to a page
Now that you have created a rendering and specified where on the page the rendering are allowed to be placed in, let's add it to an actual page. We will add the rendering to the home page.
- Navigate to
/sitecore/content/XmNextJs/home
- Make sure
home
item is selected. On the main ribbon, ClickPresentation > Details
- Click on
Final Layout
- We see that
ContentBlock
is the only rendering currently on the home page - Click edit
- Click Controls, then click Add
- Navigate to Renderings/Feature/Article and select
Article
- Under "Add to Placeholder" enter
jss-main
. This was the placeholder that we allowed our rendering to be placed in.jss-main
is also considered the placeholder key. - Click Select
- Click Move Up so that Article is now above ContentBlock and is the first component on the home page
- Click OK, then Click OK to close all the dialogs.
- Let's view our homepage
- On the main ribbon, click
Publish > Experience Editor
. The Experience Editor is where we can preview and edit how our page looks. - In the Experience Editor, we see a warning. This is because we have not created our React/Next.js component that is tied to this rendering yet.
Create the Next.js component
- Open Visual Studio Code
- Under
./rendering/src/components
, create aFeature
folder, and under that create anArticle
folder. - Create a file and name it
Article.tsx
- Add code. Notice that the component name must match the Component Name field in Sitecore.
const Article = (): JSX.Element => (
<section>
<h1>My first article</h1>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</section>
);
export default Article
Go back to the Experience Editor and we see that our component has rendered! Ignore the "Hello World" which was part of a previous lesson.
Publish changes in Sitecore
Let's visit our rendering host site: https://www.xmnextjs.localhost/ (opens in a new tab). Notice that we do not see our component.
This is because we have been working under the master
database in Sitecore whereas the rendering host site is connected to the web
database.
The Experience Editor provides a preview of our site.
To publish our site:
- In the Content editor, main ribbon, Publish > Publish drop down > Publish Site
- Select Smart Publish and all languages
- Click Publish
On a live site, the "Publish Site" option should rarely be used and "Publish Item" should be used instead. This is to prevent accidentally publishing content that an editor isn't ready to share to visitors.
Summary
We created a Sitecore rendering, edit the placeholder settings to allow our new rendering to be added, added the rendering to a page, created the corresponding Next.js component and publish the site.
Our component currently only has hardcoded content, which isn't useful to a content editor. In our next lesson, we will go over how to make the component content-editable.
Knowledge check:
- What is a rendering?
- What is a placeholder?
- How do we restrict what renderings are allowed in a placeholder?
- What is a placeholder key?
- How do we add a rendering to a page?
- What does a rendering's component must match to?
- Why do we have to publish changes in Sitecore?