Web Components with StencilJS in an existing CMS
A grown front end inside a CMS can be replaced without taking the websites offline: with Web Components that the CMS delivers like plain HTML, and a switch that lets the old and the new solution run side by side for a while. In this article I describe how I do this for an energy provider’s websites with StencilJS, Sitefinity and Storybook, which decisions have to be made early and how WCAG accessibility gets into the components. I have been working on this project with the team since 2024.
Why Web Components in a server-rendered CMS?
In the project Sitefinity renders pages on the server with ASP.NET MVC and Razor. In projects like this the front end is often tightly woven into the templates: markup, styles and scripts are spread across many views. A new design then means touching every one of those places.
Web Components solve the problem in the right place. A component is an HTML element of its own, for example <my-teaser>, which the browser knows as soon as it is registered. To Razor it is just a tag with attributes. The CMS stays responsible for content and page structure, the component for presentation and behaviour. This lets you develop, version and test the front end as a package of its own, separate from the .NET code.
There is a second benefit: Web Components are not tied to a framework. The same component runs in a Razor view, in an Angular application and in React. In the project the components also work together with Angular applications.
What does StencilJS produce, and which output target fits?
StencilJS is a compiler. Components are written with TypeScript, JSX and decorators, and the result is standard custom elements. The output targets in stencil.config.ts decide what shape that result takes:
distproduces a library that lazy-loads itself. A loader registers all components, but a page only loads the code of the components it actually uses.dist-custom-elementsproduces elements that directly extendHTMLElement. This target is meant for projects that handle bundling and registration themselves, such as a framework application with its own build.wwwis aimed at websites and web apps served directly from an HTTP server.
For a CMS in which editors assemble pages from many components, I consider the lazy loading of dist a good default: a page with three components does not load the whole library.
A component in Stencil looks like this:
import { Component, Event, EventEmitter, Prop, h } from '@stencil/core';
@Component({ tag: 'my-teaser', styleUrl: 'my-teaser.css', shadow: true })
export class MyTeaser {
@Prop() heading: string;
@Event() teaserSelected: EventEmitter<string>;
render() {
return (
<article>
<h2>{this.heading}</h2>
<slot />
<button onClick={() => this.teaserSelected.emit(this.heading)}>More</button>
</article>
);
}
}
@Prop defines the public interface; heading becomes the heading attribute in HTML, and camelCase becomes dash-case. @Event dispatches a DOM event that the page or another component can react to. Internal state uses @State, public methods use @Method, and those methods must return a promise, which usually means they are async.
How do the components get into the Razor views?
In the Razor view the component is a tag, and the values come from the model:
<my-teaser heading="@Model.Title">
@Html.Raw(Model.Text)
</my-teaser>
This is the first typical pitfall. Attributes only bring strings into the component. Stencil converts them for props of type boolean and number, but it explicitly does not try to turn object-like strings in HTML into a JavaScript object. Complex data such as lists or configuration therefore needs another route: flat attributes, nested child elements or a script that sets the data as a property. I make this decision deliberately for each component, because it defines the interface to the CMS.
The second point is content from the rich text editor. It reaches the component through a slot. With Shadow DOM, slots are a native browser feature. Without Shadow DOM, Stencil emulates them, and the documentation points out that this can cause issues. That is an argument for building components with slots on Shadow DOM.
Shadow DOM or scoped?
Stencil offers two kinds of encapsulation. With shadow: true the component uses native Shadow DOM: selectors from the page do not reach in, styles from inside do not leak out. Inherited properties such as font and colour, however, are still inherited from the host element. With scoped: true Stencil rewrites the styles so they only apply inside the component. According to the documentation, however, this does not prevent styles from the page from seeping into the component.
In a CMS with grown CSS this is decisive. As long as old and new run side by side, old global styles sit on the same page as the new components. Shadow DOM protects the components from their selectors. That is why inherited values such as font and colour are best set on the component’s own :host. If you want to adjust something specific from outside, you open individual places with ::part() instead of weakening the encapsulation.
How do old and new run side by side?
Replacing a front end in one step means switching all websites at once and finding all bugs at once. The project therefore has a switch in C# that allows the old and the new solution to run in parallel. This way the portals can be moved step by step while they stay live.
For this kind of transition I consider three things important:
- The switch belongs on the server. A page is then delivered with either the old or the new front end, and nothing has to be switched in the browser.
- Old and new should share no styles and no global JavaScript. Shadow DOM helps here.
- The content in the CMS stays the same wherever possible. Editors keep working in the same place, only the presentation changes.
What role does Storybook play?
In the project Storybook is the workbench and the documentation of the component library, and I maintain and look after it. All states of a component belong there: with long and short text, with and without an image, in an error state. That is faster than creating the same cases in the CMS, and it shows bugs before a component reaches a Razor view.
Storybook ships a framework for Web Components, @storybook/web-components-vite, and Stencil’s own documentation describes a setup with the @stencil/storybook-plugin plugin, which registers the component of each story. You only add the loader from the build on top if components use other Stencil components. Especially for a library that runs in several environments, I value a place where each component is visible on its own more than written documentation.
How does WCAG accessibility get into the components?
Since January 2025 part of my work has been making the existing websites and components accessible according to WCAG. The current version is WCAG 2.2, a W3C Recommendation since October 2023. For components these success criteria matter most:
- 2.1.1 Keyboard: all functionality must be operable with a keyboard.
- 2.4.7 Focus Visible: the keyboard focus must be visible.
- 2.4.11 Focus Not Obscured (Minimum), new in 2.2: a focused element must not be entirely hidden by other content, such as a fixed header.
- 1.4.3 Contrast (Minimum): at least 4.5:1 for normal text, 3:1 for large text.
- 4.1.2 Name, Role, Value: the name and role of every control must be programmatically determinable, states and values that users can change must be programmatically settable, and changes to them must be reported to assistive technologies.
The last one is the trickiest for Web Components. Native HTML controls already meet 4.1.2 when used according to the specification. Custom controls need additional work. That is why, inside a component, I build on real button, a and input elements wherever possible instead of recreating behaviour on divs.
Stencil helps in two places. With shadow: { delegatesFocus: true } a component passes focus on to its first focusable element. With formAssociated: true and the @AttachInternals decorator, a component takes part in forms through the ElementInternals API since Stencil 4.5: it is linked to the surrounding form automatically, and it sets its value and validity itself with setFormValue and setValidity. Both build on browser features instead of recreating focus handling and form integration yourself.
Retrofitting accessibility is more work than building it in from the start. The advantage of a component library: a fix to one component takes effect on every page that uses it.
What I watch for in a rebuild like this
- Web Components separate the front end from the CMS without replacing the CMS.
- Decide on the output target early:
distwith lazy loading fits pages that editors assemble freely. - Design the interface to Razor deliberately. Attributes only carry simple values, no objects.
- Shadow DOM protects the new components from the selectors of the old CSS while both worlds run side by side.
- A switch on the server makes the transition gradual, portal by portal.
- Accessibility belongs in the component, not on every page.