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
[labs/ssr-react] guard against enable-lit-ssr.js wrapping createElement multiple times #4986
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.
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.
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.
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.
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)
Activity
ADNolan commentedon May 13, 2025
I was tinkering with this a bit, not sure if the idea was/is to have a guard like this
This doesn't seem to solve the double shadowroot problem in dev mode. It seems to originate from the
lit-element-hydrate-supportfor the client.First render:
All other subsequent renders.
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
connectedCallbackis invoked twice. This could be react strict mode causing it in dev, however, disabling react strict mode in next config didn't address it. Addingconsole.tracein thelit-element-hydrate-supportconnectedCallback 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-hydrationwas present.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 liketest: /\/pages\/.*\.(?:j|t)sx?$|\/app\/(?!api\/).*?\.(?:j|t)sx?$|\/src\/components\/.*\.(?:j|t)sx?$/ignores theapi/apidirectory 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.ADNolan commentedon May 20, 2025
Back at this, here is a stack in the
@lit-labs/ssr-react/lib/node/render-custom-element.js. Shows that theReact.createElementkeeps having more calls added for litPatchedCreateElement on the stack because of the module being called on each page request.First render
Second render
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.After 5 page loads/renders: