Skip to content

[FIX] replace direct window reference with navigator check for ssr compatibility #6

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kikoruiz
Copy link

Remove window reference from navigator.userAgent for SSR compatibility

Problem

The Edge browser detection was using window.navigator.userAgent which causes issues when the code is parsed in Node.js environments (like during Server-Side Rendering). Even though there's a typeof navigator !== 'undefined' check, the direct window.navigator reference still breaks SSR compatibility.

Solution

Simplified the Edge browser detection to use navigator.userAgent directly instead of window.navigator.userAgent. This change:

  • Prevents Node.js parsing errors: Removes unnecessary window reference since navigator is already being checked
  • Maintains SSR compatibility: The typeof navigator !== 'undefined' check already ensures safe access
  • Future-proof for Node.js v21+: The Navigator global object was implemented in Node.js from v21, so using navigator directly (with proper checks) is more compatible
  • Preserves functionality: The Edge-specific scrollHeight bug workaround continues to work exactly as before
  • Cleaner code: Removes redundant window. prefix when navigator global is already being checked

This is a small but important fix for teams using this hook in SSR applications like Next.js, Nuxt.js, or other server-rendered React applications, and ensures better compatibility with modern Node.js versions.

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.

5 participants