Server rendering
ARC UI components server-render through @lit-labs/ssr intodeclarative shadow DOM: a <template shadowrootmode="open">the browser turns into a real shadow root while parsing, before any JavaScript runs.
All 195 custom elements (components and their child items) render this way. Server rendering is optional. A page with no build step, or one loading ARC UI from a CDN, works without it.
What it fixes
Many ARC UI components take their content from attributes, not slots. Before JavaScript loads, or without it, a tag like this one is empty. There's no heading or description, and nothing for a crawler to read.
<arc-feature-card
heading="Fast by default"
description="Ships 40% less JavaScript"
></arc-feature-card>
Here is the same tag after the server renderer, with Lit's hydration markers left out. The content is in the HTML response:
<arc-feature-card
heading="Fast by default"
description="Ships 40% less JavaScript"
>
<template shadowrootmode="open">
<link rel="stylesheet" href="/_arc/s-y0rti11lg82db.css">
<div class="card" part="card">
<div class="card__inner" part="inner">
<h3 class="card__title" part="title">
Fast by default
</h3>
<p class="card__desc" part="description">
Ships 40% less JavaScript
</p>
</div>
</div>
</template>
</arc-feature-card>
71 of the 195 elements have no slots at all, so without server rendering their content isn't in the HTML. Components that take their content from slots already have it in the page, and gain less.
Rendering and hydrating
On the server, render as you would any Lit template:
import { render } from '@lit-labs/ssr';
import { collectResult } from '@lit-labs/ssr/lib/render-result.js';
import { html } from 'lit';
import '@arclux/arc-ui/feature-card';
const markup = await collectResult(render(html`
<arc-feature-card heading="Fast by default"></arc-feature-card>
`));
On the client, import the hydration entry before any component:
import '@arclux/arc-ui/hydrate'; // first — order matters
import '@arclux/arc-ui/register';
Without it, when an element upgrades, Lit renders its template from scratch into the shadow root the server already filled. That throws away identical DOM and causes the flash server rendering is meant to prevent. With it, Lit adopts the existing nodes and renders nothing. The order matters because hydration support patchesLitElement's update path, and a component class defined before the patch never gets it.
The FOUC guard
base.css hides ARC UI elements until they upgrade, so a page doesn't flash unstyled custom elements. A server-rendered element hasn't upgraded yet but is alreadyfinished, so that rule would hide its content until hydration. Server rendering would then give you a longer blank screen instead of a shorter one.
Opt the page out at the root:
<html data-arc-ssr>
You have to set it yourself because CSS can't detect a declarative shadow root. The browser consumes the <template> during parsing and leaves nothing a selector can match.
Component coverage
pnpm check ssr renders every element in Node and fails on any that throws. It runs in CI. On the server, Lit runs the constructor, willUpdateand render, and none of connectedCallback,firstUpdated, updated, or a reactive controller'shostConnected. Browser work belongs in those.
All 195 pass, so none of them has to be client-only.
A second test checks that every component's docs preview, server-rendered, looks the same before its script arrives as after. That includes components built from their children: breadcrumbs, navigation menus, tabs, accordions and lists arrive with their items, and nothing moves when the script loads. The two exceptions depend on the current time: arc-clock shows it, and arc-activity-heatmapwithout an end-date ends today. Only the browser knows either.
Client-side routers
A router that fetches the next page and parses it with DOMParser gets declarative shadow roots as inert templates: only the page parser attaches them. Astro's<ClientRouter /> is one. Attach them before the swap:
import { attachShadowRoots } from '@arclux/arc-ui/shadow-roots';
document.addEventListener('astro:before-swap', (e) => attachShadowRoots(e.newDocument));
Without it, every component on the incoming page renders a fresh copy next to an inert template, and nothing hydrates.
Framework support
@arclux/arc-ui/ssr doesn't need a framework's component graph. A framework's server render produces HTML, and every <arc-*> in it can be rendered to a declarative shadow root from the markup alone. So it takes HTML and returns HTML, whatever produced it.
import { renderDeclarativeShadowDOM } from '@arclux/arc-ui/ssr';
const { html, stylesheets } = await renderDeclarativeShadowDOM(pageHtml);
// serve html; write each stylesheet under /_arc
- Nuxt, SvelteKit, Angular Universal, Next, Astro, or a string you built by hand: pipe the rendered HTML through it. This site's Astro build runs every page through it. The others have no test or example in this repo.
- React:
@lit-labs/ssr-reactalso works, if you would rather render through the component graph.
It renders any Lit element defined in the process, including other libraries'. Import their registration before rendering (@arclux/brand/register, your own components) and their elements are rendered too.
It needs @lit-labs/ssr, an optional peer dependency. Install it in the project that server-renders. You serve the returned stylesheets from the path they were linked with, and you import @arclux/arc-ui/hydrate on the client before any component is defined. On a bundler that usually means forcing it into its own chunk, since an import statement alone does not control evaluation order.