-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[docs] Add documentation for SSR event handling #1390
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,6 +281,21 @@ and the ContextConsumer controller: | |
); | ||
``` | ||
|
||
### SSR | ||
|
||
Basic support for server side rendering can be enabled by assigning | ||
`globalThis.litSsrCallConnectedCallback = true`. This will configure `@lit-labs/ssr` to call | ||
`connectedCallback()` and `ReactiveController.hostConnected()`, which allows `@lit/context` | ||
to register the necessary event handlers for SSR. | ||
|
||
As an alternative to `document.documentElement` or `document.body` (which are expected to be undefined | ||
in the server environment) for global event listeners (e.g. for `@lit/context ContextProvider`), you can | ||
use the global variable `globalThis.litServerRoot` which is (only) available during SSR. | ||
|
||
```ts | ||
new ContextProvider(isServer ? globalThis.litServerRoot : document.body, {...}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the overloaded constructor signature for ContextProvider is constructor(host: HostElement, options: Options<T>);
/** @deprecated Use new ContextProvider(host, options) */
constructor(host: HostElement, context: T, initialValue?: ContextType<T>);
constructor(
host: HostElement,
contextOrOptions: T | Options<T>,
initialValue?: ContextType<T>
) { /*...* } ContextRoot does not have any constructor params. Do you mean something like this? const root = new ContextRoot();
if (!isServer) {
root.attach(document.body);
} else {
root.attach(globalThis.litServerRoot);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I am referring to the use case of registering a ContextProvider globally. (See https://www.npmjs.com/package/@lit/context and search for But thank you for pointing out, that my example is incomplete and easily misunderstood. I will extend it. 😄 👍 |
||
``` | ||
|
||
## Example Use Cases | ||
|
||
### Current user, locale, etc. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,7 +103,7 @@ Whether a method is called on the server is subject to change while Lit SSR is p | |
| Method | Called on server | Notes | | ||
|-|-|-| | ||
| `constructor()` | Yes ⚠️ | | | ||
| `connectedCallback()` | No | | | ||
| `connectedCallback()` | No | Can be enabled with `globalThis.litSsrCallConnectedCallback = true` | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please give an example of this in this document? |
||
| `disconnectedCallback()` | No | | | ||
| `attributeChangedCallback()` | No | | | ||
| `adoptedCallback()` | No | | | ||
|
@@ -119,7 +119,7 @@ Whether a method is called on the server is subject to change while Lit SSR is p | |
| Method | Called on server | Notes | | ||
|-|-|-| | ||
| `constructor()` | Yes ⚠️ | | | ||
| `hostConnected()` | No | | | ||
| `hostConnected()` | No | Can be enabled with `globalThis.litSsrCallConnectedCallback = true` | | ||
| `hostDisconnected()` | No | | | ||
| `hostUpdate()` | No | | | ||
| `hostUpdated()` | No | | | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this section needs to focus a bit more on the information of "I am googling around and I just want to get this thing done where can I copy paste a solution" and then follow up with the explanation. So like questions a user might have:
So maybe rephrasing a bit backwards might help e.g.