Created
September 25, 2019 13:42
-
-
Save paulmelero/60f23237e6196a4bbcb0da4f292d2f35 to your computer and use it in GitHub Desktop.
`nuxtServerInit` checking if it's mobile
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
/* | |
This needs to be in the store/index.js | |
*/ | |
const mutationTypes = { | |
SET_MOBILE: 'SET_MOBILE', | |
} | |
const checkIfMobileOnServerSide = ({ req, isServer }) => { | |
if (isServer) return false | |
const userAgent = req && req.headers['user-agent'] | |
const isMobile = /mobile/i.test(('' + userAgent).toLowerCase()) | |
return isMobile | |
} | |
export const state = () => ({ | |
isMobile: false, | |
}) | |
export const getters = { | |
isMobile: state => state.isMobile, | |
} | |
export const mutations = { | |
[mutationTypes.SET_MOBILE](state, isMobile) { | |
state.isMobile = isMobile | |
}, | |
} | |
export const actions = { | |
// https://nuxtjs.org/guide/vuex-store/#the-nuxtserverinit-action | |
nuxtServerInit({ commit }, context) { | |
commit(mutationTypes.SET_MOBILE, checkIfMobileOnServerSide(context)) | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment