Last active
March 17, 2023 09:59
-
-
Save codeflorist/bc9a36da3dd053352bf5c8c1337018cf to your computer and use it in GitHub Desktop.
Nuxt 3 Plugin to detect bots
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
/* | |
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