You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
More control over connectedCallback when server-side Rendering #5175
I stared building an app using Web Awesome components and @lit-labs/context. To get context running, events and connectedCallback need to be enabled. I enable them both with
This is problematic when 3rd party elements come into play because I cannot expect authors to not call client-only APIs when connectedCallback is not used on the server by default.
More shimming is probably a rabbit hole so I'm thinking that maybe it could be possible to selectively enable connectedCallback for my elements where I use context.
This way, when litSsrCallConnectedCallback is set, connectedCallback would only be called on annotated element.
Alternatives and Workarounds
My reflex was to decommission the litSsrCallConnectedCallback flag. If you'd intend to keep the current behavior of enabling all connectedCallback, it would probably need to be improved and not removed. Thus, to enable the callback ony to annotated elements, one could instead set something like
This was discussed during initial implementation of support for context in SSR and the event handling implementation.
An approach as suggested was dismissed as it leads to additional payload for client side JavaScript.
This does not mean that it is completely off the table, but e.g. an approach for configuring the ElementRenderer in SSR is probably more likely to be considered.
In your example the output from @ssrConnectedCallback would also be included in the JavaScript sent to the browser, even though the browser has nothing to do with SSR. So you send payload that's not necessary for the browser to the frontend.
You could avoid this by generating a different bundle for server and frontend, but that is complicated and requires consuming projects to be aware of that.
Could you explain your other idea? At a first glance this seems a similar solution as to configure the SSR ElementRenderer.
Could you explain your other idea? At a first glance this seems a similar solution as to configure the SSR ElementRenderer.
Yes, I suppose it can be similar. I imagined a function provided to the global flag which takes the element as argument
globalThis.litSsrCallConnectedCallback=function(element){// only run connectedCallback on components with specific tag name prefixreturnelement.tagName.startsWith('my-')}
This does not exclude the annotation option if someone is not concerned with the additional bytes in the bundle. It also becomes locally scoped rather than something that spreads across component libraries...
Then the change in lit-element-renderer would be something like
- if (globalThis.litSsrCallConnectedCallback) {+ if (+ globalThis.litSsrCallConnectedCallback === true + || (typeof globalThis.litSsrCallConnectedCallback === 'function' && globalThis.litSsrCallConnectedCallback(this.element))+ ) {
This is kinda blocking me now. Do you think we could proceed @kyubisation ?
Another idea is to expect a list of allowed elements (or patterns, like /^ex-) to be able to select elements which should run their connectedCallback.
Also, instead of a globalThis flag, maybe this could become part of the SsrOptions object? However, I do not see a connection there.
So maybe instead of the globalThis.litSsrCallConnectedCallback flag, the element renderer could have a method to override and the consumer would extend? Something like
The default implementation could keep using the flag to avoid unnecessary breaking changes
class LitElementRenderer {
connectedCallback() {
// Optionally call connectedCallback via setting: `litSsrCallConnectedCallback`
// Enable this flag to process events dispatched handled via connectedCallback.
- if (globalThis.litSsrCallConnectedCallback) {+ if (this.shouldCallConnectedCallback()) {
...
}
}
+ shouldCallConnectedCallback() {+ return globalThis.litSsrCallConnectedCallback+ }
}
Sorry for the lack of response, my capacity has been limited as of late.
Is this due to third party web components or components under your control (where you could maybe opt out with if (isServer) { return } in connectedCallback())?
If you are able to extend the LitElementRenderer, you can in theory skip litSsrCallConnectedCallback and create your own logic:
I apologize for not immediately providing or adopting a solution here, but we have some additional feature desires in this space. e.g. skipping SSR for a specific element or skipping rehydration on client side.
I'd like to discuss the solution space with a previous/current maintainer of the SSR package. I can promise to bring it up in the next community call and try to define a path forward.
I'm happy to provide a PR implementing the design you choose.
Is this due to third party web components or components under your control
It's 3rd party. I want some of my specific elements to run connectedCallback when SSR but 3rd party elements inevitable will break while connectedCallback is documented as not being called on the server. I can expect that they will call DOM functions unsupported during server rendering.
If you are able to extend the LitElementRenderer [...]
I already do extend the renderer for other purpose and I currently have something like this
The implementation detail behind connectedCallbackAllowed and connectedCallbackDisallowed is regex to check allowed or disallowed custom elements.
My problem with this solution (and your suggestion alike) is the setting of enableUpdating. I extracted this from the superclass and I noticed that the renderer does implements some more logic. It's not simply calling the element. That is why I'm eager to see the solution implemented there.
That is actually everything the current implementation does, so you are fully aligned with your solution.
With litSsrCallConnectedCallback enabled, apart from setting enableUpdating to a noop and calling connectedCallback() the rest is just a sanity check.
I agree with implementing a proper solution out of the box, but I would like to avoid doing something hastily.
I can see the use case for what @tpluscode is calling out here, in particular as popular UI libraries are still working on SSR support, like Shoelace / Web Awesome - shoelace-style/webawesome#1142
I do, in fact, use Web Awesome but incomplete SSR implementation is just part of the story. The documentation of @lit-labs/ssr clearly states that connectedCallback is not used in server-side rendering, so implementors are free to access the DOM and use other client-only functions. It is expected that such elements will not work when litSsrCallConnectedCallback is enabled. And it will not be a bug
Activity
tpluscode commentedon Dec 9, 2025
cc @KonnorRogers
kyubisation commentedon Dec 21, 2025
This was discussed during initial implementation of support for context in SSR and the event handling implementation.
An approach as suggested was dismissed as it leads to additional payload for client side JavaScript.
This does not mean that it is completely off the table, but e.g. an approach for configuring the
ElementRendererin SSR is probably more likely to be considered.tpluscode commentedon Dec 21, 2025
Can you please elaborate why the payload should be increased?
By the way, I was thinking about this and maybe a better solution is a callback value for that consumers would use whether to run connectedCallback
kyubisation commentedon Dec 21, 2025
In your example the output from
@ssrConnectedCallbackwould also be included in the JavaScript sent to the browser, even though the browser has nothing to do with SSR. So you send payload that's not necessary for the browser to the frontend.You could avoid this by generating a different bundle for server and frontend, but that is complicated and requires consuming projects to be aware of that.
Could you explain your other idea? At a first glance this seems a similar solution as to configure the SSR ElementRenderer.
tpluscode commentedon Dec 21, 2025
Yes, I suppose it can be similar. I imagined a function provided to the global flag which takes the element as argument
This does not exclude the annotation option if someone is not concerned with the additional bytes in the bundle. It also becomes locally scoped rather than something that spreads across component libraries...
Then the change in
lit-element-rendererwould be something liketpluscode commentedon Dec 30, 2025
@kyubisation happy to work on a PR
tpluscode commentedon Feb 27, 2026
This is kinda blocking me now. Do you think we could proceed @kyubisation ?
Another idea is to expect a list of allowed elements (or patterns, like
/^ex-) to be able to select elements which should run theirconnectedCallback.Also, instead of a
globalThisflag, maybe this could become part of theSsrOptionsobject? However, I do not see a connection there.So maybe instead of the
globalThis.litSsrCallConnectedCallbackflag, the element renderer could have a method to override and the consumer would extend? Something likeThe default implementation could keep using the flag to avoid unnecessary breaking changes
class LitElementRenderer { connectedCallback() { // Optionally call connectedCallback via setting: `litSsrCallConnectedCallback` // Enable this flag to process events dispatched handled via connectedCallback. - if (globalThis.litSsrCallConnectedCallback) { + if (this.shouldCallConnectedCallback()) { ... } } + shouldCallConnectedCallback() { + return globalThis.litSsrCallConnectedCallback + } }kyubisation commentedon Mar 1, 2026
Sorry for the lack of response, my capacity has been limited as of late.
Is this due to third party web components or components under your control (where you could maybe opt out with
if (isServer) { return }inconnectedCallback())?If you are able to extend the LitElementRenderer, you can in theory skip
litSsrCallConnectedCallbackand create your own logic:I apologize for not immediately providing or adopting a solution here, but we have some additional feature desires in this space. e.g. skipping SSR for a specific element or skipping rehydration on client side.
I'd like to discuss the solution space with a previous/current maintainer of the SSR package. I can promise to bring it up in the next community call and try to define a path forward.
tpluscode commentedon Mar 1, 2026
I'm happy to provide a PR implementing the design you choose.
It's 3rd party. I want some of my specific elements to run
connectedCallbackwhen SSR but 3rd party elements inevitable will break whileconnectedCallbackis documented as not being called on the server. I can expect that they will call DOM functions unsupported during server rendering.I already do extend the renderer for other purpose and I currently have something like this
The implementation detail behind
connectedCallbackAllowedandconnectedCallbackDisallowedis regex to check allowed or disallowed custom elements.My problem with this solution (and your suggestion alike) is the setting of
enableUpdating. I extracted this from the superclass and I noticed that the renderer does implements some more logic. It's not simply calling the element. That is why I'm eager to see the solution implemented there.kyubisation commentedon Mar 1, 2026
That is actually everything the current implementation does, so you are fully aligned with your solution.
With
litSsrCallConnectedCallbackenabled, apart from settingenableUpdatingto a noop and callingconnectedCallback()the rest is just a sanity check.I agree with implementing a proper solution out of the box, but I would like to avoid doing something hastily.
thescientist13 commentedon Mar 3, 2026
I can see the use case for what @tpluscode is calling out here, in particular as popular UI libraries are still working on SSR support, like Shoelace / Web Awesome - shoelace-style/webawesome#1142
tpluscode commentedon Mar 3, 2026
I do, in fact, use Web Awesome but incomplete SSR implementation is just part of the story. The documentation of
@lit-labs/ssrclearly states thatconnectedCallbackis not used in server-side rendering, so implementors are free to access the DOM and use other client-only functions. It is expected that such elements will not work whenlitSsrCallConnectedCallbackis enabled. And it will not be a bug