Skip to main content
Version: v5 (Preview)

SSR Output Target

The ssr output target builds a server-side rendering module for your component library. Given an HTML string or a document, it renders every Stencil component inside it and returns the resulting HTML. Shadow DOM components are rendered as Declarative Shadow DOM by default, and the browser hydrates the markup once your components' JavaScript loads.

The module has no Node.js dependencies, so it runs in Node.js 22+, Deno, Bun, Cloudflare Workers, and other runtimes with web-standard APIs. To render from a non-JavaScript server, see ssr-wasm. To decide between rendering on request and at build time, see Rendering Strategies.

stencil.config.ts
outputTargets: [
{
type: 'ssr',
},
];
note

Formerly dist-hydrate-script in Stencil v4.

To render every page of a site built with the www output target to HTML at build time, see Prerendering.

Output​

dist/ssr
├── index.js
├── index.d.ts
├── ssr-prop.js
└── ssr-prop.d.ts

index.js is the server-side rendering module. ssr-prop.js exports ssrProp(), for framework templates; it runs in the browser too, so it's separate.

Expose both to consumers through your package's exports (generateExportMaps adds them for you):

package.json
{
"exports": {
"./ssr": {
"types": "./dist/ssr/index.d.ts",
"import": "./dist/ssr/index.js"
},
"./ssr-prop": {
"types": "./dist/ssr/ssr-prop.d.ts",
"import": "./dist/ssr/ssr-prop.js"
}
}
}

Rendering​

import { renderToString } from 'my-library/ssr';

const { html } = await renderToString(`
<html>
<body>
<my-button variant="primary">Save</my-button>
</body>
</html>
`);

The module exports three rendering functions:

FunctionReturnsUse
renderToString(html, options?)Promise<SsrResults>Render an HTML string or document and serialize it. The rendered HTML is in results.html.
streamToString(html, options?)ReadableStream<string>The same as renderToString, as a stream.
ssrDocument(doc, options?)Promise<SsrResults>Render into an existing document without serializing it (results.html is null). Use this when your server already has a Document and serializes it itself.

streamToString() emits strings. To use it as a web Response body, encode it first:

import { streamToString } from 'my-library/ssr';

export default {
async fetch(request: Request) {
const html = await loadPageTemplate(request);
const body = streamToString(html).pipeThrough(new TextEncoderStream());
return new Response(body, { headers: { 'content-type': 'text/html' } });
},
};

The module also exports setTagTransformer(), for when components are registered under transformed tag names on the client, so the server renders the same tags.

Using It in a Server Framework​

The ssr module renders Stencil components in any HTML string, so it works with any framework that lets you see a page's HTML on the server before it's sent. The browser then hydrates the rendered components, as long as the page also loads your components' client-side JavaScript, from loader-bundle or standalone.

Most frameworks let you run code on each response, usually as middleware or a response hook. Pass the page's HTML through renderToString() there:

import { renderToString } from 'my-library/ssr';

// the shape of this hook varies by framework
async function onResponse(response: Response): Promise<Response> {
if (!response.headers.get('content-type')?.includes('text/html')) {
return response;
}
const { html } = await renderToString(await response.text());
return new Response(html, response);
}

renderToString() needs the whole document, so run it on the complete response, not on streamed chunks.

Define the Components Before the Framework Hydrates​

The framework hydrates its own markup in the browser, and the Stencil components hydrate theirs. Load your components first, and wait for them: defineCustomElements() is async.

import { defineCustomElements } from 'my-library/loader';

await defineCustomElements();
// then let the framework hydrate

A scoped or unencapsulated component's server-rendered light DOM holds its own nodes next to the framework's slotted content. Once the component is defined, Stencil's light DOM patches make firstChild, childNodes, textContent and the like report only the slotted content, so the framework finds the DOM it rendered. If the framework hydrates first, it finds Stencil's nodes instead, reports a hydration mismatch, and usually replaces the elements.

Some frameworks read the DOM in ways the patches can't reach. Svelte 5, for example, walks a node's children with the browser's own firstChild and nextSibling, so text it updates inside a scoped or unencapsulated component's slot doesn't hydrate. Use shadow encapsulation for components that take framework-reactive slot content.

Next.js​

React Server Components render each component separately, so there's no single page of HTML to pass through a hook. The React output target generates server versions of your wrappers instead: see React: Server-Side Rendering.

Working Examples​

The ssr-examples repository renders the same page with the same component library in several frameworks. Each example shows where renderToString() hooks in, how to define the components before the framework hydrates, and how to pass objects and arrays with ssrProp():

FrameworkWhere Stencil hooks inExample
Next.js (App Router)the React output target's server wrappersnext
Nuxta Nitro render:response hooknuxt
Astromiddlewareastro
SvelteKitthe handle hooksveltekit
SolidStartmiddlewaresolid-start
React Routerentry.server.tsxreact-router
TanStack Starta custom server entrytanstack-start
Angularserver.tsangular
Eleventya transform, at build timeeleventy
None (Node.js)a plain node:http servernode

For servers in other languages, see ssr-wasm.

Writing SSR-Friendly Components​

Most components render on the server without changes: Stencil's server runtime implements the DOM APIs components normally use. The points below cover what does need care, for both server-side rendering and prerendering.

Browser-Only Code​

Code that needs a real browser, such as measuring layout or reading localStorage, should only run there. Check Build.isBrowser:

import { Build } from '@stencil/core';

connectedCallback() {
if (Build.isBrowser) {
this.width = this.el.getBoundingClientRect().width;
}
}

Build.isServer is the inverse. Both are resolved at build time: the browser build drops server-only branches, along with anything only they import, and the server build drops browser-only ones.

Async Data​

The server waits for a component's componentWillLoad() to finish before rendering it. Return a promise to render with fetched data:

async componentWillLoad() {
const response = await fetch('https://example.com/api/items');
this.items = await response.json();
}

Complex Props​

HTML attributes only carry strings, so a prop whose value isn't a string, number or boolean (an array, an object, a Map, a Date, a class instance) has two gaps to cross: into the server render, and from the server to the browser.

Into the server render. From a framework template, wrap the value in ssrProp(). Frameworks drop objects when they write HTML, or write "[object Object]":

<script setup>
import { ssrProp } from 'my-library/ssr-prop';
</script>

<my-menu :items="ssrProp(items)" />

In the browser, ssrProp() returns the value. On the server, it keeps the value and returns a reference that the template renders as an attribute. The ssr module swaps the reference for the value while it sets the component up, so nothing is serialized: a Map, a Date or a class instance arrives as it is. Bind it under the prop's name or its dash-case (chartData or chart-data). It only works when the page is rendered in the same process, shortly after the template ran, which is the case with a server framework hook.

Outside a framework template, set the property in the beforeSsr hook:

await renderToString(html, {
beforeSsr(doc) {
doc.querySelector('my-menu').items = menuItems;
},
});

To the browser. The browser has only the HTML, so the component has to carry the value in its attribute. For JSON, use @Prop({ serialize: 'json' }):

@Prop({ serialize: 'json' }) items: MenuItem[] = [];

For any other format, write a @PropSerialize() / @AttrDeserialize() pair. Either way, the server writes the value to the attribute and the browser reads it back before the component first renders, whether the value came from ssrProp(), beforeSsr, a framework wrapper, or the component itself.

If an object prop is set from outside on the server and the component has no serializer for it, renderToString() adds a warning to its diagnostics: the browser won't get the value, so the component first renders there without it.

The ssr-wasm output target receives only HTML, so there the value must be in the attribute from the start.

Reserved Attribute Names​

Server rendering writes s-id, c-id, s-sn and s-mode attributes. A prop whose attribute has one of those names (@Prop() sId is s-id) is a build error when there's an ssr or ssr-wasm output target. Rename the prop, or give it another attribute with @Prop({ attribute: '...' }).

Style Size​

With Declarative Shadow DOM, each instance of a shadow DOM component carries its own copy of the component's styles. A component used many times on a page can add a lot of repeated CSS. To avoid this:

Components with encapsulation: { type: 'scoped' } or { type: 'none' } don't have this cost: their styles are always sent once.

Config​

dir​

default: dist/ssr

Where the module is written.

cjs​

default: false

Also write a CommonJS build, as index.cjs.

external​

default: []

Module IDs to leave as imports instead of bundling into the module. fs, path, and crypto are always external.

minify​

default: false

Minify the module.

empty​

default: true

Empty dir before each build.

skipInDev​

default: true, or false when the dev server runs with --ssr

Skip this output during development builds.

Render Options​

Every rendering function accepts these options:

OptionDescriptionDefault
serializeShadowRootHow shadow DOM components are rendered - see below.'declarative-shadow-dom'
staticComponentsTags to render on the server but never hydrate on the client. Useful for components with no client-side behavior, such as a footer.
excludeComponentsTags to skip entirely. They're treated like any unknown element, on the server and the client.
clientSsrAnnotationsInclude the comments and attributes the client uses to hydrate the rendered markup.true
urlSets location.href.
titleSets document.title.
languageSets the lang attribute on <html>.
directionSets the dir attribute on <html>.
canonicalUrlSets the href of the <link rel="canonical"> tag.
cookieSets document.cookie.
referrerSets document.referrer.
userAgentSets navigator.userAgent.
resourcesUrlThe base URL components use to resolve assets.'./'
removeScriptsRemove every <script> from the document.false
removeUnusedStylesRemove CSS not used by any element in the document.true
maxHydrateCountThe most components rendered on one page.300
timeoutMilliseconds to wait for rendering to finish before failing.15000
buildIdAdded as <html data-stencil-build="...">.random
runtimeLoggingPrint component console output to the server's console.false
reuseWindowRender into one long-lived window instead of creating one for every call. Faster - see below.false

renderToString() and streamToString() also accept options for the serialized HTML:

OptionDescriptionDefault
fullDocumentSerialize a complete document. Set to false to return only the rendered components, preceded by the <style> elements their scoped styles need.true
beforeSsr(document)Runs before rendering. Use it to set properties that can't be expressed as attributes.
afterSsr(document)Runs after rendering, before serialization. Setting component props here doesn't change the output: the components have rendered already.
prettyHtmlIndent the output.false
approximateLineWidthWrap lines at about this width, where the HTML allows it.no wrapping
removeHtmlCommentsRemove comments. Stencil's own hydration comments are kept.false
removeAttributeQuotesRemove quotes from attribute values where possible.false
removeBooleanAttributeQuotesWrite boolean attributes as hidden instead of hidden="".false
removeEmptyAttributesRemove empty class, dir, id, lang, name, and title attributes.true

beforeSsr is one way to pass complex data to a component on the server; see Complex Props:

await renderToString(html, {
beforeSsr(doc) {
doc.querySelector('my-chart').data = new Map(/* ... */);
},
});

serializeShadowRoot​

Controls how encapsulation: { type: 'shadow' } components are rendered:

  • 'declarative-shadow-dom' - inside a <template shadowrootmode="open">, including the component's styles.
  • 'scoped' - as light DOM with scoped styles, written once to the document <head>. The client converts it to a real shadow root during hydration.
  • An object, to choose per component:
    serializeShadowRoot: {
    scoped: ['my-button'],
    default: 'declarative-shadow-dom',
    }
  • false - don't render shadow DOM components on the server.

Declarative Shadow DOM repeats a component's styles in every instance. For a component used many times on a page, 'scoped' sends its styles once.

reuseWindow​

Every render normally gets a new mock window, and sets up Stencil's server runtime and all your components in it. That setup is most of the cost of rendering a small fragment, and a good part of a whole page. With reuseWindow: true, renders share one long-lived window instead. It's reset between renders and keeps the runtime it already set up.

const { html } = await renderToString('<my-card heading="Hi"></my-card>', {
fullDocument: false,
reuseWindow: true,
});
  • It applies when the input is an HTML string. A Document passed to ssrDocument() is rendered as it is.
  • Each distinct serializeShadowRoot value gets its own window.
  • Renders that share a window run one at a time. Calls you make concurrently wait for each other, so for parallel rendering, use several worker threads or processes.
  • setTagTransformer() discards the reused windows, so later renders pick up the new tag names.

Output is the same as without reuseWindow. It's worth turning on whenever you render many fragments or pages in one process, such as one call per component from a template helper.

Results​

renderToString() and ssrDocument() resolve to an object that includes:

PropertyContents
htmlThe rendered HTML (null from ssrDocument())
diagnosticsErrors and warnings from rendering
componentsEvery component rendered
hydratedCountHow many components were rendered
titleThe document's title
httpStatus200, or the value of a <meta http-equiv="status"> tag if the page has one - for example, set by a not-found component
anchors, imgs, scripts, stylesElements found in the rendered document