Skip to content

Instantly share code, notes, and snippets.

@mercs600
Last active March 27, 2020 17:58
Show Gist options
  • Save mercs600/56f34419e29bcfbc4906e97b37819d32 to your computer and use it in GitHub Desktop.
Save mercs600/56f34419e29bcfbc4906e97b37819d32 to your computer and use it in GitHub Desktop.
nuxt-ssr-excercise-2
<template>
<div>
<h1> Context usage </h1>
<div v-if="isMacOS">
Content for MacOS
</div>
<div v-else-if="isWindows">
Content for Windows
</div>
<div v-else>
Content for crawlers and linux devs ;-)
</div>
</div>
</template>
<script>
function isWindows (a) {
return /Windows/.test(a)
}
function isMacOS (a) {
return /Mac OS X/.test(a)
}
export default {
asyncData (context) {
let agent = 'Googlebot/2.1 (+http://www.google.com/bot.html)'
if (process.server) {
const { req } = context
agent = req.headers['user-agent']
} else if (process.client && typeof navigator !== 'undefined') {
agent = navigator.userAgent
}
return {
isWindows: isWindows(agent),
isMacOS: isMacOS(agent)
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment