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

["react-aria/interactions] "Uncaught Error: document is not defined" when use SSR and window is defined #7926

Open
LisaRyrholm opened this issue Mar 13, 2025 · 2 comments · May be fixed by #7927

Comments

@LisaRyrholm
Copy link

LisaRyrholm commented Mar 13, 2025

Provide a general summary of the issue here

When using SSR and Window is define we get an error ("Uncaught Error: document is not defined") as soon as we add an react-aria component in our solution. Found it when added but also other components.

This may also solve part of the issue: #6534 and #6234

🤔 Expected Behavior?

When doing SSR no exception should be thrown when Window is defined.

😯 Current Behavior

When running the SSR our solution gets the "Uncaught Error: document is not defined" exception.

💁 Possible Solution

Make sure to not only check window === "undefined" but also check document === "undefined"

🔦 Context

Our solution uses both frontend and SSR and it is important that the SSR works the same as the frontend rendering.
Doing a workaround with useIsSSR and not load react-aria components if SSR is not acceptable.

🖥️ Steps to Reproduce

Adding a window = {}; on line 23 in your test-utils/src/ssrWorker.js file will make most of your SSR test suit to crash. Showing the problem with most of the component if window is set but not document. test by running yarn test:ssr

Version

1.7.1

What browsers are you seeing the problem on?

Chrome, Firefox

If other, please specify.

No response

What operating system are you using?

Windows 11

🧢 Your Company/Team

Avensia/Lyko

🕷 Tracking Issue

No response

@LisaRyrholm
Copy link
Author

I have forked this repo and will soon provide a PR to fix this.

LisaRyrholm added a commit to LisaRyrholm/react-spectrum that referenced this issue Mar 13, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
mback2k Marc Hörsken
Fix error when window is set but document is undefined when running SSR. Modify test input data to show problem.
LisaRyrholm added a commit to LisaRyrholm/react-spectrum that referenced this issue Mar 13, 2025
Fix error when window is set but document is undefined when running SSR. Modify test input data to show problem.

Ignore lint error in testfile
LisaRyrholm added a commit to LisaRyrholm/react-spectrum that referenced this issue Mar 13, 2025
Fix error when window is set but document is undefined when running SSR.
Modify test input data to show problem.
Ignore lint error in testfile
@LisaRyrholm LisaRyrholm linked a pull request Mar 13, 2025 that will close this issue
5 tasks
@ehellman
Copy link

This would be great! We've got window defined in server scope of our projects and always guard our browser-checks with both window and window.document!

Using any component from react-aria-components in any of our projects right now causes this exact error for us, so a fix is very welcome!

LisaRyrholm added a commit to LisaRyrholm/react-spectrum that referenced this issue Mar 22, 2025
Fix error when window is set but document is undefined when running SSR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants