Home › Articles › Reading

December 5, 2024

Building a Custom Sitecore JSS Component with Next.js

Building a component in Sitecore headless involves more than creating a React component.

Unlike a standalone React application, a Sitecore JSS component must work with content models, rendering definitions, datasources, placeholders and editorial capabilities.

A component that works perfectly on the public website but cannot be configured or edited by content authors is not a complete Sitecore implementation.

Let's understand this through a practical Hero Banner component in Sitecore 10 Headless.

Step 1: Create the datasource template

Create a Hero Banner data template in Sitecore with the following fields:

  • Title – Single-Line Text

  • Description – Rich Text

  • BannerImage – Image

  • CTA – General Link

The template defines the content contract between Sitecore and the frontend application.

I generally prefer keeping component-specific content in datasource items rather than adding every component field directly to the page template.

This improves component reuse and separates page structure from component content.

Step 2: Create a JSON Rendering

Create a JSON Rendering under the appropriate rendering folder in Sitecore.

Configure the following properties:

  • Component Name: HeroBanner

  • Datasource Template: Hero Banner template

  • Datasource Location: Appropriate component data folder

  • Rendering Contents Resolver: Default datasource behavior or a suitable custom resolver

The component name is particularly important because the JSS application uses it to identify the corresponding frontend component.

Unlike traditional MVC renderings, a JSON Rendering does not require a Razor view to generate the final presentation.

Step 3: Configure placeholder settings

Add the Hero Banner rendering to the allowed controls of the appropriate placeholder settings item.

This allows content authors to insert the component into permitted page regions.

Placeholder restrictions are an important part of content governance. Not every component should be available in every location.

Step 4: Create the Next.js component

Create a HeroBanner.tsx file in the components directory of your JSS application.

import {
  Text,
  RichText,
  Image,
  Link,
  Field,
  ImageField,
  LinkField
} from '@sitecore-jss/sitecore-jss-nextjs';

type HeroBannerProps = {
  fields: {
    Title: Field<string>;
    Description: Field<string>;
    BannerImage: ImageField;
    CTA: LinkField;
  };
};

const HeroBanner = ({
  fields
}: HeroBannerProps): JSX.Element | null => {
  if (!fields) {
    return null;
  }

  return (
    <section className="hero-banner">
      <Image field={fields.BannerImage} />

      <div className="hero-content">
        <h1>
          <Text field={fields.Title} />
        </h1>

        <RichText field={fields.Description} />

        <Link field={fields.CTA} />
      </div>
    </section>
  );
};

export default HeroBanner;

The Sitecore JSS field components provide appropriate field rendering and support editorial capabilities in the editing context.

Directly accessing field values may be suitable for certain business logic, but it can bypass inline editing markup when used for presentation.

Step 5: Component registration

The JSS application needs to associate the Sitecore component name with the corresponding React implementation.

Depending on the JSS application template and SDK version, the component factory may be generated automatically from the components directory.

The important requirement is that HeroBanner in Sitecore resolves to the correct frontend implementation.

Step 6: Understand the Layout Service response

When the component is added to a page and configured with a datasource, Layout Service returns its rendering information.

A simplified response looks like this:

{
  "componentName": "HeroBanner",
  "dataSource": "{DATASOURCE-ID}",
  "fields": {
    "Title": {
      "value": "Explore Our Solutions"
    },
    "Description": {
      "value": "<p>Discover our services.</p>"
    },
    "BannerImage": {
      "value": {
        "src": "/-/media/banner.jpg",
        "alt": "Our solutions"
      }
    },
    "CTA": {
      "value": {
        "href": "/solutions",
        "text": "Learn More"
      }
    }
  }
}

The JSS application maps this rendering information to the React component and passes the associated fields.

Step 7: Validate the editorial experience

Component development should not end after verifying the public website.

Validate the component in Experience Editor to ensure that authors can:

  • Add and remove the component.

  • Select or create a datasource.

  • Edit supported content fields.

  • Change component content without developer involvement.

  • Preview and publish the updated page.

Missing datasource handling should also provide useful feedback in editing mode rather than silently hiding the component.

A practical troubleshooting scenario

Consider a newly developed Hero Banner component that works locally but does not appear in production.

Before modifying the React code, I would investigate:

  • Whether the JSON Rendering has been published.

  • Whether the datasource template and content are available.

  • Whether the component name matches the frontend registration.

  • Whether the rendering exists in the Layout Service response.

  • Whether the production rendering host contains the latest component implementation.

  • Whether cached layout data or generated pages require revalidation.

This approach separates Sitecore configuration issues from frontend implementation and deployment issues.

Component design considerations

A production-ready Sitecore JSS component should consider accessibility, responsive design, datasource reuse, missing content, rendering parameters, localization, editorial usability and performance.

For example, a content author should be able to change a banner image or select a supported visual variation without requiring another frontend deployment.

However, providing unlimited configuration options can make components unnecessarily complex.

The objective is controlled flexibility.

Architect's takeaway

A Sitecore JSS component is a contract between content architecture and frontend implementation.

Its success depends on how effectively templates, rendering definitions, datasources, Layout Service and React components work together.

A reusable component should serve both website visitors and content authors.

Good Sitecore development is not just about writing React code. It is about building maintainable, author-friendly digital experiences.

No comments:

Post a Comment