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
This feature is considered beta and not recommended for production at this time.
The current implementation of the Lit SSR plugin for Next.js won't work for users using this as we're looking specifically for the pages directory for the entry points.
Supporting this may be as simple as adding to the test regex but will require testing.
Alternatives and Workarounds
.
Reacted by Looptimize, Berat BAYRAM, Tsukasa Murata, Adrian G, Shawn Choleva, Juan Marín, Leonard Greulich, Vito, Reece McDonald, James Bampoe and 7 more
Reacted by Adrian G
Reacted by Adrian G, Eric Swanson, Shawn Choleva, Juan Marín and Colin
app directory support definitely looks worthwhile to pursue. It does appear that something with the App router breaks Declarative Shadow DOM parsing even for browsers that should support it, so it won't be as simple as updating the entrypoint regex.
@datradito sorry I haven't gotten around to investigating fully yet as we've been focused on other things for the Lit 3.0 release. I can't say for sure when I'll get to it but it's high on my list of things to do after 3.0.
Reacted by Célia, Leonard Greulich and Thomas Jaggi
Hi @augustjk, when we consume the @labs/react wrapped web component in a Next.js application and use the app router, we encounter this error. Do you think this could be due to the app's layout?
I bring this up again as I got caught out when creating a new nextjs app and going for the recommended options, which now don't work without use client
I also played around with bare imports for a simple component and with @lit-labs/[email protected] and @lit/[email protected]
I was able to see a server render for the component, but it then fell into hydration issues described in #3858 even when matching the reflected attributes correctly.
hey! a new issue popped up with the latest version of next (14.0.1):
this issue breaks the build both with importing the enable-lit-ssr.js script from @lit-labs/ssr-react or the withLitSSR() from @lit-labs/nextjs config:
Error occurred prerendering page "/test/98". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot set property createElement of [object Module] which has only a getter
at 9485 (/Users/celiabourial/.dev/lit-next-ssr/.next/server/chunks/392.js:48:19)
at __webpack_require__ (/Users/celiabourial/.dev/lit-next-ssr/.next/server/webpack-runtime.js:1:145)
at 6823 (/Users/celiabourial/.dev/lit-next-ssr/.next/server/chunks/850.js:1:474)
at __webpack_require__ (/Users/celiabourial/.dev/lit-next-ssr/.next/server/webpack-runtime.js:1:145)
at F (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:36:6049)
at $ (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:36:4600)
at rS (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:27173)
at nC (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:52685)
at nR (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:57452)
at nk (/Users/celiabourial/.dev/lit-next-ssr/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:28:49214)
i just tried [email protected] and it builds fine, but with 14.0.1 doesn't (i haven't checked which canary between those two versions breaks it yet though).
i've updated my repro of the original issue repo on a separate branch linked below.
I found a discussion in the nextjs repo regarding declarative shadow dom and posted a comment there. vercel/next.js#51717 (comment)
For now, I think we just won't be able to have custom elements being server rendered with DSD within a React Server Component. Since RSC only get rendered on the server, never client-side, it also means that component definitions won't get shipped if that's the only place it is used.
I tried a home page without any custom elements, that links to another page that does contain a custom element. The way Next.js handles the client routing is to make an API call to the server which returns the serialized server tree including the RSC. This means the tree with the template element that has a prop called "shadowrootmode" which is re-created client side by Next/React. Of course the browser does not treat this as DSD. The custom element won't even client render because code used in RSC is not shipped to the client and it doesn't "hydrate".
To use custom elements with the App router in Next.js, I think the only solution for now is to use them in React client components with the 'use client'; directive. I have checked that that actually does SSR for initial page requests, and hydrates properly. Navigating to other pages that have the custom element in a client component also seems to work.
Something we can do for now would be update the regex used in the plugin for the injection point to allow app folder structure, and document that custom elements to be server rendered should only be used in React client components.
We also need to figure out what's going on with the module situation in Next.js 14. I think that's a separate issue though. This seems related the error @ceIia was seeing above preactjs/signals#429
Activity
ericis commentedon May 15, 2023
@augustjk et al, this is no longer marked as
experimentalin the NextJS configuration when using "create-next-app".See the PR vercel/next.js#49241
And, here is an example:
https://github.com/vercel/next.js/blob/a7601213ab1bd2fd75e120907cd7c70278d7ebc7/packages/create-next-app/templates/app/js/next.config.js#L2
The "old" way used to look like:
augustjk commentedon Jun 20, 2023
appdirectory support definitely looks worthwhile to pursue. It does appear that something with the App router breaks Declarative Shadow DOM parsing even for browsers that should support it, so it won't be as simple as updating the entrypoint regex.I'll investigate and add updates here.
datradito commentedon Aug 3, 2023
Hello @augustjk , is there any news regarding AppRouter support? to work with /app folder in NextJS 13.
Thank you.
augustjk commentedon Aug 4, 2023
@datradito sorry I haven't gotten around to investigating fully yet as we've been focused on other things for the Lit 3.0 release. I can't say for sure when I'll get to it but it's high on my list of things to do after 3.0.
ChellappanRajan commentedon Sep 8, 2023
Hi @augustjk, when we consume the @labs/react wrapped web component in a Next.js application and use the app router, we encounter this error. Do you think this could be due to the app's layout?
steveworkman commentedon Oct 3, 2023
There's a lot of really good investigation in to this from @ceIia & others on Discord. In short:
"use client"makes the error go away, but switches to CSRuse clientis a valid solutioncreateComponentthat works without React hooks, which would then work in RSCIn addition, for component library authors you can add
use clientto all your components as part of the build process like this https://github.com/shuding/react-wrap-balancer/blob/main/tsup.config.ts#L10-L13I bring this up again as I got caught out when creating a new nextjs app and going for the recommended options, which now don't work without
use clientI also played around with bare imports for a simple component and with
@lit-labs/[email protected]and@lit/[email protected]I was able to see a server render for the component, but it then fell into hydration issues described in #3858 even when matching the reflected attributes correctly.
ceIia commentedon Nov 7, 2023
hey! a new issue popped up with the latest version of next (14.0.1):
this issue breaks the build both with importing the
enable-lit-ssr.jsscript from @lit-labs/ssr-react or thewithLitSSR()from @lit-labs/nextjs config:i just tried [email protected] and it builds fine, but with 14.0.1 doesn't (i haven't checked which canary between those two versions breaks it yet though).i've updated my repro of the original issue repo on a separate branch linked below.
→ https://github.com/ceIia/lit-next-ssr/tree/next-14
augustjk commentedon Nov 7, 2023
Oof.. it seems maybe they're doing es modules so monkey patching
React.createElementdoesn't work.ceIia commentedon Nov 8, 2023
update: next.js version 13.4.20-canary.31 is the last version to work. upwards of 13.4.20-canary.32 included, the bug above appears.
https://github.com/vercel/next.js/compare/e262627bd9a371bf1acc643a040799de909f9990..15292da332d1491ffaf0fb3e2e7df3771dbc0156
something here breaks it:

augustjk commentedon Nov 21, 2023
I found a discussion in the nextjs repo regarding declarative shadow dom and posted a comment there. vercel/next.js#51717 (comment)
For now, I think we just won't be able to have custom elements being server rendered with DSD within a React Server Component. Since RSC only get rendered on the server, never client-side, it also means that component definitions won't get shipped if that's the only place it is used.
I tried a home page without any custom elements, that links to another page that does contain a custom element. The way Next.js handles the client routing is to make an API call to the server which returns the serialized server tree including the RSC. This means the tree with the template element that has a prop called "shadowrootmode" which is re-created client side by Next/React. Of course the browser does not treat this as DSD. The custom element won't even client render because code used in RSC is not shipped to the client and it doesn't "hydrate".
To use custom elements with the App router in Next.js, I think the only solution for now is to use them in React client components with the
'use client';directive. I have checked that that actually does SSR for initial page requests, and hydrates properly. Navigating to other pages that have the custom element in a client component also seems to work.augustjk commentedon Nov 21, 2023
Something we can do for now would be update the regex used in the plugin for the injection point to allow app folder structure, and document that custom elements to be server rendered should only be used in React client components.
We also need to figure out what's going on with the module situation in Next.js 14. I think that's a separate issue though. This seems related the error @ceIia was seeing above preactjs/signals#429
Roman86 commentedon Feb 9, 2024
up =) any luck with this?
gaearon commentedon Feb 12, 2024
Left a small comment here: vercel/next.js#51717 (reply in thread)