Created
January 29, 2019 08:21
-
-
Save koyta/d6adc7b9761672132ec309ca3a1cd147 to your computer and use it in GitHub Desktop.
Checking device for server-side rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smoothscroll from 'smoothscroll-polyfill'; | |
export const DEVICE_TYPE = { | |
MOBILE: 'mobile', | |
MOBILE_BIG: 'mobile-big', | |
TABLET: 'tablet', | |
TABLET_BIG: 'tablet-big', | |
LAPTOP: 'laptop', | |
DESKTOP: 'desktop', | |
}; | |
const isServer = !( | |
typeof window !== 'undefined' && | |
window.document && | |
window.document.createElement && | |
smoothscroll.polyfill() | |
); | |
export default function checkDevice() { | |
if (!process.env.BROWSER || (!isServer && typeof window === 'undefined')) { | |
return DEVICE_TYPE.DESKTOP; | |
} | |
const width = window.innerWidth || document.body.clientWidth; | |
if (width < 576) return DEVICE_TYPE.MOBILE; | |
if (width >= 576 && width < 768) return DEVICE_TYPE.MOBILE_BIG; | |
if (width >= 768 && width < 1024) return DEVICE_TYPE.TABLET; | |
if (width >= 1024 && width < 1440) return DEVICE_TYPE.TABLET_BIG; | |
if (width === 1440) return DEVICE_TYPE.LAPTOP; | |
return DEVICE_TYPE.DESKTOP; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment