Headless Sitefinity with React: moving to the renderer
With Sitefinity’s renderer approach, React runs headless in front of the CMS: Sitefinity provides content and page structure, a Next.js application renders the pages. For an existing project this means moving page by page, and this is exactly where Web Components help, because they run in both worlds. In this article I explain how the renderer works according to the Progress documentation, where the limits of a gradual move are and how React and existing StencilJS components fit together. I am currently building this move for an energy provider’s websites; as a first step we are converting every page to React.
How does the renderer work?
In this model Sitefinity separates presentation from the CMS. The presentation layer is either the ASP.NET Core Renderer or the Next.js Renderer. Renderer and CMS talk to each other through the Page Layout Service, using REST. According to Progress this also makes it easier to upgrade the backend, because presentation runs separately.
For the ASP.NET Core Renderer the documentation describes the flow in detail: the renderer forwards a page request to Sitefinity, and the CMS checks whether it is a renderer page. If it is, Sitefinity returns a JSON file with the page structure, and the renderer assembles the page with its own templates and widgets. For MVC pages Sitefinity renders the HTML itself and returns it to the renderer, which serves it as it is.
The renderer is a proxy at the same time: whatever it does not answer itself is forwarded to Sitefinity. With the ASP.NET Core Renderer its own resources live under the sfrenderer path, and each forwarded request carries the header SF-RENDERER-PROXY=true, from which the CMS knows that it came through the renderer. For visitors and editors it stays one domain, no matter which application answers a request.
What changes for development?
For React developers the Next.js Renderer is a regular Next.js application with an SDK for Sitefinity. As prerequisites the documentation lists an LTS version of Node.js and a minimum version of Sitefinity; the exact versions are in the renderer’s setup guide. The SF_CMS_URL variable tells the application where to forward all requests that are not pages and where to send its API calls.
Widgets become React components. A widget consists of a .tsx component and an entity class. The entity describes the fields from which Sitefinity builds the dialog for editors:
import { WidgetEntity } from '@progress/sitefinity-widget-designers-sdk';
@WidgetEntity('HelloWorld', 'Hello world')
export class HelloWorldEntity {
Content: string | null = null;
}
A registry maps each widget to its component, and a flag decides whether it renders on the server or in the browser. One detail that is easy to miss: the widget needs the attributes from the SDK function htmlAttributes(props) on its root element, so that the editor recognises it as a widget and it is visible in edit mode.
Templates are created either in the Sitefinity backend or programmatically through layout files. To make a template a Next.js template, it has to be based on another programmatically created Next.js template or on the default Next.js template. Editors get a WYSIWYG editor that shows the page as it will appear on the front end. It requires a dedicated Next.js template for the page.
Why can pages only be moved one at a time?
The Progress documentation describes a hybrid approach: move one page at a time from MVC or Web Forms to the renderer, and with the page all widgets on it. There is no mixing on the same page. In the documentation’s words, ASP.NET Core or Next.js widgets cannot be rendered in MVC or Web Forms pages, and vice versa.
For running both side by side, the two renderers differ. The ASP.NET Core Renderer proxies MVC pages automatically. With the Next.js Renderer, all pages still built with MVC or Web Forms have to be submitted explicitly, for example through the SF_WHITELISTED_PATHS variable with a list of paths. This is how the documentation enables a page by page migration. To me this also makes the list a useful overview during a move of which pages are still on the old stack.
For the move itself Progress provides the Sitefinity CLI. It migrates the content and structure of page templates and pages, built-in widgets where an equivalent exists for the new renderers, forms and, with a separate command, form responses, and it requires Sitefinity 15.3 or later. It does not migrate custom widgets or custom code; those have to be implemented again. In a project with many custom widgets that is a large part of the work.
How do the existing Web Components fit in?
This is where a decision made long before the renderer pays off. The websites’ front end consists of Web Components built with StencilJS. In the MVC pages they appear as tags in the Razor views. To React they are HTML elements as well. A React widget can therefore use the same component that used to sit in Razor. Presentation, behaviour and accessibility come from the same library, whichever stack a page is on at the moment.
A new React widget can thereby become a thin layer: it reads the entity’s fields and passes them on to the Web Component. The real work stays in the component, which is developed and checked in Storybook.
What do you need to watch for with custom elements in React?
For a long time the combination was awkward, because React treated unknown props as attributes rather than properties. React 19 adds full support for custom elements. In the browser React assigns a prop as a property if the element has a property with that name, otherwise as an attribute. During server rendering only primitive values such as strings, numbers and true become attributes. Objects, functions and false are omitted there.
For a Next.js application that renders on the server this matters: without a wrapper, a component that expects data as an object does not get it in the first HTML. It is therefore worth designing component interfaces so that everything that has to be visible on first render arrives through simple attributes or slots.
React listens to custom events with the on prefix directly in JSX. Event names are case-sensitive and may contain dashes:
<my-element onsay-hi={console.log}></my-element>
Stencil also provides its own output target for React, which generates a React wrapper component for each component, with bindings for events and properties. The documentation lists React 17 and later and support for server-side rendering with frameworks such as Next.js. For rendering Stencil components on the server there is also the dist-hydrate-script output target. By default it outputs components with Shadow DOM as Declarative Shadow DOM.
What I take away from the move
- The renderer separates presentation and CMS cleanly, and as a proxy it keeps both under one domain.
- Pages move one at a time, always with all widgets of a page. With the Next.js Renderer the list of submitted legacy pages shows what is left.
- Custom widgets have to be rebuilt, the Sitefinity CLI does not migrate them.
- Web Components make the transition easier, because the same component runs in Razor and in React.
- In React 19 server rendering, only primitive values become attributes. That belongs in the design of component interfaces.