New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ssr] SSR rendering in NextJS #2391
Comments
We have exactly the same issue, I've made this codesandbox which replicates the issue (just for the reference it's a modified edition of this): |
I managed to make an implementation with |
@mohsenasfiamaersk Thanks for the creating the sample repo. |
Hey @manandkumaar, for ease of event binding, my suggestion would be to use |
+1 for this, we are also working on a design system using lit and want to use in with SSR. |
Any updates on this? We're trying to use a lib component package within our nextjs setup and I'm getting the same issues |
To clarify, the original post of the issue is more akin to issues like #1213 where bringing in the global DOM shim caused breakages. For purely importing Lit components in a Next.js project (without actually server-rendering them) is now possible (without using dynamic imports) without loading the global DOM shim as of Due to the issue title, I believe some of you are looking for actually server-rendering Lit components in a Next.js project as well. We have a working proof of concept for achieving this by patching Polishing this approach, along with designing and publishing a plugin for adding this patch to |
For those that might be subscribed to this issue, we have released Here's a stackblitz example: https://stackblitz.com/edit/nextjs-lit-ssr?file=next.config.js,pages%2Findex.js Please give it a try and let us know your thoughts with issues, discussions, or at our discord server. Thank you! |
@augustjk, please, how does this work with the @lit-labs/react wrapper? |
@watife We added some more prop coordination with the |
@
@augustjk, thank you very much. However, I need some clarification about the implementation of this, please. With this, if I render a component with @lit-labs/react in, for example, next.js, does it work out of the box supporting ssr or any particular configuration I need to do? Thank you once again |
@watife See https://github.com/lit/lit/tree/main/packages/labs/nextjs#readme. There's an example project here https://github.com/lit/lit/tree/main/examples/nextjs. Please open a separate discussion if you're having trouble. |
In the nextjs example, I cannot import a library created with lit-labs/react like shoelace. To import it, based on above example, we will need the entire source of the lit component/library. That does not seem like a good idea. |
@harshunifi If you're talking about duplication of Lit code, that's a shoelace specific issue as they have been bundling Lit into their component code rather than leaving them as bare specifiers to be deduplicated for users, though it looks like that'll be addressed soon shoelace-style/shoelace#559 |
Description
I'm trying to render a lit component via SSR in NextJS, the default behavior is that it fails to import the component because there is no
window
property at the server-side rendering.To fix that, I've imported
@lit-labs/ssr/lib/install-global-dom-shim.js
before importing my lit component.Unfortunately, some packages relay their SSR-detection on the availability of
window
property.And because of the installed shim, packages will fail to detect the valid environment and will be executing client-side code on the server.
The shim installed by
@lit-labs/ssr
does not contain all of the functions of the real, client-sided,window
property.In my case, the package
styled-jsx
tries to execute the following line of code:var node = this._isBrowser && document.querySelector('meta[property="csp-nonce"]');
.This results in an exception because the
_isBrowser
property is true on the server-side.Hardcoding this property results in compiling without any problems.
Steps to Reproduce
element.ts
index.js
npm run build
Expected Results
Use of lit element in NextJS using SSR (which is enabled by default), without adding any new server-side specific functionality to the element itself.
Actual Results
Exception is thrown when builiding:
error - TypeError: document.querySelector is not a function
The text was updated successfully, but these errors were encountered: