File tree 3 files changed +17
-4
lines changed
3 files changed +17
-4
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @jest -environment node
3
+ */
4
+
5
+ import { isServer } from '../core/utils' ;
6
+
7
+ describe ( 'utils-node-env' , ( ) => {
8
+ test ( 'isServer should work' , async ( ) => {
9
+ expect ( isServer ) . toBe ( true ) ;
10
+ } ) ;
11
+ } ) ;
Original file line number Diff line number Diff line change @@ -17,10 +17,12 @@ export const isFunction = (fn: unknown): fn is Function =>
17
17
18
18
export const isNil = ( val : unknown ) => val === null || val === undefined ;
19
19
20
+ export const isServer = typeof window === 'undefined' ;
21
+
20
22
export const isDocumentVisibility = ( ) =>
21
- window ?. document ?. visibilityState === 'visible' ;
23
+ ! isServer && window ?. document ?. visibilityState === 'visible' ;
22
24
23
- export const isOnline = ( ) => window ?. navigator ?. onLine ?? true ;
25
+ export const isOnline = ( ) => ( ! isServer && window ?. navigator ?. onLine ) ?? true ;
24
26
25
27
export const unRefObject = < T extends RefObject > ( val : T ) => {
26
28
const obj = { } ;
Original file line number Diff line number Diff line change 1
- import { isDocumentVisibility } from './index' ;
1
+ import { isDocumentVisibility , isServer } from './index' ;
2
2
3
3
type EventFunc = ( ) => void ;
4
4
type ListenersSet = Set < EventFunc > ;
@@ -40,7 +40,7 @@ const observer = (listeners: ListenersSet) => {
40
40
} ;
41
41
42
42
/* istanbul ignore else */
43
- if ( window ?. addEventListener ) {
43
+ if ( ! isServer && window ?. addEventListener ) {
44
44
window . addEventListener (
45
45
'visibilitychange' ,
46
46
( ) => {
You can’t perform that action at this time.
0 commit comments