Skip to content

Commit f0dbafd

Browse files
authored
[docs] Add documentation for SSR event handling
1 parent b291729 commit f0dbafd

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

packages/lit-dev-content/site/docs/v3/data/context.md

+15
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ and the ContextConsumer controller:
281281
);
282282
```
283283

284+
### SSR
285+
286+
Basic support for server side rendering can be enabled by assigning
287+
`globalThis.litSsrCallConnectedCallback = true`. This will configure `@lit-labs/ssr` to call
288+
`connectedCallback()` and `ReactiveController.hostConnected()`, which allows `@lit/context`
289+
to register the necessary event handlers for SSR.
290+
291+
As an alternative to `document.documentElement` or `document.body` (which are expected to be undefined
292+
in the server environment) for global event listeners (e.g. for `@lit/context ContextProvider`), you can
293+
use the global variable `globalThis.litServerRoot` which is (only) available during SSR.
294+
295+
```ts
296+
new ContextProvider(isServer ? globalThis.litServerRoot : document.body, {...});
297+
```
298+
284299
## Example Use Cases
285300

286301
### Current user, locale, etc.

packages/lit-dev-content/site/docs/v3/ssr/authoring.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Whether a method is called on the server is subject to change while Lit SSR is p
103103
| Method | Called on server | Notes |
104104
|-|-|-|
105105
| `constructor()` | Yes ⚠️ | |
106-
| `connectedCallback()` | No | |
106+
| `connectedCallback()` | No | Can be enabled with `globalThis.litSsrCallConnectedCallback = true` |
107107
| `disconnectedCallback()` | No | |
108108
| `attributeChangedCallback()` | No | |
109109
| `adoptedCallback()` | No | |
@@ -119,7 +119,7 @@ Whether a method is called on the server is subject to change while Lit SSR is p
119119
| Method | Called on server | Notes |
120120
|-|-|-|
121121
| `constructor()` | Yes ⚠️ | |
122-
| `hostConnected()` | No | |
122+
| `hostConnected()` | No | Can be enabled with `globalThis.litSsrCallConnectedCallback = true` |
123123
| `hostDisconnected()` | No | |
124124
| `hostUpdate()` | No | |
125125
| `hostUpdated()` | No | |

packages/lit-dev-content/site/docs/v3/ssr/dom-emulation.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ When running in Node, Lit automatically imports and uses a set of DOM shims, and
1717
<!-- TODO(augustinekim) Consider replacing emojis below with icons https://github.com/lit/lit.dev/pull/880#discussion_r944821511 -->
1818
| Property | Notes |
1919
|-|-|
20-
| `Element` | ⚠️ Partial <table><tbody><tr><td>`attributes`</td><td>✅</td><tr><td>`shadowRoot`</td><td>⚠️ Returns `{host: this}` if `attachShadow()` was called with `{mode: 'open'}`</td><tr><td>`setAttribute()`</td><td>✅</td><tr><td>`removeAttribute()`</td><td>✅</td><tr><td>`hasAttribute()`</td><td>✅</td><tr><td>`attachShadow()`</td><td>⚠️ Returns `{host: this}`</td><tr><td>`getAttribute()`</td><td>✅</td></tr></tbody></table> |
21-
| `HTMLElement` | ⚠️ Empty class |
20+
| `EventTarget` | ⚠️ Partial <table><tbody><tr><td>`addEventListener()`</td><td>✅</td><tr><td>`removeEventListener()`</td><td>✅</td><tr><td>`dispatchEvent()`</td><td>✅</td></tr></tbody></table> |
21+
| `Element` | Inherits `EventTarget`<br> ⚠️ Partial <table><tbody><tr><td>`attributes`</td><td>✅</td><tr><td>`shadowRoot`</td><td>⚠️ Returns `{host: this}` if `attachShadow()` was called with `{mode: 'open'}`</td><tr><td>`setAttribute()`</td><td>✅</td><tr><td>`removeAttribute()`</td><td>✅</td><tr><td>`hasAttribute()`</td><td>✅</td><tr><td>`attachShadow()`</td><td>⚠️ Returns `{host: this}`</td><tr><td>`getAttribute()`</td><td>✅</td></tr></tbody></table> |
22+
| `HTMLElement` | Inherits `Element`<br> ⚠️ Empty class |
2223
| `CustomElementRegistry` | <table><tbody><tr><td>`define()`</td><td>✅</td></tr><tr><td>`get()`</td><td>✅</td></tr></tbody></table> |
2324
| `customElements` | Instance of `CustomElementRegistry` |

0 commit comments

Comments
 (0)