Skip to content

Instantly share code, notes, and snippets.

@gangsthub
Created September 25, 2019 13:42
Show Gist options
  • Save gangsthub/60f23237e6196a4bbcb0da4f292d2f35 to your computer and use it in GitHub Desktop.
Save gangsthub/60f23237e6196a4bbcb0da4f292d2f35 to your computer and use it in GitHub Desktop.
`nuxtServerInit` checking if it's mobile
/*
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