Last active
March 27, 2020 17:58
-
-
Save mercs600/56f34419e29bcfbc4906e97b37819d32 to your computer and use it in GitHub Desktop.
nuxt-ssr-excercise-2
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
<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