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:
- Sitecore JSS Components Documentation.
- For a deeper dive into how different Sitecore JSS field types work and how to use them correctly, check out All Sitecore JSS Types and How to Use Them.
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} />
enforce-link-component
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