Skip to content

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