Improving Sitecore JSS Development with ESLint: Introducing eslint-plugin-sitecore-jss

Boost your Sitecore JSS development with eslint-plugin-sitecore-jss, an ESLint plugin that enforces best practices and automates component usage checks in Sitecore projects.

March 13, 2025

By Sohrab Saboori

Why Using Sitecore JSS Components is Best Practice

When working with Sitecore JSS and XM Cloud, following best practices ensures maintainability, scalability, and proper integration with Sitecore’s editing capabilities. Sitecore provides a set of JSS components specifically designed for rendering fields such as text, rich text, images, links, and files.

Using these predefined components is recommended because:

  • They ensure compatibility with Sitecore Experience Editor.
  • They handle field-specific rendering correctly.
  • They improve consistency across your Next.js project.

More details can be found in Sitecore’s official documentation:

The Role of ESLint in JavaScript Code Quality

ESLint is a widely used tool for enforcing coding standards and identifying potential issues in JavaScript and TypeScript projects. It helps developers catch errors early and maintain clean, readable, and efficient code.

In the context of Sitecore JSS, manually reviewing all components for correct usage is time-consuming and error-prone. That’s where ESLint comes in! By integrating TypeScript type-checking, we can automate the process of identifying incorrect usage of Sitecore JSS components and suggest proper fixes.

Introducing ESLint Plugin for Sitecore JSS

To make it easier for developers to follow best practices, I’ve developed eslint-plugin-sitecore-jss, an ESLint plugin that enforces the correct usage of Sitecore JSS components in Next.js projects.

Features

  • Detects incorrect usage of Sitecore JSS components.
  • Recommends proper Sitecore JSS components for fields.
  • Supports auto-fixing with --fix .
  • Integrates seamlessly with TypeScript and ESLint.
  • Includes multiple individual rules, or a single rule for all components.

Installation

To install the plugin, run:

npm install --save-dev eslint-plugin-sitecore-jss

Or using Yarn:

yarn add -D eslint-plugin-sitecore-jss

Usage

Update your ESLint configuration (.eslintrc.json or .eslintrc.js):

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": ["sitecore-jss"],
}

Alternatively, you can enable all rules at once with:

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": ["sitecore-jss"],
  "extends": ["plugin:sitecore-jss/all"],
}

Rules and Examples

enforce-text-component

Ensures that Field<string> and TextField are wrapped with <Text>.

Incorrect:

<p>{props.fields.title.value}</p>

Correct:

<Text field={props.fields.title} tag="p" />

enforce-richtext-component

Ensures that RichTextField is wrapped with <RichText>.

Incorrect:

<div>{props.fields.body.value}</div>

Correct:

<RichText field={props.fields.body} tag="div" />

enforce-image-component

Ensures that ImageField is used with <Image> instead of <img>.

Incorrect:

<img src={props.fields.image.value.src} alt={props.fields.image.value.alt} />

Correct:

<Image field={props.fields.image} />

Ensures that LinkField is used with <Link> instead of <a>.

Incorrect:

<a href={props.fields.externalLink.value.href}>
  {props.fields.externalLink.value.text}
</a>

Correct:

<Link field={props.fields.externalLink} />

enforce-file-component

Ensures that FileField is used with <File> instead of <a>.

Incorrect:

<a href={props.fields.file.value.src}>{props.fields.file.value.title}</a>

Correct:

<File field={props.fields.file} />

Contribute and Provide Feedback!

This is an open-source project, and contributions are always welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on GitHub.

🔗 GitHub Repository: eslint-plugin-sitecore-jss

It would be really helpful if you try it out in your projects and share your feedback. Let’s improve this together and make Sitecore JSS development even better! 🚀

🔗NPM: eslint-plugin-sitecore-jss

🔗GitHub: github.com/rikaweb/eslint-plugin-sitecore-jss

References:

Photo of Fishtank employee Sohrab Saboori

Sohrab Saboori

Senior Full-Stack Developer

Sohrab is a Senior Front-End Developer with extensive experience in React, Next.js, JavaScript, and TypeScript. Sohrab is committed to delivering outstanding digital solutions that not only meet but exceed clients' expectations. His expertise in building scalable and efficient web applications, responsive websites, and e-commerce platforms is unparalleled. Sohrab has a keen eye for detail and a passion for creating seamless user experiences. He is a problem-solver at heart and enjoys working with clients to find innovative solutions to their digital needs. When he's not coding, you can find him lifting weights at the gym, pounding the pavement on the run, exploring the great outdoors, or trying new restaurants and cuisines. Sohrab believes in a healthy and balanced lifestyle and finds that these activities help fuel his creativity and problem-solving skills.