A server rendering HTML - SSR

Implementing Your Own SSR Server for Web Components

Server Side Rendering (SSR) is a very hot topic today. What’s with React Server Components bringing all these buzz words that “I just have to implement in my project”… Let’s see what’s all the fuss about by implementing our own SSR server for web components.

At Vonage we have a public project called Developer Portal. It’s a documentation website that’s not behind a login page (a.k.a. public) and holds mostly content. We also want the content to be Search Engines Optimized (SEO). This makes it a good candidate for SSR.

The developer portal is written in Vue and is served using Nuxt. Nuxt allows for SSR via its Universal Rendering mechanism. We needed to allow Nuxt to also SSR our design system’s web components. Hence started our journey of building an SSR mechanism for Web Components.

What is SSR?

In a nutshell – SSR is the process in which we run our app on a server and return plain HTML to the client. 

In our portal, the vue code is rendered on a nodejs (nuxt) server. The output of the rendering is HTML (with possibly inlined CSS). This HTML (+CSS) is sent to the browser and shown there – without any JavaScript. Hence, the user gets to see the website really fast.

In addition, showing the website’s layout as it should be with JavaScript avoids heavy layout shifts resulting from components suddenly getting content and expanding once JavaScript kicks in.

Note that bots (such as search engine crawlers) usually don’t see JavaScript, so getting this bunch of contentful HTML right away could do wonders for your search engine ranking.

The one caveat here is that without JavaScript, we have no functionality or interactivity. So, the user gets to see the website but not interact with non-native functionality. Forms, links, videos, etc., should work in non-complex examples. 

Remember that the Developer Portal is mostly documentation? This is a classic example for when SSR is truly needed. Documentation is mostly just text and images shown to the user. The interactivity is mainly scrolling to see more of the text and images. You can call this “thin” view layer as the dehydrated version of our application.

What if we need to interact with the page? We will need to hydrate our components. Hydration is a marketable name for “load our JavaScript”. Once JavaScript loads, we get our functionality.

So in essence, SSR helps us load our content faster so users can consume it – but not interact with it. It also contributes to our SEO ranking.

How to Build Your Own SSR Server?

The first thing I recommend to most people is: Don’t Build Your Own SSR Server.

Having said that, in this article we will build our own server to learn the mechanics behind the SSR paradigm and its possible extensions. Understanding how SSR works will help you extend current SSR solutions to fit your needs. For instance, you’d might find yourself in need to  SSR web components in a nuxt server.

Now that we understand the usefulness of building an SSR server (or lack thereof 😉 ), let’s build one for learning purposes.

An SSR server is essentially an HTTP server that receives a request from the client, and through this request parses a template and returns HTML to the client.

Here’s an illustration of the process:

From this we can define the building blocks of our server:

  1. An HTTP server that handles routes 
  2. A rendering function

Setting Up the HTTP Server

The HTTP server is pretty simple:

The code is folded for readability purposes.

On line 13 we create the server. 

On line 46 we set the server to listen on a port, so we can access the server in localhost:3000

The full code for the server is here:

import http from 'http';
import fs from 'fs';
import path from 'path';
const CONTENT_TYPES = {
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/png',
'.gif': 'image/png',
};
function returnFileContent(filePath, contentType) {
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end(`Server Error: ${err.code}`);
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}
const server = http.createServer((req, res) => {
let filePath = '.' + req.url;
if (filePath === './') {
filePath = 'HomePage';
}
const extname = path.extname(filePath);
let contentType = CONTENT_TYPES[extname] ?? 'text/html';
if (contentType === 'text/html') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('Hello World', 'utf-8');
} else {
returnFileContent(filePath, contentType);
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}/`);
});
view raw index.mjs hosted with ❤ by GitHub

The createServer callback handles the request. It gets the URL of the request and parses it.

If the extension’s name is one of CONTENT_TYPES (defined in line 5), it just returns the file with the content type in the header (the logic of returnFileContent defined in line 13).

In any other case, we return text/html.

We return only ‘Hello World’, but we will change that momentarily.

A Simple Routing

According to our specification, the server needs to accept routes and handle them. The routes will be URLs like: localhost:3000/home-page. We’ll use a simple hash to create our router.

In our project we’ll have a routes folder that’s going to hold an index.mjs file.

Beside it we will create a home-page folder in which the homepage route will reside. It’ll look like this:

home-page will hold its own index.mjs file:

This HomePage object will also be exported from the routes/index.mjs file:

Now we just need to implement getHomePageTemplate in home-page.template.mjs:

export function getHomePageTemplate() {
return `
<div>Hello World</div>
`;
}

Finally, we need  to use the route in our server, so we will change the main index.mjs file:

import http from 'http';
import fs from 'fs';
import path from 'path';
import * as routes from './routes/index.mjs';
const CONTENT_TYPES = {
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/png',
'.gif': 'image/png',
};
function returnFileContent(filePath, contentType) {
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end(`Server Error: ${err.code}`);
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}
const server = http.createServer((req, res) => {
let filePath = '.' + req.url;
if (filePath === './') {
filePath = 'HomePage';
}
const extname = path.extname(filePath);
let contentType = CONTENT_TYPES[extname] ?? 'text/html';
if (contentType === 'text/html') {
res.writeHead(200, { 'Content-Type': contentType });
res.end(routes[filePath].template(), 'utf-8');
} else {
returnFileContent(filePath, contentType);
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}/`);
});
view raw index.mjs hosted with ❤ by GitHub

Here we import the routes (line 5) and use the routes when we return text/html (line 41).

The results are astounding!

Let’s Add a Better Template

This template is quite boring… let’s return something spicy. For this, I’ll use the Vivid design system. Vivid components are pure web components. We will use them to spice up our template and render them server side. 

In Vivid’s button component page we can take the appearance example which exhibits four different buttons:

We can replace our template in home-page.template.mjs: with the example code:

export function getHomePageTemplate() {
return `
<vwc-button label="ghost" appearance="ghost"></vwc-button>
<vwc-button label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button label="filled" appearance="filled"></vwc-button>
<vwc-button label="outlined" appearance="outlined"></vwc-button>
`;
}

And the result here is:

A blank page beside a not-so-empty body. Where are the components from the code example?

They do not load because they require us to load JS and CSS.

How to Load CSS and JavaScript?

This is usually a trivial question – but how is it done in an SSR server?

Let’s go for the simplest way to do this by using a CDN. You can import Vivid components by using this convention:

https://unpkg.com/@vonage/vivid@latest/{pathToFile}

Using this, we can import our code in the template:

export function getHomePageTemplate() {
return `
<style>
@import "https://unpkg.com/@vonage/vivid@latest/styles/tokens/theme-light.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/core/all.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/fonts/spezia-variable.css";
</style>
<vwc-button label="ghost" appearance="ghost"></vwc-button>
<vwc-button label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button label="filled" appearance="filled"></vwc-button>
<vwc-button label="outlined" appearance="outlined"></vwc-button>
<script type="module" src="https://unpkg.com/@vonage/vivid@latest/button"></script>
`;
}

If we go test our client we will see our components. Well… kinda:

One thing we need to make Vivid components to work is to add the vvd-root class to the element that wraps the components (usually the body…).

Let’s define a wrapper to our template:

export function getHomePageTemplate() {
return `
<style>
@import "https://unpkg.com/@vonage/vivid@latest/styles/tokens/theme-light.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/core/all.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/fonts/spezia-variable.css";
#buttons-wrapper {
min-width: 50px;
min-height: 50px;
background-color: crimson;
}
</style>
<div id="buttons-wrapper" class="vvd-root">
<vwc-button label="ghost" appearance="ghost"></vwc-button>
<vwc-button label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button label="filled" appearance="filled"></vwc-button>
<vwc-button label="outlined" appearance="outlined"></vwc-button>
</div>
<script type="module" src="https://unpkg.com/@vonage/vivid@latest/button"></script>
`;
}

Here’s the outcome:

So the buttons work but… can you see the issue?

The HTML loads – as we can see from the wrapping div – and then the buttons render once the JS kicks in, creating a major layout shift. Imagine this happening in a bigger app with a lot more components. 

How can we prevent this flash? Let’s render the components on the server!

Creating the Rendering Function

Instead of loading the JS on the client side, we can render the components on the server and send a complete HTML. So we need to find a way to render our components on the server as if they were in a browser.

Every framework has a different rendering method. 

Web components are rendered natively by the browser. Web components also bring the idea of shadow DOM. In essence, the shadow DOM is a document fragment in which you can add HTML and CSS. For this, browser creates a shadow root inside our component:

Everything outside the shadow-root is “in the light” while the rest is in the shadow. The advantage of a shadowDOM is that it encapsulates the styles. Styles inside do not affect anything outside and (almost completely) vice-versa.

That means that if we take our template and set it as the innerHTML of a div, we should get rendered components. Let’s try that in the browser:

const div = document.createElement('div');
div.innerHTML = `
<style>
@import "https://unpkg.com/@vonage/vivid@latest/styles/tokens/theme-light.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/core/all.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/fonts/spezia-variable.css";
#buttons-wrapper {
min-width: 50px;
min-height: 50px;
background-color: crimson;
}
</style>
<div id="buttons-wrapper" class="vvd-root">
<vwc-button label="ghost" appearance="ghost"></vwc-button>
<vwc-button label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button label="filled" appearance="filled"></vwc-button>
<vwc-button label="outlined" appearance="outlined"></vwc-button>
</div>
<script type="module" src="https://unpkg.com/@vonage/vivid@latest/button"></script>
`;
document.body.appendChild(div);

If you paste this code into your browser, you should see the crimson div without the button because the JS would not be imported.

Nevertheless – if you’d have imported the JS beforehand, it would have worked:

const script = document.createElement('script');
script.type = 'module';
script.src = 'https://unpkg.com/@vonage/vivid@latest/button';
const div = document.createElement('div');
div.innerHTML = `
<style>
@import "https://unpkg.com/@vonage/vivid@latest/styles/tokens/theme-light.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/core/all.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/fonts/spezia-variable.css";
#buttons-wrapper {
min-width: 50px;
min-height: 50px;
background-color: crimson;
}
</style>
<div id="buttons-wrapper" class="vvd-root">
<vwc-button label="ghost" appearance="ghost"></vwc-button>
<vwc-button label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button label="filled" appearance="filled"></vwc-button>
<vwc-button label="outlined" appearance="outlined"></vwc-button>
</div>
`;
document.body.appendChild(div);
document.body.appendChild(script);

As tested on Google.com:

Thing is – document, body and HTML elements do not exist natively server-side. So…

How can you render HTML on a server?

Great question! Glad you asked.

There are several ways to render HTML server-side. 

Because Vivid tests its components using jsdom, we know it can render our components without a browser.

Hence, if we create a JSDOM environment in our server, we can use our code to render our components.

That’s easy enough because of the almighty NPM!

npm i global-jsdom/register jsdom will add jsdom – a library that mocks the browser’s DOM API in the server runtime, allowing it to create markup as if it were in the browser. global-jsdom/register exposes browser API globally so we can use it in our code. Hence, we can render our components serverside.

Let’s change our template’s code a bit to use that:

import 'global-jsdom/register';
import '@vonage/vivid/button';
export function getHomePageTemplate() {
const template = `
<style>
@import "https://unpkg.com/@vonage/vivid@latest/styles/tokens/theme-light.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/core/all.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/fonts/spezia-variable.css";
#buttons-wrapper {
min-width: 50px;
min-height: 50px;
background-color: crimson;
}
</style>
<div id="buttons-wrapper" class="vvd-root">
<vwc-button label="ghost" appearance="ghost"></vwc-button>
<vwc-button label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button label="filled" appearance="filled"></vwc-button>
<vwc-button label="outlined" appearance="outlined"></vwc-button>
</div>
`;
const div = document.createElement('div');
div.innerHTML = template;
document.body.appendChild(div);
return div.innerHTML;
}

We import global-jsdom/register. Note that we import the @vonage/vivid/button package server-side, so the web component will be rendered as one.

We let jsdom render our template just by adding it to the DOM and returning its innerHTML. It looks like this:

OH NO! No buttons in the view! They are indeed in the DOM. We can also see the input in the light DOM inside every button (it’s there to solve form association). 

The reason we do not see anything is that innerHTML does not get us the content of the shadowDOM.

So what we could try doing is getting the shadowDOM of every component like this:

function appendOwnShadow(element) {
    const shadowTemplate = `${element.shadowRoot.innerHTML}`;
    const tmpElement = document.createElement('div');
    tmpElement.innerHTML = shadowTemplate;
    element.appendChild(tmpElement.children[0]);
}

Array.from(div.querySelectorAll(‘vwc-button’))
    .forEach(button => button.appendChild(appendOwnShadow(button)));

Which give us this UI:

Yay! We can see something but… it’s not exactly the same, right?

And looking at the HTML, we can see the shadowroot is missing:

<div id="buttons-wrapper" class="vvd-root">
<vwc-button label="ghost" appearance="ghost" role="presentation"><input style="display: none;" slot="form-associated-proxy" type="undefined">
<!—-> <button class="control appearance-ghost" value="">
<!—-><slot name="icon" aria-hidden="true"></slot>
<!—-><span class="text" role="presentation">ghost</span>
</button>
<style class="fast-style-class-1">:host{display:inline-block}.control{display:inline-flex;box-sizing:border-box;align-items:center;justify-content:center;border:0 none;border-radius:var(–_button-border-radius);margin:0;background-color:var(–_appearance-color-fill);block-size:var(–_button-block-size);box-shadow:inset 0 0 0 1px var(–_appearance-color-outline);color:var(–_appearance-color-text);gap:var(–_button-icon-gap);text-decoration:none;vertical-align:middle;–focus-stroke-gap-color: transparent}.control.connotation-cta{–_connotation-color-primary: var(–vvd-button-cta-primary, var(–vvd-color-cta-500));–_connotation-color-primary-text: var(–vvd-button-cta-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-cta-primary-increment, var(–vvd-color-cta-600));–_connotation-color-contrast: var(–vvd-button-cta-contrast, var(–vvd-color-cta-800));–_connotation-color-fierce: var(–vvd-button-cta-fierce, var(–vvd-color-cta-700));–_connotation-color-firm: var(–vvd-button-cta-firm, var(–vvd-color-cta-600));–_connotation-color-soft: var(–vvd-button-cta-soft, var(–vvd-color-cta-100));–_connotation-color-faint: var(–vvd-button-cta-faint, var(–vvd-color-cta-50))}.control.connotation-success{–_connotation-color-primary: var(–vvd-button-success-primary, var(–vvd-color-success-500));–_connotation-color-primary-text: var(–vvd-button-success-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-success-primary-increment, var(–vvd-color-success-600));–_connotation-color-contrast: var(–vvd-button-success-contrast, var(–vvd-color-success-800));–_connotation-color-fierce: var(–vvd-button-success-fierce, var(–vvd-color-success-700));–_connotation-color-firm: var(–vvd-button-success-firm, var(–vvd-color-success-600));–_connotation-color-soft: var(–vvd-button-success-soft, var(–vvd-color-success-100));–_connotation-color-faint: var(–vvd-button-success-faint, var(–vvd-color-success-50))}.control.connotation-alert{–_connotation-color-primary: var(–vvd-button-alert-primary, var(–vvd-color-alert-500));–_connotation-color-primary-text: var(–vvd-button-alert-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-alert-primary-increment, var(–vvd-color-alert-600));–_connotation-color-contrast: var(–vvd-button-alert-contrast, var(–vvd-color-alert-800));–_connotation-color-fierce: var(–vvd-button-alert-fierce, var(–vvd-color-alert-700));–_connotation-color-firm: var(–vvd-button-alert-firm, var(–vvd-color-alert-600));–_connotation-color-soft: var(–vvd-button-alert-soft, var(–vvd-color-alert-100));–_connotation-color-faint: var(–vvd-button-alert-faint, var(–vvd-color-alert-50))}.control:not(.connotation-cta,.connotation-success,.connotation-alert){–_connotation-color-primary: var(–vvd-button-accent-primary, var(–vvd-color-canvas-text));–_connotation-color-primary-text: var(–vvd-button-accent-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-accent-primary-increment, var(–vvd-color-neutral-800));–_connotation-color-contrast: var(–vvd-button-accent-contrast, var(–vvd-color-neutral-800));–_connotation-color-fierce: var(–vvd-button-accent-fierce, var(–vvd-color-neutral-700));–_connotation-color-firm: var(–vvd-button-accent-firm, var(–vvd-color-canvas-text));–_connotation-color-soft: var(–vvd-button-accent-soft, var(–vvd-color-neutral-100));–_connotation-color-faint: var(–vvd-button-accent-faint, var(–vvd-color-neutral-50))}.control.appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary);–_appearance-color-outline: transparent}.control.appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–_connotation-color-firm)}.control{–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary-increment);–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-filled{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: var(–vvd-color-neutral-100);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-outlined{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–vvd-color-neutral-300)}.control:where(.disabled,:disabled){–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-fierce);–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.active,:active):where(:not(.disabled,:disabled)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: transparent}.control .text{display:-webkit-box;overflow:hidden;-webkit-box-orient:vertical;-webkit-line-clamp:var(–button-line-clamp, 1);max-inline-size:100%}.control:not(.icon-only){inline-size:100%}.control.appearance-filled{–focus-stroke-gap-color: unset}.control:focus-visible{box-shadow:inset 0 0 0 3px var(–focus-stroke-gap-color, currentColor);outline:2px solid var(–focus-stroke-color, var(–vvd-color-canvas-text));outline-offset:calc(-2px – var(–focus-inset, 0px))}@supports (user-select: none){.control{user-select:none}}.control:not(:disabled){cursor:pointer}.control:disabled{cursor:not-allowed}.control.icon-only{contain:size;padding-inline:0;place-content:center}@supports (aspect-ratio: 1){.control.icon-only{aspect-ratio:1}}@supports not (aspect-ratio: 1){.control.icon-only{inline-size:var(–_button-block-size)}}.control:not(.stacked).size-super-condensed{–_button-block-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:8px}.control:not(.stacked).size-condensed{–_button-block-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-condensed:not(.icon-only){–_button-icon-gap: 8px;padding-inline:12px}.control:not(.stacked).size-expanded{–_button-block-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-extended-bold)}.control:not(.stacked).size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed){–_button-block-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-bold)}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}.control:not(.shape-pill){–_button-border-radius: 8px}.control:not(.shape-pill).size-condensed:not(.stacked),.control:not(.shape-pill).size-super-condensed:not(.stacked){–_button-border-radius: 4px}.control.shape-pill:not(.icon-only,.stacked.size-super-condensed,.stacked.size-condensed,.stacked.normal){–_button-border-radius: 24px}.control.shape-pill.stacked.size-condensed,.control.shape-pill.stacked.size-super-condensed{–_button-border-radius: 16px}.control.shape-pill.stacked.size-normal{–_button-border-radius: 20px}.control.shape-pill.icon-only{–_button-border-radius: 50%}.control.stacked{flex-direction:column;justify-content:center}.control.stacked.size-super-condensed{–stacked-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 20px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:16px}.control.stacked.size-condensed{–stacked-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 24px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-condensed:not(.icon-only){–_button-icon-gap: 6px;padding-inline:12px}.control.stacked.size-expanded{–stacked-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 32px);font:var(–vvd-typography-base-extended-bold)}.control.stacked.size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed){–stacked-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 28px);font:var(–vvd-typography-base-bold)}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}slot[name=icon]{line-height:1}.icon-trailing slot[name=icon]{display:flex;order:1}.control.stacked>slot[name=icon]{font-size:calc(var(–stacked-size) / 2)}.control:not(.stacked)>slot[name=icon]{font-size:calc(var(–_button-block-size) / 2)}:host(:not([icon])) .pending{position:absolute}:host(:not([icon])) .pending+.text{visibility:hidden}
</style><slot name="form-associated-proxy"></slot></vwc-button>
<vwc-button label="ghost-light" appearance="ghost-light" role="presentation"><input style="display: none;" slot="form-associated-proxy" type="undefined">
<!—-> <button class="control appearance-ghost-light" value="">
<!—-><slot name="icon" aria-hidden="true"></slot>
<!—-><span class="text" role="presentation">ghost-light</span>
</button>
<style class="fast-style-class-1">:host{display:inline-block}.control{display:inline-flex;box-sizing:border-box;align-items:center;justify-content:center;border:0 none;border-radius:var(–_button-border-radius);margin:0;background-color:var(–_appearance-color-fill);block-size:var(–_button-block-size);box-shadow:inset 0 0 0 1px var(–_appearance-color-outline);color:var(–_appearance-color-text);gap:var(–_button-icon-gap);text-decoration:none;vertical-align:middle;–focus-stroke-gap-color: transparent}.control.connotation-cta{–_connotation-color-primary: var(–vvd-button-cta-primary, var(–vvd-color-cta-500));–_connotation-color-primary-text: var(–vvd-button-cta-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-cta-primary-increment, var(–vvd-color-cta-600));–_connotation-color-contrast: var(–vvd-button-cta-contrast, var(–vvd-color-cta-800));–_connotation-color-fierce: var(–vvd-button-cta-fierce, var(–vvd-color-cta-700));–_connotation-color-firm: var(–vvd-button-cta-firm, var(–vvd-color-cta-600));–_connotation-color-soft: var(–vvd-button-cta-soft, var(–vvd-color-cta-100));–_connotation-color-faint: var(–vvd-button-cta-faint, var(–vvd-color-cta-50))}.control.connotation-success{–_connotation-color-primary: var(–vvd-button-success-primary, var(–vvd-color-success-500));–_connotation-color-primary-text: var(–vvd-button-success-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-success-primary-increment, var(–vvd-color-success-600));–_connotation-color-contrast: var(–vvd-button-success-contrast, var(–vvd-color-success-800));–_connotation-color-fierce: var(–vvd-button-success-fierce, var(–vvd-color-success-700));–_connotation-color-firm: var(–vvd-button-success-firm, var(–vvd-color-success-600));–_connotation-color-soft: var(–vvd-button-success-soft, var(–vvd-color-success-100));–_connotation-color-faint: var(–vvd-button-success-faint, var(–vvd-color-success-50))}.control.connotation-alert{–_connotation-color-primary: var(–vvd-button-alert-primary, var(–vvd-color-alert-500));–_connotation-color-primary-text: var(–vvd-button-alert-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-alert-primary-increment, var(–vvd-color-alert-600));–_connotation-color-contrast: var(–vvd-button-alert-contrast, var(–vvd-color-alert-800));–_connotation-color-fierce: var(–vvd-button-alert-fierce, var(–vvd-color-alert-700));–_connotation-color-firm: var(–vvd-button-alert-firm, var(–vvd-color-alert-600));–_connotation-color-soft: var(–vvd-button-alert-soft, var(–vvd-color-alert-100));–_connotation-color-faint: var(–vvd-button-alert-faint, var(–vvd-color-alert-50))}.control:not(.connotation-cta,.connotation-success,.connotation-alert){–_connotation-color-primary: var(–vvd-button-accent-primary, var(–vvd-color-canvas-text));–_connotation-color-primary-text: var(–vvd-button-accent-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-accent-primary-increment, var(–vvd-color-neutral-800));–_connotation-color-contrast: var(–vvd-button-accent-contrast, var(–vvd-color-neutral-800));–_connotation-color-fierce: var(–vvd-button-accent-fierce, var(–vvd-color-neutral-700));–_connotation-color-firm: var(–vvd-button-accent-firm, var(–vvd-color-canvas-text));–_connotation-color-soft: var(–vvd-button-accent-soft, var(–vvd-color-neutral-100));–_connotation-color-faint: var(–vvd-button-accent-faint, var(–vvd-color-neutral-50))}.control.appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary);–_appearance-color-outline: transparent}.control.appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–_connotation-color-firm)}.control{–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary-increment);–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-filled{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: var(–vvd-color-neutral-100);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-outlined{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–vvd-color-neutral-300)}.control:where(.disabled,:disabled){–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-fierce);–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.active,:active):where(:not(.disabled,:disabled)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: transparent}.control .text{display:-webkit-box;overflow:hidden;-webkit-box-orient:vertical;-webkit-line-clamp:var(–button-line-clamp, 1);max-inline-size:100%}.control:not(.icon-only){inline-size:100%}.control.appearance-filled{–focus-stroke-gap-color: unset}.control:focus-visible{box-shadow:inset 0 0 0 3px var(–focus-stroke-gap-color, currentColor);outline:2px solid var(–focus-stroke-color, var(–vvd-color-canvas-text));outline-offset:calc(-2px – var(–focus-inset, 0px))}@supports (user-select: none){.control{user-select:none}}.control:not(:disabled){cursor:pointer}.control:disabled{cursor:not-allowed}.control.icon-only{contain:size;padding-inline:0;place-content:center}@supports (aspect-ratio: 1){.control.icon-only{aspect-ratio:1}}@supports not (aspect-ratio: 1){.control.icon-only{inline-size:var(–_button-block-size)}}.control:not(.stacked).size-super-condensed{–_button-block-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:8px}.control:not(.stacked).size-condensed{–_button-block-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-condensed:not(.icon-only){–_button-icon-gap: 8px;padding-inline:12px}.control:not(.stacked).size-expanded{–_button-block-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-extended-bold)}.control:not(.stacked).size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed){–_button-block-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-bold)}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}.control:not(.shape-pill){–_button-border-radius: 8px}.control:not(.shape-pill).size-condensed:not(.stacked),.control:not(.shape-pill).size-super-condensed:not(.stacked){–_button-border-radius: 4px}.control.shape-pill:not(.icon-only,.stacked.size-super-condensed,.stacked.size-condensed,.stacked.normal){–_button-border-radius: 24px}.control.shape-pill.stacked.size-condensed,.control.shape-pill.stacked.size-super-condensed{–_button-border-radius: 16px}.control.shape-pill.stacked.size-normal{–_button-border-radius: 20px}.control.shape-pill.icon-only{–_button-border-radius: 50%}.control.stacked{flex-direction:column;justify-content:center}.control.stacked.size-super-condensed{–stacked-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 20px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:16px}.control.stacked.size-condensed{–stacked-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 24px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-condensed:not(.icon-only){–_button-icon-gap: 6px;padding-inline:12px}.control.stacked.size-expanded{–stacked-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 32px);font:var(–vvd-typography-base-extended-bold)}.control.stacked.size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed){–stacked-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 28px);font:var(–vvd-typography-base-bold)}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}slot[name=icon]{line-height:1}.icon-trailing slot[name=icon]{display:flex;order:1}.control.stacked>slot[name=icon]{font-size:calc(var(–stacked-size) / 2)}.control:not(.stacked)>slot[name=icon]{font-size:calc(var(–_button-block-size) / 2)}:host(:not([icon])) .pending{position:absolute}:host(:not([icon])) .pending+.text{visibility:hidden}
</style><slot name="form-associated-proxy"></slot></vwc-button>
<vwc-button label="filled" appearance="filled" role="presentation"><input style="display: none;" slot="form-associated-proxy" type="undefined">
<!—-> <button class="control appearance-filled" value="">
<!—-><slot name="icon" aria-hidden="true"></slot>
<!—-><span class="text" role="presentation">filled</span>
</button>
<style class="fast-style-class-1">:host{display:inline-block}.control{display:inline-flex;box-sizing:border-box;align-items:center;justify-content:center;border:0 none;border-radius:var(–_button-border-radius);margin:0;background-color:var(–_appearance-color-fill);block-size:var(–_button-block-size);box-shadow:inset 0 0 0 1px var(–_appearance-color-outline);color:var(–_appearance-color-text);gap:var(–_button-icon-gap);text-decoration:none;vertical-align:middle;–focus-stroke-gap-color: transparent}.control.connotation-cta{–_connotation-color-primary: var(–vvd-button-cta-primary, var(–vvd-color-cta-500));–_connotation-color-primary-text: var(–vvd-button-cta-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-cta-primary-increment, var(–vvd-color-cta-600));–_connotation-color-contrast: var(–vvd-button-cta-contrast, var(–vvd-color-cta-800));–_connotation-color-fierce: var(–vvd-button-cta-fierce, var(–vvd-color-cta-700));–_connotation-color-firm: var(–vvd-button-cta-firm, var(–vvd-color-cta-600));–_connotation-color-soft: var(–vvd-button-cta-soft, var(–vvd-color-cta-100));–_connotation-color-faint: var(–vvd-button-cta-faint, var(–vvd-color-cta-50))}.control.connotation-success{–_connotation-color-primary: var(–vvd-button-success-primary, var(–vvd-color-success-500));–_connotation-color-primary-text: var(–vvd-button-success-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-success-primary-increment, var(–vvd-color-success-600));–_connotation-color-contrast: var(–vvd-button-success-contrast, var(–vvd-color-success-800));–_connotation-color-fierce: var(–vvd-button-success-fierce, var(–vvd-color-success-700));–_connotation-color-firm: var(–vvd-button-success-firm, var(–vvd-color-success-600));–_connotation-color-soft: var(–vvd-button-success-soft, var(–vvd-color-success-100));–_connotation-color-faint: var(–vvd-button-success-faint, var(–vvd-color-success-50))}.control.connotation-alert{–_connotation-color-primary: var(–vvd-button-alert-primary, var(–vvd-color-alert-500));–_connotation-color-primary-text: var(–vvd-button-alert-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-alert-primary-increment, var(–vvd-color-alert-600));–_connotation-color-contrast: var(–vvd-button-alert-contrast, var(–vvd-color-alert-800));–_connotation-color-fierce: var(–vvd-button-alert-fierce, var(–vvd-color-alert-700));–_connotation-color-firm: var(–vvd-button-alert-firm, var(–vvd-color-alert-600));–_connotation-color-soft: var(–vvd-button-alert-soft, var(–vvd-color-alert-100));–_connotation-color-faint: var(–vvd-button-alert-faint, var(–vvd-color-alert-50))}.control:not(.connotation-cta,.connotation-success,.connotation-alert){–_connotation-color-primary: var(–vvd-button-accent-primary, var(–vvd-color-canvas-text));–_connotation-color-primary-text: var(–vvd-button-accent-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-accent-primary-increment, var(–vvd-color-neutral-800));–_connotation-color-contrast: var(–vvd-button-accent-contrast, var(–vvd-color-neutral-800));–_connotation-color-fierce: var(–vvd-button-accent-fierce, var(–vvd-color-neutral-700));–_connotation-color-firm: var(–vvd-button-accent-firm, var(–vvd-color-canvas-text));–_connotation-color-soft: var(–vvd-button-accent-soft, var(–vvd-color-neutral-100));–_connotation-color-faint: var(–vvd-button-accent-faint, var(–vvd-color-neutral-50))}.control.appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary);–_appearance-color-outline: transparent}.control.appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–_connotation-color-firm)}.control{–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary-increment);–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-filled{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: var(–vvd-color-neutral-100);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-outlined{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–vvd-color-neutral-300)}.control:where(.disabled,:disabled){–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-fierce);–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.active,:active):where(:not(.disabled,:disabled)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: transparent}.control .text{display:-webkit-box;overflow:hidden;-webkit-box-orient:vertical;-webkit-line-clamp:var(–button-line-clamp, 1);max-inline-size:100%}.control:not(.icon-only){inline-size:100%}.control.appearance-filled{–focus-stroke-gap-color: unset}.control:focus-visible{box-shadow:inset 0 0 0 3px var(–focus-stroke-gap-color, currentColor);outline:2px solid var(–focus-stroke-color, var(–vvd-color-canvas-text));outline-offset:calc(-2px – var(–focus-inset, 0px))}@supports (user-select: none){.control{user-select:none}}.control:not(:disabled){cursor:pointer}.control:disabled{cursor:not-allowed}.control.icon-only{contain:size;padding-inline:0;place-content:center}@supports (aspect-ratio: 1){.control.icon-only{aspect-ratio:1}}@supports not (aspect-ratio: 1){.control.icon-only{inline-size:var(–_button-block-size)}}.control:not(.stacked).size-super-condensed{–_button-block-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:8px}.control:not(.stacked).size-condensed{–_button-block-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-condensed:not(.icon-only){–_button-icon-gap: 8px;padding-inline:12px}.control:not(.stacked).size-expanded{–_button-block-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-extended-bold)}.control:not(.stacked).size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed){–_button-block-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-bold)}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}.control:not(.shape-pill){–_button-border-radius: 8px}.control:not(.shape-pill).size-condensed:not(.stacked),.control:not(.shape-pill).size-super-condensed:not(.stacked){–_button-border-radius: 4px}.control.shape-pill:not(.icon-only,.stacked.size-super-condensed,.stacked.size-condensed,.stacked.normal){–_button-border-radius: 24px}.control.shape-pill.stacked.size-condensed,.control.shape-pill.stacked.size-super-condensed{–_button-border-radius: 16px}.control.shape-pill.stacked.size-normal{–_button-border-radius: 20px}.control.shape-pill.icon-only{–_button-border-radius: 50%}.control.stacked{flex-direction:column;justify-content:center}.control.stacked.size-super-condensed{–stacked-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 20px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:16px}.control.stacked.size-condensed{–stacked-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 24px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-condensed:not(.icon-only){–_button-icon-gap: 6px;padding-inline:12px}.control.stacked.size-expanded{–stacked-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 32px);font:var(–vvd-typography-base-extended-bold)}.control.stacked.size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed){–stacked-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 28px);font:var(–vvd-typography-base-bold)}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}slot[name=icon]{line-height:1}.icon-trailing slot[name=icon]{display:flex;order:1}.control.stacked>slot[name=icon]{font-size:calc(var(–stacked-size) / 2)}.control:not(.stacked)>slot[name=icon]{font-size:calc(var(–_button-block-size) / 2)}:host(:not([icon])) .pending{position:absolute}:host(:not([icon])) .pending+.text{visibility:hidden}
</style><slot name="form-associated-proxy"></slot></vwc-button>
<vwc-button label="outlined" appearance="outlined" role="presentation"><input style="display: none;" slot="form-associated-proxy" type="undefined">
<!—-> <button class="control appearance-outlined" value="">
<!—-><slot name="icon" aria-hidden="true"></slot>
<!—-><span class="text" role="presentation">outlined</span>
</button>
<style class="fast-style-class-1">:host{display:inline-block}.control{display:inline-flex;box-sizing:border-box;align-items:center;justify-content:center;border:0 none;border-radius:var(–_button-border-radius);margin:0;background-color:var(–_appearance-color-fill);block-size:var(–_button-block-size);box-shadow:inset 0 0 0 1px var(–_appearance-color-outline);color:var(–_appearance-color-text);gap:var(–_button-icon-gap);text-decoration:none;vertical-align:middle;–focus-stroke-gap-color: transparent}.control.connotation-cta{–_connotation-color-primary: var(–vvd-button-cta-primary, var(–vvd-color-cta-500));–_connotation-color-primary-text: var(–vvd-button-cta-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-cta-primary-increment, var(–vvd-color-cta-600));–_connotation-color-contrast: var(–vvd-button-cta-contrast, var(–vvd-color-cta-800));–_connotation-color-fierce: var(–vvd-button-cta-fierce, var(–vvd-color-cta-700));–_connotation-color-firm: var(–vvd-button-cta-firm, var(–vvd-color-cta-600));–_connotation-color-soft: var(–vvd-button-cta-soft, var(–vvd-color-cta-100));–_connotation-color-faint: var(–vvd-button-cta-faint, var(–vvd-color-cta-50))}.control.connotation-success{–_connotation-color-primary: var(–vvd-button-success-primary, var(–vvd-color-success-500));–_connotation-color-primary-text: var(–vvd-button-success-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-success-primary-increment, var(–vvd-color-success-600));–_connotation-color-contrast: var(–vvd-button-success-contrast, var(–vvd-color-success-800));–_connotation-color-fierce: var(–vvd-button-success-fierce, var(–vvd-color-success-700));–_connotation-color-firm: var(–vvd-button-success-firm, var(–vvd-color-success-600));–_connotation-color-soft: var(–vvd-button-success-soft, var(–vvd-color-success-100));–_connotation-color-faint: var(–vvd-button-success-faint, var(–vvd-color-success-50))}.control.connotation-alert{–_connotation-color-primary: var(–vvd-button-alert-primary, var(–vvd-color-alert-500));–_connotation-color-primary-text: var(–vvd-button-alert-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-alert-primary-increment, var(–vvd-color-alert-600));–_connotation-color-contrast: var(–vvd-button-alert-contrast, var(–vvd-color-alert-800));–_connotation-color-fierce: var(–vvd-button-alert-fierce, var(–vvd-color-alert-700));–_connotation-color-firm: var(–vvd-button-alert-firm, var(–vvd-color-alert-600));–_connotation-color-soft: var(–vvd-button-alert-soft, var(–vvd-color-alert-100));–_connotation-color-faint: var(–vvd-button-alert-faint, var(–vvd-color-alert-50))}.control:not(.connotation-cta,.connotation-success,.connotation-alert){–_connotation-color-primary: var(–vvd-button-accent-primary, var(–vvd-color-canvas-text));–_connotation-color-primary-text: var(–vvd-button-accent-primary-text, var(–vvd-color-canvas));–_connotation-color-primary-increment: var(–vvd-button-accent-primary-increment, var(–vvd-color-neutral-800));–_connotation-color-contrast: var(–vvd-button-accent-contrast, var(–vvd-color-neutral-800));–_connotation-color-fierce: var(–vvd-button-accent-fierce, var(–vvd-color-neutral-700));–_connotation-color-firm: var(–vvd-button-accent-firm, var(–vvd-color-canvas-text));–_connotation-color-soft: var(–vvd-button-accent-soft, var(–vvd-color-neutral-100));–_connotation-color-faint: var(–vvd-button-accent-faint, var(–vvd-color-neutral-50))}.control.appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary);–_appearance-color-outline: transparent}.control.appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–_connotation-color-firm)}.control{–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-primary-increment);–_appearance-color-outline: transparent}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.hover,:hover):where(:not(.disabled,:disabled,.readonly)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-faint);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-filled{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: var(–vvd-color-neutral-100);–_appearance-color-outline: transparent}.control:where(.disabled,:disabled).appearance-outlined{–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: var(–vvd-color-neutral-300)}.control:where(.disabled,:disabled){–_appearance-color-text: var(–vvd-color-neutral-300);–_appearance-color-fill: transparent;–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-filled{–_appearance-color-text: var(–_connotation-color-primary-text);–_appearance-color-fill: var(–_connotation-color-fierce);–_appearance-color-outline: transparent}.control:where(.active,:active):where(:not(.disabled,:disabled)).appearance-outlined{–_appearance-color-text: var(–_connotation-color-firm);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: var(–_connotation-color-firm)}.control:where(.active,:active):where(:not(.disabled,:disabled)){–_appearance-color-text: var(–_connotation-color-primary);–_appearance-color-fill: var(–_connotation-color-soft);–_appearance-color-outline: transparent}.control .text{display:-webkit-box;overflow:hidden;-webkit-box-orient:vertical;-webkit-line-clamp:var(–button-line-clamp, 1);max-inline-size:100%}.control:not(.icon-only){inline-size:100%}.control.appearance-filled{–focus-stroke-gap-color: unset}.control:focus-visible{box-shadow:inset 0 0 0 3px var(–focus-stroke-gap-color, currentColor);outline:2px solid var(–focus-stroke-color, var(–vvd-color-canvas-text));outline-offset:calc(-2px – var(–focus-inset, 0px))}@supports (user-select: none){.control{user-select:none}}.control:not(:disabled){cursor:pointer}.control:disabled{cursor:not-allowed}.control.icon-only{contain:size;padding-inline:0;place-content:center}@supports (aspect-ratio: 1){.control.icon-only{aspect-ratio:1}}@supports not (aspect-ratio: 1){.control.icon-only{inline-size:var(–_button-block-size)}}.control:not(.stacked).size-super-condensed{–_button-block-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:8px}.control:not(.stacked).size-condensed{–_button-block-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-condensed-bold)}.control:not(.stacked).size-condensed:not(.icon-only){–_button-icon-gap: 8px;padding-inline:12px}.control:not(.stacked).size-expanded{–_button-block-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-extended-bold)}.control:not(.stacked).size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed){–_button-block-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));font:var(–vvd-typography-base-bold)}.control:not(.stacked):not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}.control:not(.shape-pill){–_button-border-radius: 8px}.control:not(.shape-pill).size-condensed:not(.stacked),.control:not(.shape-pill).size-super-condensed:not(.stacked){–_button-border-radius: 4px}.control.shape-pill:not(.icon-only,.stacked.size-super-condensed,.stacked.size-condensed,.stacked.normal){–_button-border-radius: 24px}.control.shape-pill.stacked.size-condensed,.control.shape-pill.stacked.size-super-condensed{–_button-border-radius: 16px}.control.shape-pill.stacked.size-normal{–_button-border-radius: 20px}.control.shape-pill.icon-only{–_button-border-radius: 50%}.control.stacked{flex-direction:column;justify-content:center}.control.stacked.size-super-condensed{–stacked-size:calc(1px*(24 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 20px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-super-condensed:not(.icon-only){–_button-icon-gap: 4px;padding-inline:16px}.control.stacked.size-condensed{–stacked-size:calc(1px*(32 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 24px);font:var(–vvd-typography-base-condensed-bold)}.control.stacked.size-condensed:not(.icon-only){–_button-icon-gap: 6px;padding-inline:12px}.control.stacked.size-expanded{–stacked-size:calc(1px*(48 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 32px);font:var(–vvd-typography-base-extended-bold)}.control.stacked.size-expanded:not(.icon-only){–_button-icon-gap: 10px;padding-inline:20px}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed){–stacked-size:calc(1px*(40 + 4*clamp(-1, var(–vvd-size-density, 0), 2)));–_button-block-size: calc(var(–stacked-size) + 28px);font:var(–vvd-typography-base-bold)}.control.stacked:not(.size-condensed,.size-expanded,.size-super-condensed):not(.icon-only){–_button-icon-gap: 8px;padding-inline:16px}slot[name=icon]{line-height:1}.icon-trailing slot[name=icon]{display:flex;order:1}.control.stacked>slot[name=icon]{font-size:calc(var(–stacked-size) / 2)}.control:not(.stacked)>slot[name=icon]{font-size:calc(var(–_button-block-size) / 2)}:host(:not([icon])) .pending{position:absolute}:host(:not([icon])) .pending+.text{visibility:hidden}
</style><slot name="form-associated-proxy"></slot></vwc-button>
</div>
view raw innerHTML.html hosted with ❤ by GitHub

This definitely might affect the component’s styling, since we are losing the encapsulation.

How to Explicitly Render Shadow DOM without JavaScript?

For this purpose, the HTML spec now defines a shadowrootmode attribute for the template tag. When the browser encounters <template shadowrootmode=”open”> it knows to take everything inside that template and render it inside a shadow DOM.

Using this knowledge, we can change our code as follows:

function appendOwnShadow(element) {
    const shadowTemplate = `<template shadowrootmode="open">   ${element.shadowRoot.innerHTML}</template>`;
    const tmpElement = document.createElement('div');
    tmpElement.innerHTML = shadowTemplate;
    element.appendChild(tmpElement.children[0]);
}

Array.from(div.querySelectorAll(‘vwc-button’))
    .forEach(button => button.appendChild(appendOwnShadow(button)));

It now renders like this:

Which is how we expected it to render! Hooray!

If you look at the DOM now, it looks like this:

How cool is that? We rendered our web components server-side and prevented the layout shift in our app!

Let’s try to spice up our application.

Handling Complex Components

The button we used was quite basic. Let’s try to use a button with an icon inside:

<vwc-button icon="facebook-color" label="ghost" appearance="ghost"></vwc-button>
<vwc-button icon="linkedin-color" label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button icon="twitter-color" label="filled" appearance="filled"></vwc-button>
<vwc-button icon="instagram-color" label="outlined" appearance="outlined"></vwc-button>

And it looks like this in the browser:

Something changed, but we can’t see any icons…

The HTML inside the button looks like this:

We can see vwc-icon right there in the middle. We can see two problems here:

  1. The icon has no attributes – so it doesn’t really know how to render itself. 
  2. The icon has no content – mainly, no shadowroot

Solving the Icon not Getting Attributes

Let’s solve the simpler issue. The icon gets its attributes from the button component. The template is rendered asynchronously. That means that after we add the div to the DOM, the actual update happens after another iteration of the event loop. So, we need to await the completion of the rendering process.

For this, we can set the template function to be async and await one event loop cycle:

import 'global-jsdom/register';
import '@vonage/vivid/button';
function appendOwnShadow(element) {
const shadowTemplate = `<template shadowrootmode="open">${element.shadowRoot.innerHTML}</template>`;
const tmpElement = document.createElement('div');
tmpElement.innerHTML = shadowTemplate;
element.appendChild(tmpElement.children[0]);
}
export async function getHomePageTemplate() {
const template = `
<style>
@import "https://unpkg.com/@vonage/vivid@latest/styles/tokens/theme-light.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/core/all.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/fonts/spezia-variable.css";
#buttons-wrapper {
min-width: 50px;
min-height: 50px;
background-color: crimson;
}
</style>
<div id="buttons-wrapper" class="vvd-root">
<vwc-button icon="facebook-color" label="ghost" appearance="ghost"></vwc-button>
<vwc-button icon="linkedin-color" label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button icon="twitter-color" label="filled" appearance="filled"></vwc-button>
<vwc-button icon="instagram-color" label="outlined" appearance="outlined"></vwc-button>
</div>
`;
const div = document.createElement('div');
div.innerHTML = template;
document.body.appendChild(div);
await new Promise(res => setTimeout(res));
Array.from(div.querySelectorAll('vwc-button')).forEach(appendOwnShadow);
return div.innerHTML;
}

Notice we’ve added the magic await new Promise(res => setTimeout(res)); in line 28. Because we changed the template method to be async, we also need to change our server function to be async and await the template:

import http from 'http';
import fs from 'fs';
import path from 'path';
import * as routes from './routes/index.mjs';
const CONTENT_TYPES = {
'.js': 'text/javascript',
'.mjs': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/png',
'.gif': 'image/png',
'.ico': 'image/png',
};
const server = http.createServer(async (req, res) => {
function returnFileContent(filePath, contentType) {
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end(`Server Error: ${err.code}`);
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}
let filePath = '.' + req.url;
if (filePath === './') {
filePath = 'HomePage';
}
const extname = path.extname(filePath);
let contentType = CONTENT_TYPES[extname] ?? 'text/html';
if (contentType === 'text/html') {
res.writeHead(200, { 'Content-Type': contentType });
res.end(await routes[filePath].template, 'utf-8');
} else {
returnFileContent(filePath, contentType);
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}/`);
});
view raw index.mjs hosted with ❤ by GitHub

Now when we glimpse at our HTML we see the icon gets the attributes:

Loading Internal Components

The second issue – because of which we do not see the icons – arise from the fact we do not get the shadowroot’s HTML of the internal components.

One way to fix this would be to find all the web components recursively and render them as well.

To find the components, we can traverse the DOM tree like this:

function getAllNestedShadowRootsParents(element) {
const nestedShadowRoots = [];
function traverseShadowRoot(node) {
if (node.shadowRoot) {
nestedShadowRoots.push(node);
node.shadowRoot.querySelectorAll('*').forEach(child => {
traverseShadowRoot(child);
});
} else {
Array.from(node.querySelectorAll('*')).forEach(child => traverseShadowRoot(child));
}
}
traverseShadowRoot(element);
return Array.from(new Set(nestedShadowRoots));
}

This function gets an element (supposedly our wrapping div) and gets all the web components with shadowDOM.

Now, all that’s left to do is parse each one of them in our template file:

Let’s do that:

import 'global-jsdom/register';
import '@vonage/vivid/button';
function getAllNestedShadowRootsParents(element) {
const nestedShadowRoots = [];
function traverseShadowRoot(node) {
if (node.shadowRoot) {
nestedShadowRoots.push(node);
node.shadowRoot.querySelectorAll('*').forEach(child => {
traverseShadowRoot(child);
});
} else {
Array.from(node.querySelectorAll('*')).forEach(child => traverseShadowRoot(child));
}
}
traverseShadowRoot(element);
return Array.from(new Set(nestedShadowRoots));
}
function appendOwnShadow(element) {
const shadowTemplate = `<template shadowrootmode="open">${element.shadowRoot.innerHTML}</template>`;
const tmpElement = document.createElement('div');
tmpElement.innerHTML = shadowTemplate;
element.appendChild(tmpElement.children[0]);
}
export async function getHomePageTemplate() {
const template = `
<style>
@import "https://unpkg.com/@vonage/vivid@latest/styles/tokens/theme-light.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/core/all.css";
@import "https://unpkg.com/@vonage/vivid@latest/styles/fonts/spezia-variable.css";
#buttons-wrapper {
min-width: 50px;
min-height: 50px;
background-color: crimson;
}
</style>
<div id="buttons-wrapper" class="vvd-root">
<vwc-button icon="facebook-color" label="ghost" appearance="ghost"></vwc-button>
<vwc-button icon="linkedin-color" label="ghost-light" appearance="ghost-light"></vwc-button>
<vwc-button icon="twitter-color" label="filled" appearance="filled"></vwc-button>
<vwc-button icon="instagram-color" label="outlined" appearance="outlined"></vwc-button>
</div>
`;
const div = document.createElement('div');
div.innerHTML = template;
document.body.appendChild(div);
await new Promise(res => setTimeout(res));
getAllNestedShadowRootsParents(div).reverse().forEach(appendOwnShadow);
return div.innerHTML;
}

Notice the change in line 54 – we’re going over all the elements with shadow DOM in reverse order and append a shadowroot template with their innerHTML for each of them.

The result is astounding:

If you followed so far – good job! You got the basics of SSR.

Can We Serve More?

Our simple SSR server can be optimized further. For instance, some things, such as the CSS and the icons’ SVGs, are still dependent on servers far away. We can add more logic to our SSR server to fetch them and inline them in the returned HTML.

More ideas can be taken from other SSR systems. For instance, react server components have a dedicated API to fetch and send requests to the server, which in turn requests the data and renders the needed view.

Qwik sets up service workers to fetch the JS in the background.

All of the SSR frameworks have many optimizations done for you, but they do not always fit your needs, so knowing how they work is a good start to extending them.

Summary

That was quite a ride, wasn’t it?

Building an SSR mechanism is quite simple in essence, but it can always be improved, tweaked, and optimized. You might possibly find yourself maintaining a big codebase just to handle SSR.

You can choose to use nextjs (react), nuxtjs (vue) or some other SSR library. If you are using web components, SSR libraries like litssr or fastssr can take the heavy lifting from you. 

One big caveat with these SSR frameworks or libraries is that they work only for the framework or library they were meant to work with. 

Our use case was to build an SSR mechanism to work alongside nuxt. So you can call my code an SSR plugin. I hope this article gave you a hint on how to get started building a plugin like that if the need ever arises. 

The commonality to all SSRs is that there is some rendering function. This function is used on your template and returns an HTML string that is sent to the client (well, except React Server Components that actually send a JSON – but that’s beyond the scope of this article). 

Some of this HTML is hydrated later on after the JavaScript loads asynchronously, without blocking the page. In this article, we learned how to do it with web components and shadow DOM.

The fact we do not block the page with JS load helps us serve content faster, avoid heavy layout shifts, and possibly enhance our SEO ranking.

Thanks a lot to Evyatar Alush, the Author of Vest, for the kind and thorough review of this article.

Sign up to my newsletter to enjoy more content:

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments