|
| 1 | +--- |
| 2 | +title: useBotDetection() |
| 3 | +description: A reactive composable for detecting and classifying bots with optional client-side fingerprinting. |
| 4 | +--- |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +**Type:** `function useBotDetection(options?: UseBotDetectionOptions): UseBotDetectionReturn`{lang="ts"} |
| 9 | + |
| 10 | +```ts |
| 11 | +import type { UseBotDetectionOptions, UseBotDetectionReturn } from '@nuxtjs/robots/util' |
| 12 | +``` |
| 13 | + |
| 14 | +Detect and classify bots using server-side header analysis and optional client-side browser fingerprinting. |
| 15 | + |
| 16 | +The composable provides reactive access to bot detection results with automatic caching. Client-side fingerprinting is opt-in due to performance costs. |
| 17 | + |
| 18 | +**🔔 Important:** Bot detection only runs when you use this composable. No automatic bot detection occurs - it's entirely opt-in based on your usage of this composable. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +**Basic detection:** |
| 23 | + |
| 24 | +```ts |
| 25 | +import { useBotDetection } from '#robots/app/composables/useBotDetection' |
| 26 | + |
| 27 | +const { isBot, botName, botCategory, trusted } = useBotDetection() |
| 28 | +// isBot: ComputedRef<boolean> |
| 29 | +// botName: ComputedRef<BotName | undefined> // 'googlebot', 'facebook', etc. |
| 30 | +// botCategory: ComputedRef<BotCategory | undefined> // 'search-engine', 'social', etc. |
| 31 | +// trusted: ComputedRef<boolean | undefined> |
| 32 | +``` |
| 33 | + |
| 34 | +**With fingerprinting:** |
| 35 | + |
| 36 | +```ts |
| 37 | +import { useBotDetection } from '#robots/app/composables/useBotDetection' |
| 38 | + |
| 39 | +const { isBot, botName, botCategory, trusted, reset } = useBotDetection({ |
| 40 | + fingerprint: true, |
| 41 | + onFingerprintError: (error) => { |
| 42 | + console.error('Fingerprint error:', error) |
| 43 | + }, |
| 44 | + onFingerprintResult: (result) => { |
| 45 | + console.log('Fingerprinting completed:', result) |
| 46 | + } |
| 47 | +}) |
| 48 | +``` |
| 49 | + |
| 50 | +**Watching for changes:** |
| 51 | + |
| 52 | +```ts |
| 53 | +import { useBotDetection } from '#robots/app/composables/useBotDetection' |
| 54 | +import { watch } from 'vue' |
| 55 | + |
| 56 | +const { isBot, botName, botCategory } = useBotDetection() |
| 57 | + |
| 58 | +watch(isBot, (detected) => { |
| 59 | + if (detected) { |
| 60 | + console.log(`Bot: ${botName.value} (${botCategory.value})`) |
| 61 | + } |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +## Options |
| 66 | + |
| 67 | +```ts |
| 68 | +interface UseBotDetectionOptions { |
| 69 | + fingerprint?: boolean |
| 70 | + onFingerprintError?: (error: Error) => void |
| 71 | + onFingerprintResult?: (result: BotDetectionContext | null) => void |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### `fingerprint` |
| 76 | + |
| 77 | +**Type:** `boolean` |
| 78 | +**Default:** `false` |
| 79 | + |
| 80 | +Enable automatic client-side fingerprinting when no bot is detected server-side. |
| 81 | + |
| 82 | +### `onFingerprintError` |
| 83 | + |
| 84 | +**Type:** `(error: Error) => void` |
| 85 | + |
| 86 | +Error handler for fingerprinting failures. |
| 87 | + |
| 88 | +### `onFingerprintResult` |
| 89 | + |
| 90 | +**Type:** `(result: BotDetectionContext | null) => void` |
| 91 | + |
| 92 | +Callback that fires when fingerprinting completes, providing the final detection result. |
| 93 | + |
| 94 | +## Return Type |
| 95 | + |
| 96 | +```ts |
| 97 | +interface UseBotDetectionReturn { |
| 98 | + isBot: ComputedRef<boolean> |
| 99 | + botName: ComputedRef<BotName | undefined> |
| 100 | + botCategory: ComputedRef<BotCategory | undefined> |
| 101 | + trusted: ComputedRef<boolean | undefined> |
| 102 | + reset: () => void |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Return Value |
| 107 | + |
| 108 | +### `isBot` |
| 109 | + |
| 110 | +**Type:** `ComputedRef<boolean>` |
| 111 | + |
| 112 | +Reactive boolean indicating whether a bot was detected. |
| 113 | + |
| 114 | +### `botName` |
| 115 | + |
| 116 | +**Type:** `ComputedRef<BotName | undefined>` |
| 117 | + |
| 118 | +The specific bot identity (e.g., 'googlebot', 'facebook', 'claude', 'selenium'). `undefined` if no bot detected. |
| 119 | + |
| 120 | +### `botCategory` |
| 121 | + |
| 122 | +**Type:** `ComputedRef<BotCategory | undefined>` |
| 123 | + |
| 124 | +The bot category/purpose (e.g., 'search-engine', 'social', 'ai', 'automation'). `undefined` if no bot detected. |
| 125 | + |
| 126 | +### `trusted` |
| 127 | + |
| 128 | +**Type:** `ComputedRef<boolean | undefined>` |
| 129 | + |
| 130 | +Whether the detected bot is considered trusted. `undefined` if no bot detected. |
| 131 | + |
| 132 | +### `reset()` |
| 133 | + |
| 134 | +**Type:** `() => void` |
| 135 | + |
| 136 | +Clear all detection state and cached results. |
| 137 | + |
| 138 | +## Server Side Behavior |
| 139 | + |
| 140 | +On the server, bot detection runs when you use the composable: |
| 141 | + |
| 142 | +```ts |
| 143 | +import { useBotDetection } from '#robots/app/composables/useBotDetection' |
| 144 | + |
| 145 | +// Only runs when composable is used |
| 146 | +const { isBot, botName, botCategory } = useBotDetection() |
| 147 | + |
| 148 | +if (isBot.value) { |
| 149 | + // Bot detected via server-side analysis |
| 150 | + console.log('Bot:', botName.value, 'Category:', botCategory.value) |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## Client Side Behavior |
| 155 | + |
| 156 | +Client-side fingerprinting is automatic when enabled: |
| 157 | + |
| 158 | +```ts |
| 159 | +import { useBotDetection } from '#robots/app/composables/useBotDetection' |
| 160 | + |
| 161 | +const { isBot, botName, botCategory } = useBotDetection({ |
| 162 | + fingerprint: true, |
| 163 | + onFingerprintError: (error) => { |
| 164 | + console.error('Fingerprinting failed:', error) |
| 165 | + } |
| 166 | +}) |
| 167 | + |
| 168 | +// Fingerprinting runs automatically if no server detection occurred |
| 169 | +``` |
| 170 | + |
| 171 | +## Configuration |
| 172 | + |
| 173 | +### Disabling Bot Detection |
| 174 | + |
| 175 | +You can disable the entire bot detection plugin: |
| 176 | + |
| 177 | +```ts |
| 178 | +// nuxt.config.ts |
| 179 | +import { defineNuxtConfig } from 'nuxt/config' |
| 180 | + |
| 181 | +export default defineNuxtConfig({ |
| 182 | + robots: { |
| 183 | + botDetection: false |
| 184 | + } |
| 185 | +}) |
| 186 | +``` |
| 187 | + |
| 188 | +When disabled, the `useBotDetection` composable will not be available. |
| 189 | + |
| 190 | +## Bot Categories |
| 191 | + |
| 192 | +The following bot types are detected: |
| 193 | + |
| 194 | +- **search-engine**: Google, Bing, Yandex crawlers |
| 195 | +- **social**: Twitter, Facebook, LinkedIn bots |
| 196 | +- **seo**: Ahrefs, SEMrush, Majestic tools |
| 197 | +- **ai**: GPT, Claude, Perplexity crawlers |
| 198 | +- **automation**: Selenium, Puppeteer, WebDriver |
| 199 | +- **security-scanner**: nmap, Nikto, ZGrab |
| 200 | +- **http-tool**: curl, wget, Python requests |
0 commit comments