forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.ts
37 lines (30 loc) · 1.18 KB
/
env.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import React, { useEffect, useLayoutEffect } from 'react'
import { hasRequestAnimationFrame, isWindowDefined } from './helper'
export const IS_REACT_LEGACY = !React.useId
export const IS_SERVER = !isWindowDefined || 'Deno' in window
// Polyfill requestAnimationFrame
export const rAF = (f: (...args: any[]) => void) =>
hasRequestAnimationFrame()
? window['requestAnimationFrame'](f)
: setTimeout(f, 1)
// React currently throws a warning when using useLayoutEffect on the server.
// To get around it, we can conditionally useEffect on the server (no-op) and
// useLayoutEffect in the browser.
export const useIsomorphicLayoutEffect = IS_SERVER ? useEffect : useLayoutEffect
// This assignment is to extend the Navigator type to use effectiveType.
const navigatorConnection =
typeof navigator !== 'undefined' &&
(
navigator as Navigator & {
connection?: {
effectiveType: string
saveData: boolean
}
}
).connection
// Adjust the config based on slow connection status (<= 70Kbps).
export const slowConnection =
!IS_SERVER &&
navigatorConnection &&
(['slow-2g', '2g'].includes(navigatorConnection.effectiveType) ||
navigatorConnection.saveData)