Comparing version 1.0.19 to 1.0.21
@@ -5,2 +5,3 @@ interface UseSSRReturn { | ||
isNative: boolean; | ||
device: Device; | ||
canUseWorkers: boolean; | ||
@@ -11,7 +12,8 @@ canUseEventListeners: boolean; | ||
export declare enum Device { | ||
BROWSER = "BROWSER", | ||
SERVER = "SERVER", | ||
NATIVE = "NATIVE" | ||
Browser = "browser", | ||
Server = "server", | ||
Native = "native" | ||
} | ||
export default function useSSR(): UseSSRReturn; | ||
export {}; | ||
export declare const weAreServer: () => void; | ||
export declare const useSSR: () => UseSSRReturn; | ||
export default useSSR; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_1 = require("react"); | ||
var Device; | ||
(function (Device) { | ||
Device["BROWSER"] = "BROWSER"; | ||
Device["SERVER"] = "SERVER"; | ||
Device["NATIVE"] = "NATIVE"; | ||
Device["Browser"] = "browser"; | ||
Device["Server"] = "server"; | ||
Device["Native"] = "native"; | ||
})(Device = exports.Device || (exports.Device = {})); | ||
const { BROWSER, SERVER, NATIVE } = Device; | ||
const { Browser, Server, Native } = Device; | ||
const canUseDOM = !!(typeof window !== 'undefined' && | ||
@@ -15,23 +14,20 @@ window.document && | ||
const canUseNative = typeof navigator != 'undefined' && navigator.product == 'ReactNative'; | ||
const location = canUseNative ? NATIVE : canUseDOM ? BROWSER : SERVER; | ||
function useSSR() { | ||
const [whereAmI, setWhereAmI] = react_1.useState(location); | ||
const mounted = react_1.useRef(false); | ||
react_1.useEffect(() => { | ||
if (mounted.current) | ||
return; | ||
mounted.current = true; | ||
setWhereAmI(location); | ||
}); | ||
const useSSRObject = react_1.useMemo(() => ({ | ||
isBrowser: whereAmI === BROWSER, | ||
isServer: whereAmI === SERVER, | ||
isNative: whereAmI === NATIVE, | ||
canUseWorkers: typeof Worker !== 'undefined', | ||
canUseEventListeners: whereAmI === BROWSER && !!window.addEventListener, | ||
canUseViewport: whereAmI === BROWSER && !!window.screen | ||
}), [whereAmI]); | ||
return react_1.useMemo(() => Object.assign(Object.values(useSSRObject), useSSRObject), [whereAmI]); | ||
} | ||
exports.default = useSSR; | ||
const device = canUseNative ? Native : canUseDOM ? Browser : Server; | ||
const SSRObject = { | ||
isBrowser: device === Browser, | ||
isServer: device === Server, | ||
isNative: device === Native, | ||
device, | ||
canUseWorkers: typeof Worker !== 'undefined', | ||
canUseEventListeners: device === Browser && !!window.addEventListener, | ||
canUseViewport: device === Browser && !!window.screen, | ||
}; | ||
const toArrayObject = () => Object.assign(Object.values(SSRObject), SSRObject); | ||
let useSSRObject = toArrayObject(); | ||
exports.weAreServer = () => { | ||
SSRObject.isServer = true; | ||
useSSRObject = toArrayObject(); | ||
}; | ||
exports.useSSR = () => useSSRObject; | ||
exports.default = exports.useSSR; | ||
//# sourceMappingURL=useSSR.js.map |
{ | ||
"name": "use-ssr", | ||
"version": "1.0.19", | ||
"version": "1.0.21", | ||
"description": "☯️ React hook to determine if you're on the server or browser", | ||
@@ -5,0 +5,0 @@ "main": "dist/useSSR.js", |
<h1 align="center">useSSR</h1> | ||
<p align="center">☯️ React hook to determine if you are on the server, in the browser, or in react native</p> | ||
<p align="center">☯️ React hook to determine if you are on the server, browser, or react native</p> | ||
<p align="center"> | ||
@@ -101,2 +101,3 @@ <a href="https://github.com/alex-cory/use-ssr/pulls"> | ||
isNative, | ||
device, // 'server', 'browser', or 'native' | ||
canUseWorkers, | ||
@@ -111,2 +112,3 @@ canUseEventListeners, | ||
isNative, | ||
device, // 'server', 'browser', or 'native' | ||
canUseWorkers, | ||
@@ -113,0 +115,0 @@ canUseEventListeners, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127
11351
48