0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/renderers/renderer-lit
Nate Moore 34e03cf912
do not format vite (#1710)
* Revert "[ci] yarn format"

This reverts commit 93489946cc.

* chore: ignore vendor
2021-10-29 14:45:32 -05:00
..
.gitignore Adds support for a Lit renderer (#665) 2021-07-13 08:27:40 -04:00
CHANGELOG.md Version Packages (#1451) 2021-09-28 15:34:16 -04:00
client-shim.js [ci] yarn format 2021-07-20 01:24:59 +00:00
hydration-support.js do not format vite (#1710) 2021-10-29 14:45:32 -05:00
index.js do not format vite (#1710) 2021-10-29 14:45:32 -05:00
package.json 🚀 Astro Next (0.21.0) (#1406) 2021-10-29 15:30:22 -04:00
readme.md Docs: Add READMEs for renderers (#1351) 2021-09-14 16:26:23 -07:00
server-shim.js Update lit version to v2 (#1445) 2021-09-28 14:59:28 -04:00
server.js 🚀 Astro Next (0.21.0) (#1406) 2021-10-29 15:30:22 -04:00

Astro Lit Renderer

This is a plugin for Astro apps that enables server-side rendering of custom elements build with Lit.

Server-side rendering uses Declarative Shadow DOM, a new web technology that allows custom elements to be rendered to HTML with 0 JavaScript.

Installation

Install @astrojs/renderer-lit and then add it to your astro.config.mjs in the renderers property:

npm install @astrojs/renderer-lit

astro.config.mjs

export default {
  // ...

  renderers: [
    // ...
    '@astrojs/renderer-lit'
  ]
}

Usage

If you're familiar with Astro then you already know that you can import components into your templates and use them. What's different about custom elements is you can use the tag name directly.

Astro needs to know which tag is associated with which component script. We expose this through exporting a tagName variable from the component script. It looks like this:

src/components/my-element.js

import { LitElement, html } from 'lit';

export const tagName = 'my-element';

class MyElement extends LitElement {
  render() {
    return html` <p>Hello world! From my-element</p> `;
  }
}

customElements.define(tagName, MyElement);

Note that exporting the tagName is required if you want to use the tag name in your templates. Otherwise you can export and use the constructor, like with non custom element frameworks.

In your Astro template import this component as a side-effect and use the element.

src/pages/index.astro

---
import '../components/my-element.js';
---

<my-element></my-element>

Note that Lit requires browser globals such as HTMLElement and customElements to be present. For this reason the Lit renderer shims the server with these globals so Lit can run. You might run into libraries that work incorrectly because of this.

Polyfills & Hydration

The renderer automatically handles adding appropriate polyfills for support in browsers that don't have Declarative Shadow DOM. The polyfill is about 1.5kB. If the browser does support Declarative Shadow DOM then less than 250 bytes are loaded (to feature detect support).

Hydration is also handled automatically. You can use the same hydration directives such as client:load, client:idle and client:visible as you can with other libraries that Astro supports.

---
import '../components/my-element.js';
---

<my-element client:visible />

The above will only load the element's JavaScript when the user has scrolled it into view. Since it is server rendered they will not see any jank; it will load and hydrate transparently.

More Documentation

Astro Renderer Documentation