Skip to content

Commit

Permalink
fix: undefined navigator (#18)
Browse files Browse the repository at this point in the history
* fix: undefined navigator

* fix: navigator behavior on ssr
  • Loading branch information
AmrAhmedA authored Aug 12, 2024
1 parent 1558859 commit e9c3ac9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@
"rollup-plugin-visualizer": "^5.9.2",
"typescript": "^5.5.4",
"vite": "^4.5.3",
"vue-tsc": "^2.0.29",
"vite-plugin-dts": "^3.9.1"
"vite-plugin-css-injected-by-js": "^3.5.1",
"vite-plugin-dts": "^3.9.1",
"vue-tsc": "^2.0.29"
},
"peerDependencies": {
"react": ">=16",
"react-dom": ">=16",
"react-transition-group": "^4.4.5"
}
}
}
50 changes: 19 additions & 31 deletions src/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
import {
useCallback,
useEffect,
useLayoutEffect,
useReducer,
useRef,
useState,
} from 'react'
import { useCallback, useEffect, useReducer, useRef, useState } from 'react'
import { DEFAULT_POLLING_URL, timeSince } from './utils'

const isWindowDocumentAvailable = typeof window !== 'undefined'

const isNavigatorObjectAvailable = typeof navigator !== 'undefined'

function getConnectionInfo() {
return (
navigator['connection'] ||
navigator['mozConnection'] ||
navigator['webkitConnection'] ||
null
)
if (isNavigatorObjectAvailable)
return (
navigator?.['connection'] ||
navigator?.['mozConnection'] ||
navigator?.['webkitConnection'] ||
null
)
return false
}

const useIsomorphicLayoutEffect =
typeof window !== 'undefined' || typeof navigator !== 'undefined'
? useLayoutEffect
: useEffect

type OnlineStatusProps = {
pollingUrl?: string
pollingDuration?: number
}

const InitialOnlineStatus =
isNavigatorObjectAvailable &&
isWindowDocumentAvailable &&
typeof navigator?.onLine === 'boolean'
? navigator?.onLine
: true
const InitialOnlineStatus = isNavigatorObjectAvailable ? navigator.onLine : true

type ReducerActionTypes = 'offline' | 'online'
type ReducerActions = {
Expand Down Expand Up @@ -175,12 +160,14 @@ function useOnlineStatus({

// Reactive logic for detecting browser side online/offline
useEffect(() => {
window.addEventListener('online', handleOnlineStatus)
window.addEventListener('offline', handleOnlineStatus)
if (isWindowDocumentAvailable) {
window.addEventListener('online', handleOnlineStatus)
window.addEventListener('offline', handleOnlineStatus)

return () => {
window.removeEventListener('online', handleOnlineStatus)
window.removeEventListener('offline', handleOnlineStatus)
return () => {
window.removeEventListener('online', handleOnlineStatus)
window.removeEventListener('offline', handleOnlineStatus)
}
}
}, [handleOnlineStatus])

Expand Down Expand Up @@ -216,6 +203,7 @@ export function useInterval(
function tick() {
savedCallback.current()
}

if (delay !== null) {
const id = setInterval(tick, delay)
return () => clearInterval(id)
Expand All @@ -234,7 +222,7 @@ function useFirstRender(): { isFirstRender: boolean } {
const [firstRender, setFirstRender] = useState(true)

const { isOffline } = useOnlineStatus()
useIsomorphicLayoutEffect(() => {
useEffect(() => {
if (firstRender && isOffline) setFirstRender(false)
}, [isOffline])

Expand Down

0 comments on commit e9c3ac9

Please sign in to comment.