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
Many libraries check for the presence of window to decide if they are running in the browser or on the server. This can pose a problem when trying to run such a library alongside Lit SSR, because the Lit SSR DOM shim defines window, so libraries that would usually be compatible with Node get confused about where they're running and break.
We don't really need to define window in our DOM shim, because any supported global API (e.g. customElements.define) that a Lit component library needs to call for SSR can be referenced without the namespace, or using globalThis instead of window.
However, it is somewhat common to see e.g. window.addEventListener in connectedCallback, so removing window would break those cases. Ultimately, I think it's a better trade off to not define window, and just require those occasional lines to switch to globalThis. (We don't actually call connectedCallback or shim addEventListener currently, but we likely will soon).
In the immediate term, to avoid a breaking change, maybe we should make the non-window defining version opt-in. We could export another function from dom-shim.js called e.g. installDomShimWithoutWindow -- and then eventually make this the standard behavior.
(Side note: our implementation of isServer uses node export conditions instead of checking any globals, which I think would be a great direction for other libraries to move in, since it avoids this problem and also avoids any runtime check: https://github.com/lit/lit/blob/main/packages/lit-html/src/is-server.ts)
The text was updated successfully, but these errors were encountered:
Many libraries check for the presence of
window
to decide if they are running in the browser or on the server. This can pose a problem when trying to run such a library alongside Lit SSR, because the Lit SSR DOM shim defineswindow
, so libraries that would usually be compatible with Node get confused about where they're running and break.We don't really need to define
window
in our DOM shim, because any supported global API (e.g.customElements.define
) that a Lit component library needs to call for SSR can be referenced without the namespace, or usingglobalThis
instead ofwindow
.However, it is somewhat common to see e.g.
window.addEventListener
inconnectedCallback
, so removingwindow
would break those cases. Ultimately, I think it's a better trade off to not definewindow
, and just require those occasional lines to switch toglobalThis
. (We don't actually callconnectedCallback
or shimaddEventListener
currently, but we likely will soon).In the immediate term, to avoid a breaking change, maybe we should make the non-window defining version opt-in. We could export another function from dom-shim.js called e.g.
installDomShimWithoutWindow
-- and then eventually make this the standard behavior.(Side note: our implementation of
isServer
uses node export conditions instead of checking any globals, which I think would be a great direction for other libraries to move in, since it avoids this problem and also avoids any runtime check: https://github.com/lit/lit/blob/main/packages/lit-html/src/is-server.ts)The text was updated successfully, but these errors were encountered: