- Concepts
Components for rendering Sitecore fields
Content SDK provides components that help you render Sitecore fields in your Next.js applications.
The following table shows the correspondence between Sitecore field types and Content SDK components:
| Sitecore field type | Content SDK component |
|---|---|
| Date | DateField |
| File | File |
| Image | Image. You can also use the NextImage component. |
| General Link | Link |
| Single-Line Text, Multi-Line Text, or numeric types | Text |
| Rich Text | RichText |
Import all of these components from @sitecore-content-sdk/nextjs. Link and RichText have Next.js-specific implementations there that add internal link detection, prefetching, and client-side routing on top of the base components in @sitecore-content-sdk/react. DateField, Text, File, and Image are the same components re-exported from @sitecore-content-sdk/react.
The following sections describe the supported components with usage examples. In the examples, props.fields is the component's field data, which the Placeholder component supplies from the layout data.
Date
The DateField component helps render date and time content fields.
The DateField component has the following properties:
| Name | Description |
|---|---|
field | Required. The field you want to render. It must be a Sitecore Date type. The field data includes the following properties:
|
tag | The name of the HTML element wrapping the date value. If omitted, the value is rendered without a wrapping element. Default: none |
editable | Explicitly enable/disable inline editing. Default: true |
render | A function that receives the field value as a parsed JavaScript Date object and returns the value to render in the wrapping tag of the component. It can be used to format the value for date and datetime localization. |
emptyFieldEditingComponent | If the field has metadata and an empty value, the component specified here is rendered instead. If this is also empty, the default empty field component is rendered. |
Usage
To render a date field, import it into your component:
You can use the component as follows:
-
To render a simple date field:
-
To render a datetime field:
-
To render a date as a UTC-formatted string:
The component provides direct access to the JS
Dateobject for formatting purposes. Therefore, you can use it to render localized date and datetime strings as follows:
File
The File component renders a file link.
The File component has the following properties:
| Name | Description |
|---|---|
field | Required. The field you want to render. It represents a Sitecore File type with the properties src, title, and displayName, either directly on field or on field.value. |
children | A React node, populating the rendered <a /> tag. The default value is the file's title or displayName. |
The File component doesn't support inline editing in Pages.
Usage
To render the file field, import it into your component:
You can use the component as follows:
-
To render a simple file link:
-
To render a file link that opens in a new tab with custom anchor text:
Image
The Image component lets you render editable, responsive images. For details on the properties imageParams, srcSet, and mediaUrlPrefix and for examples of specifying media URL query parameters and rendering responsive images, see The Content SDK Image component.
Usage
To use the Image component, import it into your component:
You can use the Image component to render a simple image:
You can also use the NextImage component, which integrates with next/image.
Link
The Link component helps you render the content of the General Link Sitecore field.
The component checks whether the link in the Sitecore field is internal, and if so, renders it using the Next.js Link component (next/link), including prefetching. Otherwise, it renders a plain <a> tag.
The Link component has the following properties:
| Name | Description |
|---|---|
field | Required. The field you want to render. It must be a Sitecore General Link field type. The field data includes the following properties:
|
editable | Explicitly enable/disable inline editing. Default: true |
internalLinkMatcher | A regex pattern for identifying internal links. Links matching the pattern are rendered with next/link; links to files (matched by file extension) are never treated as routes, regardless of this pattern.Default: /^\//g |
showLinkTextWithChildrenPresent | A boolean that enables or disables the display of the link text even when children exist. Default: false |
renderChildrenWhenEmpty | When true, renders an anchor element containing children even when the link field value is empty, instead of rendering nothing.Default: false |
prefetch, replace, scroll, shallow, passHref, as, onNavigate | Passed through to next/link for internal links. shallow, passHref, and as apply only to the Pages Router. See the Next.js Link documentation for the App Router and Pages Router for details. |
emptyFieldEditingComponent | If the field has metadata and an empty value, the component specified here is rendered instead. If this is also empty, the default empty field component is rendered. |
Usage
To use the Link component, import it into your component:
You can use the Link component to render:
-
External links:
-
Internal links with HTML or other components:
-
Email links:
The Link component accepts additional properties or attributes. The following example displays the link text alongside the child element because showLinkTextWithChildrenPresent is true:
Text
The Text component helps you render Sitecore fields of type Single-Line Text, Multi-Line Text, or numeric fields.
The Text component has the following properties:
| Name | Description |
|---|---|
field | Required. The field you want to render. The field data includes the following properties:
|
tag | The name of the HTML element wrapping the text value. If omitted, the value is rendered without a wrapping element, except in editing mode or when encode is false, where span is used. |
editable | Explicitly enable/disable inline editing. Default: true |
encode | Enables or disables HTML encoding of the output value. Setting this to false also makes editable: false.Default: true |
emptyFieldEditingComponent | If the field has metadata and an empty value, the component specified here is rendered instead. If this is also empty, the default empty field component is rendered. |
For a Multi-line Text field type, line breaks in the value are replaced with <br /> elements.
Usage
To use the Text component, import it into your component:
You can render:
-
A text field with default options:
-
A non-editable text field with a custom tag, CSS classes, and custom attributes:
ImportantSetting
encode={false}outputs raw HTML. Use this option only with trusted content.
If you want to render the raw value of a Sitecore text field, import the getFieldValue function. You can then inspect the raw value in your application. For example:
RichText
The RichText component helps you render Sitecore Rich Text fields.
The component also handles internal links within the rendered HTML: it intercepts clicks on links matching internalLinksSelector and routes them client-side, and it prefetches them according to prefetchLinks.
The RichText component has the following properties:
| Name | Description |
|---|---|
field | Required. The field you want to render. The field data includes the following properties:
|
tag | The name of the HTML element you want to wrap the text value. Default: div |
editable | Explicitly enable/disable inline editing. Default: true |
internalLinksSelector | The CSS selector used to find internal links within the rendered HTML for prefetching and client-side routing. Default: a[href^="/"] |
prefetchLinks | Controls the prefetch of internal links found via internalLinksSelector. This can be beneficial if you have RichText fields with large numbers of internal links in them. Can be one of the following:
Default: true |
emptyFieldEditingComponent | If the field has metadata and an empty value, the component specified here is rendered instead. If this is also empty, the default empty field component is rendered. |
Usage
To use the RichText component, import it into your component:
You can render:
-
A field with default options:
-
A non-editable rich text field with a custom tag, CSS classes, and custom attributes:
-
A rich text field that prefetches its internal links only on hover, useful when a field contains many links: