Skip to content

Instantly share code, notes, and snippets.

@codeflorist
Last active March 17, 2023 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeflorist/bc9a36da3dd053352bf5c8c1337018cf to your computer and use it in GitHub Desktop.
Save codeflorist/bc9a36da3dd053352bf5c8c1337018cf to your computer and use it in GitHub Desktop.
Nuxt 3 Plugin to detect bots
/*
Nuxt 3 Plugin to detect bots
---------------------------------------------
Usage as helper in script setup:
const { $isBot } = useNuxtApp()
const isBot = $isBot()
Usage as helper in template:
<div v-if="$isBot()"></div>
*/
export default defineNuxtPlugin(() => {
return {
provide: {
isBot: (): boolean => {
const botRegex = /bot|googlebot|crawler|spider|robot|crawling/i
const isBot =
navigator.userAgent && botRegex.test(navigator.userAgent)
return isBot
},
},
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment