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




