Skip to content

Commit 2500818

Browse files
Update ssr to use use ssr-client and new eleventy plugin (#1114)
* Update ssr to use use ssr-client and new eleventy plugin. Also update docs with this guidance. * Fix lit-html version in package-lock * Add ssr-client to lit.js rollup chunk to fix hydration on prod. * Update client usage to show examples using ssr-client * Revert the stackblitz link
1 parent 4831c37 commit 2500818

File tree

6 files changed

+41
-37
lines changed

6 files changed

+41
-37
lines changed

package-lock.json

+24-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lit-dev-content/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@
157157
"@11ty/eleventy": "^2.0.1",
158158
"@11ty/eleventy-navigation": "^0.3.2",
159159
"@11ty/eleventy-plugin-rss": "^1.1.2",
160-
"@lit-labs/eleventy-plugin-lit": "^1.0.0",
160+
"@lit-labs/eleventy-plugin-lit": "^1.0.1",
161161
"@lit-labs/ssr": "^3.1.2",
162+
"@lit-labs/ssr-client": "^1.1.1",
162163
"@rollup/plugin-node-resolve": "^15.0.1",
163164
"clean-css": "^5.1.0",
164165
"codemirror": "^5.58.2",
@@ -194,7 +195,7 @@
194195
"@types/react": "^18.0.17",
195196
"@webcomponents/template-shadowroot": "^0.2.1",
196197
"algoliasearch": "^4.14.2",
197-
"lit": "^2.1.0",
198+
"lit": "^2.7.4",
198199
"postdoc-lib": "^1.0.3",
199200
"tarts": "^1.0.0",
200201
"timeago.js": "^4.0.2",

packages/lit-dev-content/rollup.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export default [
7171
relative.startsWith('lit/') ||
7272
relative.startsWith('lit-html/') ||
7373
relative.startsWith('lit-element/') ||
74-
relative.startsWith('@lit/reactive-element/')
74+
relative.startsWith('@lit/reactive-element/') ||
75+
relative.startsWith('@lit-labs/ssr-client/')
7576
) {
7677
return 'lit';
7778
}

packages/lit-dev-content/site/blog/2022-02-07-eleventy.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ implementations haven't loaded.
155155
its JavaScript implementation, becoming responsive and interactive.
156156

157157
Lit comes with automatic support for hydration via the special
158-
`lit/experimental-hydrate-support.js` module. As long as this module has been
158+
`@lit-labs/ssr-client/lit-element-hydrate-support.js` module. As long as this module has been
159159
loaded, Lit components will detect when they have been statically rendered, and
160160
will adopt their existing shadow root.
161161

packages/lit-dev-content/site/docs/v2/ssr/client-usage.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ Lit SSR generates static HTML for the browser to parse and paint without any Jav
1313
How to re-apply that reactivity client-side depends on whether you are rendering standalone Lit templates or utilizing Lit components.
1414

1515
## Standalone Lit Templates
16-
"Hydration" for Lit templates is the process of having Lit re-associate the expressions of Lit templates with the nodes they should update in the DOM as well as adding event listeners. In order to hydrate Lit templates, the `hydrate()` method from the `experimental-hydrate` module is provided in the `lit` package. Before you update a server-rendered container using `render()`, you must first call `hydrate()` on that container using the same template and data that was used to render on the server:
16+
"Hydration" for Lit templates is the process of having Lit re-associate the expressions of Lit templates with the nodes they should update in the DOM as well as adding event listeners. In order to hydrate Lit templates, the `hydrate()` method is provided in the `@lit-labs/ssr-client` package. Before you update a server-rendered container using `render()`, you must first call `hydrate()` on that container using the same template and data that was used to render on the server:
1717

1818
```js
1919
import {render} from 'lit';
20-
import {hydrate} from 'lit/experimental-hydrate.js';
20+
import {hydrate} from '@lit-labs/ssr-client';
2121
import {myTemplate} from './my-template.js';
2222
// Initial hydration required before render:
2323
// (must be same data used to render on the server)
@@ -31,33 +31,33 @@ const update = (data) => render(myTemplate(data), document.body);
3131
## Lit components
3232
To re-apply reactivity to Lit components, custom element definitions need to be loaded for them to upgrade, enabling their lifecycle callbacks, and the templates in the components' shadow roots needs to be hydrated.
3333

34-
Upgrading can be achieved simply by loading the component module that registers the custom element. This can be done by loading a bundle of all the component definitions for a page, or may be done based on more sophisticated heuristics where only a subset of definitions are loaded as needed. To ensure templates in `LitElement` shadow roots are hydrated, load the `lit/experimental-hydrate-support.js` module which installs support for `LitElement` to automatically hydrate itself when it detects it was server-rendered with declarative shadow DOM. This module must be loaded before the `lit` module is loaded (including any component modules that import `lit`) to ensure hydration support is properly installed.
34+
Upgrading can be achieved simply by loading the component module that registers the custom element. This can be done by loading a bundle of all the component definitions for a page, or may be done based on more sophisticated heuristics where only a subset of definitions are loaded as needed. To ensure templates in `LitElement` shadow roots are hydrated, load the `@lit-labs/ssr-client/lit-element-hydrate-support.js` module which installs support for `LitElement` to automatically hydrate itself when it detects it was server-rendered with declarative shadow DOM. This module must be loaded before the `lit` module is loaded (including any component modules that import `lit`) to ensure hydration support is properly installed.
3535

3636
When Lit components are server rendered, their shadow root contents are emitted inside a `<template shadowroot>`, also known as a [Declarative Shadow Root](https://web.dev/declarative-shadow-dom/). Declarative shadow roots automatically attach their contents to a shadow root on the template's parent element when HTML is parsed without the need for JavaScript.
3737

3838
Until all browsers include declarative shadow DOM support, a very small polyfill is available that can be inlined into your page. This lets you use SSR now for any browsers with JavaScript enabled and incrementally address non-JavaScript use cases as the feature is rolled out to other browsers. The usage of the [`template-shadowroot` polyfill](https://github.com/webcomponents/template-shadowroot) is described below.
3939

40-
### Loading `lit/experimental-hydrate-support.js`
40+
### Loading `@lit-labs/ssr-client/lit-element-hydrate-support.js`
4141
This needs to be loaded before any component modules and the `lit` library.
4242

4343
For example:
4444
```html
4545
<body>
4646
<!-- App components rendered with declarative shadow DOM placed here. -->
4747

48-
<!-- exprimental-hydrate-support should be loaded first. -->
49-
<script src="/node_modules/lit/exprimental-hydrate-support.js"></script>
48+
<!-- ssr-client lit-element-hydrate-support should be loaded first. -->
49+
<script src="/node_modules/@lit-labs/ssr-client/lit-element-hydrate-support.js"></script>
5050

51-
<!-- As compnent definition loads, your pre-rendered components will
51+
<!-- As component definition loads, your pre-rendered components will
5252
come to life and become interactive. -->
5353
<script src="/app-components.js"></script>
5454
</body>
5555
```
5656

57-
If you are [bundling](/docs/v2/tools/production/) your code, make sure the `lit/expriemntal-hydrate-support.js` is imported first:
57+
If you are [bundling](/docs/v2/tools/production/) your code, make sure the `@lit-labs/ssr-client/lit-element-hydrate-support.js` is imported first:
5858
```js
5959
// index.js
60-
import 'lit/experimental-hydrate-support.js';
60+
import '@lit-labs/ssr-client/lit-element-hydrate-support.js';
6161
import './app-components.js';
6262
```
6363

@@ -117,6 +117,6 @@ The HTML snippet below includes an optional strategy to hide the body until the
117117
```
118118

119119
### Combined example
120-
This example shows a strategy that combines both the `lit/experimental-hydrate-support.js` and the `template-shadowroot` polyfill loading and serves a page with a SSRed component to hydrate client-side.
120+
This example shows a strategy that combines both the `@lit-labs/ssr-client/lit-element-hydrate-support.js` and the `template-shadowroot` polyfill loading and serves a page with a SSRed component to hydrate client-side.
121121

122122
[Lit SSR in a Koa server](https://stackblitz.com/edit/lit-ssr-global?file=src/server.js)

packages/lit-dev-content/src/global/lit-hydrate-support.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
// This file is needed for rollup bundling order despite it never being loaded
88
// in Production. Loading this file directly in prod in default.html would cause
99
// an unnecessary second request to lit.js
10-
import 'lit/experimental-hydrate-support.js';
10+
import '@lit-labs/ssr-client/lit-element-hydrate-support.js';

0 commit comments

Comments
 (0)