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

fix: Fix FileInput in SSR #2367

Merged

Conversation

sawyerh
Copy link
Contributor

@sawyerh sawyerh commented Apr 30, 2023

Summary

navigator is only available in a browser environment, and calling it on the server causes an exception.

Checking if navigator is defined isn't enough however, since that causes a different React error:

Error: Text content does not match server-rendered HTML.

See more info here: https://nextjs.org/docs/messages/react-hydration-error

To resolve both issues, I've followed the suggestion in the link above, to run the check in useEffect.

Related Issues or PRs

Resolves #2366

How To Test

I tested this locally with a Next.js app by:

  1. Running yarn build
  2. Running npm link in the library's directory
  3. Running npm link @trussworks/react-uswds in the Next.js project
  4. Rendering FileInput in the Next.js project
  5. Running npm run dev in the Next.js project and confirming no errors occur

Screenshots

CleanShot 2023-04-30 at 10 17 43@2x

Copy link
Contributor

@brandonlenz brandonlenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sawyerh long time no see 😉! Just one small thing before I approve. Changes are working well for me

.all-contributorsrc Outdated Show resolved Hide resolved
@brandonlenz
Copy link
Contributor

@all-contributors add @sawyerh for code

@allcontributors
Copy link
Contributor

@brandonlenz

I've put up a pull request to add @sawyerh! 🎉

@sawyerh sawyerh requested a review from brandonlenz May 1, 2023 22:17
@brandonlenz brandonlenz merged commit 3d4845e into trussworks:main May 2, 2023
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 this pull request may close these issues.

[fix] FileInput breaks server-side rendering
2 participants