Skip to content

More control over connectedCallback when server-side Rendering #5175

Description

@tpluscode

Should this be an RFC?

  • This is not a substantial change

Which package is this a feature request for?

SSR (@lit-labs/ssr)

Description

Re shoelace-style/webawesome#1858

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

globalThis.window = getWindow({
    includeJSBuiltIns: true,
})
globalThis.litSsrCallConnectedCallback = true

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.

A decorator maybe?

import { ssrConnectedCallback } from '@lit-labs/ssr`

@customElement('my-element')
@ssrConnectedCallback
export class MyElement {
}

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

globalThis.litSsrCallConnectedCallback = {
  annotatedOnly: true
}

Activity

  1. added theissue type on Dec 9, 2025
  2. tpluscode commented on Dec 9, 2025

    @tpluscode
    Author
  3. kyubisation commented on Dec 21, 2025

    @kyubisation
    Contributor

    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.

  4. tpluscode commented on Dec 21, 2025

    @tpluscode
    Author

    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

  5. kyubisation commented on Dec 21, 2025

    @kyubisation
    Contributor

    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.

  6. tpluscode commented on Dec 21, 2025

    @tpluscode
    Author

    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 prefix
      return element.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))
    +        ) {
  7. tpluscode commented on Dec 30, 2025

    @tpluscode
    Author

    @kyubisation happy to work on a PR

  8. tpluscode commented on Feb 27, 2026

    @tpluscode
    Author

    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

    class Renderer extends LitElementRenderer {
      shouldCallConnectedCallback() {
        return this.element?.tagName.startsWith('ex-') || super.shouldCallConnectedCallback() 
      }
    }

    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
    +   }
    }
  9. kyubisation commented on Mar 1, 2026

    @kyubisation
    Contributor

    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:

    class MyLitElementRenderer extends LitElementRenderer {
      connectedCallback() {
        if (specificConditionForElement) {
          this.element.connectedCallback();
        }
        super.connectedCallback();
      }
    }

    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.

  10. tpluscode commented on Mar 1, 2026

    @tpluscode
    Author

    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

    connectedCallback(): void {
          const connectedCallbackAllowed = // ...
          const connectedCallbackDisallowed = // ...
    
          if (connectedCallbackAllowed && !connectedCallbackDisallowed) {
              this.element.enableUpdating = function () { }
              this.element.connectedCallback()
          }
    
          return super.connectedCallback()
        }

    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.

  11. kyubisation commented on Mar 1, 2026

    @kyubisation
    Contributor

    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.

  12. thescientist13 commented on Mar 3, 2026

    @thescientist13
    Contributor

    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

  13. tpluscode commented on Mar 3, 2026

    @tpluscode
    Author

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions