Skip to content

[labs/ssr-react] guard against enable-lit-ssr.js wrapping createElement multiple times #4986

Open

Description

@augustjk

Should this be an RFC?

  • This is not a substantial change

Which package is this a feature request for?

Other/unknown (please mention in description)

Description

in server frameworks like next.js, it may be possible that enable-lit-ssr.js might run multiple times in the server, leading to createElement being wrapped multiple times.

part of the observation described #4527 (comment) and from the stackblitz example shared there, refreshing the page causes error because multiple <template shadowrootmode="open"> elements are added to a single custom element.

we're already setting a global flag from this so we could guard the patching of createElement behind that flag too.

Alternatives and Workarounds

.

Activity

  1. ADNolan commented on May 13, 2025

    @ADNolan
    Contributor

    I was tinkering with this a bit, not sure if the idea was/is to have a guard like this

    if (process.env.NODE_ENV === 'production' && !globalThis.litSsrReactEnabled) {
        Object.assign(ReactJSXRuntime.default ?? ReactJSXRuntime, {
            jsx: wrapJsx(ReactJSXRuntime.jsx, ReactJSXRuntime.jsxs),
            jsxs: wrapJsxs(ReactJSXRuntime.jsxs),
        });
    }
    else if(!globalThis.litSsrReactEnabled) {
      Object.assign(ReactJSXDevRuntime.default ?? ReactJSXDevRuntime, {
            jsxDEV: wrapJsxDev(ReactJSXDevRuntime.jsxDEV),
        });
    }

    This doesn't seem to solve the double shadowroot problem in dev mode. It seems to originate from the lit-element-hydrate-support for the client.

    First render:

    page.js:436 DEVELOPMENT: Lit-HTML Private SSR Support
    17:08:42.753 page.js:392 Development Lit-HTML Directive
    17:08:42.756 page.js:238 DEVELOPMENT: litElementHydrateSupport for client
    17:08:42.766 page.js:370 Patching LitElement with litElementHydrateSupport
    17:08:42.772 page.js:403 DEVELOPMENT: Client StyleMap Directive
    17:08:42.774 page.js:51 litElementHydrateSupport: called connectedCallback <my-divider defer-hydration orientation=​"horizontal">​…​</my-divider>​
    17:08:42.776 hook.js:608 Lit is in dev mode. Not recommended for production! See https://lit.dev/msg/dev-mode for more information.
    17:08:42.902 lit-element-hydrate-support.js:46 litElementHydrateSupport: called createRenderRoot: <my-divider orientation=​"horizontal" role=​"separator">​…​</my-divider>​
    17:08:42.903 lit-element-hydrate-support.js:48 litElementHydrateSupport: calling createRenderRoot. Already has shadowroot <my-divider orientation=​"horizontal" role=​"separator">​…​</my-divider>​
    17:08:42.910 lit-element-hydrate-support.js:60 litElementHydrateSupport: called update <my-divider orientation=​"horizontal" role=​"separator">​…​</my-divider>​ Map(1) {'orientation' => undefined}
    17:08:42.911 lit-element-hydrate-support.js:76 litElementHydrateSupport: calling hydrate #shadow-root (open) {host: my-divider}
    

    All other subsequent renders.

    my-divider:1 A second declarative shadow root cannot be created on a host.
    17:11:51.640 page.js:436 DEVELOPMENT: Lit-HTML Private SSR Support
    17:11:51.655 page.js:392 Development Lit-HTML Directive
    17:11:51.658 page.js:238 DEVELOPMENT: litElementHydrateSupport for client
    17:11:51.666 page.js:370 Patching LitElement with litElementHydrateSupport
    17:11:51.672 page.js:403 DEVELOPMENT: Client StyleMap Directive
    17:11:51.673 page.js:51 litElementHydrateSupport: called connectedCallback <my-divider defer-hydration orientation=​"horizontal">​…​</my-divider>​
    17:11:51.675 hook.js:608 Lit is in dev mode. Not recommended for production! See https://lit.dev/msg/dev-mode for more information.
    17:11:51.797 lit-element-hydrate-support.js:35 litElementHydrateSupport: called connectedCallback <my-divider orientation=​"horizontal" role=​"separator">​…​</my-divider>​
    17:11:51.798 lit-element-hydrate-support.js:46 litElementHydrateSupport: called createRenderRoot: <my-divider orientation=​"horizontal" role=​"separator">​…​</my-divider>​
    17:11:51.798 lit-element-hydrate-support.js:53 litElementHydrateSupport: calling createRenderRoot. Needs a shadowroot <my-divider orientation=​"horizontal" role=​"separator">​…​</my-divider>​
    17:11:51.805 on-recoverable-error.js:28 Uncaught Error: Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used:
    17:11:51.806 lit-element-hydrate-support.js:60 litElementHydrateSupport: called update <my-divider orientation=​"horizontal" role=​"separator">​…​</my-divider>​ Map(1) {'orientation' => undefined}
    17:11:51.806 lit-element-hydrate-support.js:80 litElementHydrateSupport: calling render #shadow-root (open) {host: my-divider, renderBefore: null}
    

    I haven't pinpointed what may be loaded out of order or if its a Next.js specific "optimization" causing the problem. Just that in the subsequent page loaded connectedCallback is invoked twice. This could be react strict mode causing it in dev, however, disabling react strict mode in next config didn't address it. Adding console.trace in the lit-element-hydrate-support connectedCallback override seems to indicate React Dev mode.

    Here is an abbreviate trace which is logged after the hydrate support connectedCallback has been invoked when defered-hydration was present.

    main-app.js?v=1747231480747:171 ConnectedCallback
    overrideMethod	@	hook.js:608
    LitElement.connectedCallback	@	lit-element-hydrate-support.js:35
    insertOrAppendPlacementNodeIntoContainer	@	react-dom-client.development.js:12431
    //...many more insertOrAppendPlacementNodeIntoContainer
    commitPlacement	@	react-dom-client.development.js:12514
    runWithFiberInDEV	@	react-dom-client.development.js:845
    commitReconciliationEffects	@	react-dom-client.development.js:13653
    commitMutationEffectsOnFiber	@	react-dom-client.development.js:13645
    recursivelyTraverseMutationEffects	@	react-dom-client.development.js:13204
    commitMutationEffectsOnFiber	@	react-dom-client.development.js:13487
    flushMutationEffects	@	react-dom-client.development.js:15553
    commitRoot	@	react-dom-client.development.js:15527
    commitRootWhenReady	@	react-dom-client.development.js:14759
    performWorkOnRoot	@	react-dom-client.development.js:14682
    performWorkOnRootViaSchedulerTask	@	react-dom-client.development.js:16350
    performWorkUntilDeadline	@	scheduler.development.js:45
    <HeadManagerContext.Provider>		
    exports.jsx	@	react-jsx-runtime.development.js:339
    hydrate	@	app-index.js:192
    eval	@	app-next-dev.js:12
    eval	@	app-bootstrap.js:62
    loadScriptsInSequence	@	app-bootstrap.js:23
    appBootstrap	@	app-bootstrap.js:56
    eval	@	app-next-dev.js:10
    (app-pages-browser)/./node_modules/next/dist/client/app-next-dev.js	@	main-app.js?v=1747231480747:171
    options.factory	@	webpack.js?v=1747231480747:703
    __webpack_require__	@	webpack.js?v=1747231480747:37
    __webpack_exec__	@	main-app.js?v=1747231480747:2813
    (anonymous)	@	main-app.js?v=1747231480747:2814
    webpackJsonpCallback	@	webpack.js?v=1747231480747:1381
    (anonymous)	@	main-app.js?v=1747231480747:9
    

    Recently came to the realization that our directory structure of src/components/* that does not match the default regex for the Next.js SSR Plugin. This was causing multiple of the same parts to be double rendered. One from the server, one from the client. I believe there was an update to the Lit Next.js plug to allow customizing the pattern. Updating to something like test: /\/pages\/.*\.(?:j|t)sx?$|\/app\/(?!api\/).*?\.(?:j|t)sx?$|\/src\/components\/.*\.(?:j|t)sx?$/ ignores the api/api directory since there is no need for Lit SSR there and adds to where the current structure was housing our Lit/React components. Support for this seems to just be waiting for this release of the Next.js plugin.

    Image

  2. ADNolan commented on May 20, 2025

    @ADNolan
    Contributor

    Back at this, here is a stack in the @lit-labs/ssr-react/lib/node/render-custom-element.js. Shows that the React.createElement keeps having more calls added for litPatchedCreateElement on the stack because of the module being called on each page request.

    First render

    Error: renderCustomElement
        at renderCustomElement (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/render-custom-element.js:34:13)
        at Object.litPatchedCreateElement [as createElement] (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/wrap-create-element.js:26:157)
        at BlzHeader (webpack-internal:///(ssr)/./node_modules/@lit/react/node/development/create-component.js:74:22)
    

    Second render

    Error: renderCustomElement
        at renderCustomElement (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/render-custom-element.js:34:13)
        at litPatchedCreateElement (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/wrap-create-element.js:26:157)
        at litPatchedCreateElement (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/wrap-create-element.js:46:24)
        at Object.litPatchedCreateElement [as createElement] (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/wrap-create-element.js:46:24)
        at BlzHeader (webpack-internal:///(ssr)/./node_modules/@lit/react/node/development/create-component.js:74:22)
    

    I patched locally with the following and seems the issue was resolved for duplicated <template> during SSR leading to hydration mismatch and a browser error for attempting to attach multiple shadow roots.

    if(React.createElement.name !== 'litPatchedCreateElement') {
      Object.assign(React.default ?? React, {
        createElement: wrapCreateElement(React.createElement),
      });
    }

    After 5 page loads/renders:

    Error: renderCustomElement
        at renderCustomElement (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/render-custom-element.js:34:13)
        at Object.litPatchedCreateElement [as createElement] (webpack-internal:///(ssr)/./node_modules/@lit-labs/ssr-react/lib/node/wrap-create-element.js:26:157)
        at BlzHeader (webpack-internal:///(ssr)/./node_modules/@lit/react/node/development/create-component.js:74:22)
    
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

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions