liveBook · Manning

Server-side rendering

As explained in Web Component Development with Modern Libraries and Tooling

The technique of generating a web application's HTML on a server before sending it to a client.

Server-side rendering (SSR) generates a web component’s initial HTML on a server and sends it to the browser with content already present. Without SSR, component code adds that content after the page loads. SSR can make the initial render faster, especially on older devices, and can make content more reliably available to search crawlers. It does not remove the need for client-side JavaScript when a component must respond to interaction. section 11.0 section 11.1

A greeting that arrives ready to display

A server can return a <hello-world name="SSR"> element containing a <template shadowrootmode="open"> with its style and <p>Hello, SSR!</p>. Declarative Shadow DOM (DSD) tells the browser to create the element’s shadow root from that template. The same content function can supply HTML for server rendering and for a client-rendered instance. section 11.2

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>
  `;
}

The small ssr function shows the essential wrapper: a custom element containing a DSD template filled with initial content. section 11.2

For a vanilla component that also runs in the browser, its constructor checks for an existing shadow root before attaching one. That lets a server-rendered instance keep its initial DOM, while a client-rendered instance creates its own. Client-side code can then add behavior, such as responding when the greeting is clicked. DSD gained cross-browser support in early 2024. section 11.2 section 11.0

Rendering a radio group

A server endpoint can return a <radio-group> whose DSD template already contains labeled radio buttons and a selected value. After that HTML is inserted, client-side code can listen for the component’s change event. This separates an immediately visible selection from the JavaScript that responds to later changes. section 11.3

Choose an implementation that fits the component:

  • With vanilla web components, generate the custom-element markup and DSD template yourself; a shared content function can serve both server and client rendering. section 11.2
  • With Lit, use its SSR package for the first render and its client hydration support to reuse the DSD shadow root rather than render the content twice. section 11.4
  • With Stencil, generate a hydrate app and call renderToString; this route involves many setup steps. With wrec, a component exposes an ssr method that returns markup. section 11.5 section 11.7

SSR is a choice, not a requirement for every component. The performance cost of rendering web components in the browser often is not noticeable, although search crawlers do not all guarantee indexing of content added after load. SSR also creates an interactivity gap: a page may look ready before its JavaScript can respond. For content that never changes, ordinary server-generated HTML may be simpler than a web component. section 11.1

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works through vanilla DSD markup and client behavior, then outlines Lit, Stencil, and wrec setup and rendering calls.
Examples
Shows a greeting component returned by an HTTP endpoint and a radio group whose server-rendered buttons gain change handling.
What it assumes
Assumes familiarity with custom elements and browser-side JavaScript while demonstrating server and client rendering together.
What you can do after
Build it from scratch with a DSD template and shared content function, or apply it via Lit, Stencil, or wrec.
How it connects
Connects Declarative Shadow DOM, custom elements, shadow roots, client-side rendering, hydration, and search engine optimization.

Also explained in

Compare how 13 books explain server-side rendering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.