Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thescientist13/get-inner-html

Example for demonstrating and understanding getInnerHTML
https://github.com/thescientist13/get-inner-html

Last synced: about 1 month ago
JSON representation

Example for demonstrating and understanding getInnerHTML

Awesome Lists containing this project

README

        

[![Netlify Status](https://api.netlify.com/api/v1/badges/7b65545b-431a-49fb-aa46-8ff5bbcb58b4/deploy-status)](https://app.netlify.com/sites/unique-bombolone-b3ee26/deploys)

# get-inner-html

Example for demonstrating and understanding `getInnerHTML`, used in Declarative Shadow DOM.

## Setup
1. Clone the repo
1. Run `npm ci`
1. Run `npm start`

`localhost:8080` should open in your browser.

> _Make sure to test with a browser that supports Declarative Shadow DOM, like Chrome or Edge._

## Overview

To quote from the [web.dev article](https://web.dev/declarative-shadow-dom/#serialization) on serializing Declarative Shadow DOM

> Passing the `includeShadowRoots:true` option serializes the entire subtree of an element, **including its shadow roots**. The included shadow roots are serialized using the `` syntax.

In the context of server side rendering (SSR), I'm not sure if it is saying that if it was set to _`false`_
a) It would not literally does not render shadow roots at all
b) just doesn't render / return them in ``

So for example, given these custom elements + shadowRoot

```js
// navigation.js
const template = document.createElement('template');

template.innerHTML = `


`;

class Navigation extends HTMLElement {
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
}

customElements.define('wcc-navigation', Navigation);

// header.js
import './navigation.js';

const template = document.createElement('template');

template.innerHTML = `

Welcome to my website




`;

class Header extends HTMLElement {
connectedCallback() {
if (!this.shadowRoot) {
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
}

export { Header };

customElements.define('wcc-header', Header);
```

And invoked as so
```js
import { Header } from './header.js';

const element = new Header();
const html = element.getInnerHTML({ includeShadowRoots: false });

console.debug({ html });
```

----

Would the output be

A) No Shadow Roots
```html

Welcome to my website


```

or

B) Just no `` (e.g. only light DOM)
```html

Welcome to my website



```

----

It seems per testing in Chrome, `true` outputs just the first pass, and `false` returns nothing?
```
getInnerHTML (true)

Welcome to my website



(index):15 getInnerHTML (false)
```