React/Vue/Svelte components for conditionally rendering content based on bot detection with IP validation. Server-side rendering only.
npm install bot-gate
import { BotGate } from "bot-gate/react";
export async function getServerSideProps({ req }) {
return {
props: {
userAgent: req.headers["user-agent"],
ipAddress: req.ip || req.connection.remoteAddress,
},
};
}
function ProductPage({ userAgent, ipAddress }) {
return (
<div>
<BotGate
userAgent={userAgent}
ipAddress={ipAddress}
display="show"
role="bot"
>
<FullProductDescription />
<StructuredData />
</BotGate>
<BotGate
userAgent={userAgent}
ipAddress={ipAddress}
display="show"
role="user"
>
<InteractiveGallery />
<AddToCartButton />
</BotGate>
</div>
);
}
<template>
<div>
<BotGate
:user-agent="userAgent"
:ip-address="ipAddress"
display="show"
role="bot"
>
<FullProductDescription />
<StructuredData />
</BotGate>
<BotGate
:user-agent="userAgent"
:ip-address="ipAddress"
display="show"
role="user"
>
<InteractiveGallery />
<AddToCartButton />
</BotGate>
</div>
</template>
<script>
import { BotGate } from "bot-gate/vue";
export default {
components: { BotGate },
async asyncData({ req }) {
return {
userAgent: req.headers["user-agent"],
ipAddress: req.ip,
};
},
};
</script>
<script>
import { BotGate } from 'bot-gate/svelte';
export let userAgent;
export let ipAddress;
</script>
<BotGate {userAgent} {ipAddress} display="show" role="bot">
<FullProductDescription />
<StructuredData />
</BotGate>
<BotGate {userAgent} {ipAddress} display="show" role="user">
<InteractiveGallery />
<AddToCartButton />
</BotGate>
import { validateBot } from "bot-gate/core";
// Validate using user agent and IP address
const userAgent =
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
const isValid = validateBot(userAgent, "66.249.64.1");
// Returns: true
const isInvalid = validateBot(userAgent, "192.168.1.1");
// Returns: false
That's it! One simple function that returns true
for valid bots, false
for everything else.
Prop | Type | Required | Description |
---|---|---|---|
userAgent |
string | ✓ | User agent string from request headers |
ipAddress |
string | ✓ | IP address from request |
display |
'show' | 'hide' | ✓ | Whether to show or hide content |
role |
'bot' | 'user' | ✓ | Target role for the display rule |
bots |
string[] | ✗ | Specific bot names to target |
// Show SEO content to search engines
<BotGate userAgent={userAgent} ipAddress={ipAddress} display="show" role="bot">
<h1>SEO Optimized Title</h1>
<meta name="description" content="..." />
</BotGate>
// Show interactive content to users
<BotGate userAgent={userAgent} ipAddress={ipAddress} display="show" role="user">
<InteractiveWidget />
<LazyLoadedImages />
</BotGate>
<BotGate
userAgent={userAgent}
ipAddress={ipAddress}
display="show"
role="bot"
bots={["googlebot", "bingbot"]}
>
<SearchEngineContent />
</BotGate>
Available bot names for the bots
prop:
"googlebot"
- Google search bot"bingbot"
- Microsoft Bing search bot "applebot"
- Apple search bot"duckduckbot"
- DuckDuckGo search bot"oai-searchbot"
- OpenAI SearchBot"chatgpt-user"
- ChatGPT User"gptbot"
- GPTBot"perplexitybot"
- PerplexityBot"perplexity-user"
- Perplexity User"applebot-extended"
- Apple Extended bot"duckassistbot"
- DuckDuckGo AI bot// Hide complex UI from bots
<BotGate userAgent={userAgent} ipAddress={ipAddress} display="hide" role="bot">
<ComplexJavaScriptWidget />
</BotGate>
// Equivalent to showing content to users only
<BotGate userAgent={userAgent} ipAddress={ipAddress} display="show" role="user">
<ComplexJavaScriptWidget />
</BotGate>
// pages/_app.js or in your page component
export async function getServerSideProps({ req }) {
return {
props: {
userAgent: req.headers["user-agent"] || "",
ipAddress: req.ip || req.connection.remoteAddress || "",
},
};
}
// nuxt.config.js
export default {
serverMiddleware: [
{
path: "/api",
handler: (req, res, next) => {
req.ip = req.connection.remoteAddress;
next();
},
},
],
};
// src/routes/+layout.server.js
export async function load({ request, getClientAddress }) {
return {
userAgent: request.headers.get("user-agent") || "",
ipAddress: getClientAddress(),
};
}
Bot Gate uses a two-step verification process:
Both checks must pass for a request to be considered a legitimate bot. This prevents:
MIT
Made by Hall