Skip to content
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

document is not defined #3196

Open
stepan662 opened this issue Jun 12, 2024 · 8 comments
Open

document is not defined #3196

stepan662 opened this issue Jun 12, 2024 · 8 comments

Comments

@stepan662
Copy link

stepan662 commented Jun 12, 2024

Current behavior:

In some environments, checking for typeof HTMLElement !== undefined might not be enough, when accessing document.

We use emotion in @tolgee/ngx in a dev tools dialog (which is in react). However when using this package in angular 17 SSR, it fails, because it's trying to access document.

typeof HTMLElement !== 'undefined'

To reproduce:

https://stackblitz.com/edit/fuck-yeah-angular-ssr-1e3ut7

Seems like in angular SSR environment document is undefined, but HTMLElement is not, which is causing the issue.

Expected behavior:

I think this check should be more robust e.g typeof document !== 'undefined' && typeof HtmlElement !== 'undefined'.
We are not actually rendering anything using emotion, it's just a part of the package, but since createCache is called immediately when we import the file.

If I replace this check with typeof HTMLElement !== undefined && typeof document !== undefined, it is resolved.

Environment information:

  • @emotion/react version: 11.11.4
@Andarist
Copy link
Member

Seems like in angular SSR environment document is undefined, but HTMLElement is not, which is causing the issue.

This is a bizarre environment. Why does it work like that?

@stepan662
Copy link
Author

I can't really tell you 😄

@Andarist
Copy link
Member

To assess this issue I need to know, at the very least, where this HTMLElement is coming from in this implementation

@stepan662
Copy link
Author

I'm not an angular expert, but I think it works like this because angular is using html element class for rendering, so it needs to have this primitive available on SSR. And it doesn't need the whole document.

@stepan662
Copy link
Author

I think it's kinda not correct to check typeof HTMLElement !== 'undefined' and then touch the document, it would make more sense to check the document itself. (Not sure if there is some reason why it's done like this).

@Andarist
Copy link
Member

Could u check if the new version works in this context?

@stepan662
Copy link
Author

Yes, seems like it's fixed. Thank you ❤️

@stepan662
Copy link
Author

Hey, sorry my previous comment was wrong. I didn't test it properly and the problem is still there. It still only checks for HtmlElement and not document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants