liveBook · Manning

Client-side rendering

As explained in Web Component Development with Modern Libraries and Tooling

Client-side rendering is the technique of generating or updating a web application's interface in the browser using client-executed code.

Client-side rendering creates or updates an interface in the browser. For web components, loading the component’s JavaScript lets the browser turn a custom-element tag into visible content and attach behavior. It can also keep an interface interactive after server-rendered HTML arrives. section 11.2 section 11.4

A greeting rendered in the browser

Load the component module, then place `<hello-world></hello-world>` and `<hello-world name="CSR"></hello-world>` in the page. The component supplies the greeting; changing the `name` attribute gives the second instance a different message. section 11.2 section 11.4

Listing 11.20 public/index.html
<!doctype html>
<html lang="en">
  <head>
    <title>Lit SSR Demo</title>
    <script src="./index.js" type="module"></script>
    <script src="./hello-world.js" type="module"></script>
  </head>
  <body>
    <p>The following components are client-side rendered:</p>
    <hello-world></hello-world>
    <hello-world name="CSR"></hello-world>
    <button>Greet</button>
    <div id="ssr-content"></div>
  </body>
</html>

The page places two client-rendered greetings beside a button and a separate target for server-rendered greetings. This makes the two rendering paths easy to compare. section 11.4

A vanilla component can create a shadow root and fill it with HTML when no shadow root exists. Its attribute callback handles a changed name, while its connection callback attaches a click handler. A shared content function can supply the HTML for both browser and server rendering. section 11.2

Choose the browser-side behavior that fits the component:

  • For a client-rendered radio group, listen for its change event and update other elements—for example, change greeting colors when a color is selected. section 11.3
  • For server-rendered content inserted later, still load the component’s JavaScript to define the element and support its behavior. In the vanilla approach, reuse an existing declarative shadow root rather than creating another. section 11.7 section 11.2
  • With Lit, server-side rendering can handle the first paint while client-side rendering handles later interactivity and updates. Its hydration-support import reuses existing declarative shadow roots instead of rendering that content twice. section 11.4

Start with a custom element on the page and its browser module. Add server rendering only when needed, and check how the client code handles markup the server has already supplied. section 11.2 section 11.4

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works through component markup, browser setup, shadow-root reuse, and event handling; concentrates on web components rather than general rendering algorithms.
Examples
Contrasts browser-rendered greetings and a color-selecting radio group with greetings and controls inserted from server responses.
What it assumes
Introduces project setup step by step while using JavaScript modules, HTML custom-element tags, and browser event listeners.
What you can do after
Build it from scratch with a vanilla web component, or apply it via Lit or wrec alongside server rendering.
How it connects
Connects web components, shadow DOM, declarative shadow DOM, server-side rendering, and hydration.

Also explained in

Compare how 5 books explain client-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.