Declarative Shadow DOM (DSD) lets an HTML response provide a web component’s shadow content before its JavaScript runs. This makes server-side rendering possible for web components: visitors can receive populated custom elements instead of waiting for browser-side code to add their initial content. section 11.0 section 11.2
A greeting rendered by the server
The server can return `<hello-world name="SSR"><template shadowrootmode="open"><style>:host { color: blue; }</style><p>Hello, SSR!</p></template></hello-world>`. The template holds the initial styling and greeting. Its `shadowrootmode` attribute tells the browser to create a shadow root for the custom element; inspecting the result shows the paragraph and style inside that root. section 11.2
For a component that also renders on the client, share a function that generates its inner HTML. The server places that HTML inside the declarative template. In the component constructor, check `this.shadowRoot`: if it already exists, keep it; otherwise call `attachShadow` and populate it for client-side rendering. The component can then add behavior to the existing content. section 11.2
Two practical distinctions matter:
- DSD supplies initial content; interactive behavior can still be attached by client-side code. A server-rendered radio group, for example, gains a change listener after it is inserted. section 11.3
- HTML assigned with `innerHTML` does not activate declarative shadow roots. For that insertion path, `setHTMLUnsafe(htmlString)` triggers DSD parsing, but support for this method only became complete in September 2025. section 11.4
- With Lit, client hydration support reuses existing DSD roots; without that import, the example renders the DSD content twice. section 11.4
Listing 11.2 public/hello-world-html.js ssr function
export function ssr(name) {
const attr = name ? `name="${name}"` : "";
return html`
<hello-world ${attr}>
<template shadowrootmode="open">${content(name)}</template>
</hello-world>
`;
}
See how the vanilla SSR function wraps shared component content in `<template shadowrootmode="open">`. section 11.2
Use DSD when a web component needs server-rendered initial content, particularly when initial rendering performance or search visibility matters. It works with vanilla components and has support in Lit, Stencil, FAST, and wrec; SSR is not necessarily worthwhile for every application. section 11.0 section 11.8

