0.5.3 • Published 1 month ago

@spyglasses/next v0.5.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Spyglasses for Next.js

npm version License: MIT

A middleware for Next.js that detects and optionally blocks AI bots and trackers, while also tracking human visitors coming from AI platforms like ChatGPT, Claude, and Perplexity.

Key Features

  • High Performance: Uses runtime pattern loading with Next.js built-in fetch caching for optimal performance in serverless environments
  • Bot Detection: Identifies and can block AI crawlers, model trainers, and other bots
  • AI Referrer Tracking: Tracks human visitors coming from AI platforms
  • Customizable Rules: Configure which bots to block or allow with fine-grained control
  • Serverless Ready: Designed specifically for serverless deployments (Vercel, Netlify, etc.)

Installation

npm install @spyglasses/next
# or
yarn add @spyglasses/next
# or
pnpm add @spyglasses/next

Quick Start

  1. Create a middleware.ts file in the root of your Next.js project:
// middleware.ts
import { createSpyglassesMiddleware } from '@spyglasses/next';

export default createSpyglassesMiddleware({
  apiKey: process.env.SPYGLASSES_API_KEY,
  debug: process.env.SPYGLASSES_DEBUG === 'true'
});

export const config = {
  matcher: ['/((?!_next|api|favicon.ico|.*\\.(jpg|jpeg|gif|png|svg|ico|css|js)).*)'],
};
  1. Add your API key to your environment variables (.env.local):
SPYGLASSES_API_KEY=your_api_key_here
SPYGLASSES_CACHE_TTL=86400
SPYGLASSES_DEBUG=true

That's it! The middleware will now detect and log AI bots and referrers visiting your site.

How It Works

Spyglasses uses runtime pattern loading with Next.js caching:

  1. First request: Patterns are fetched from the Spyglasses API
  2. Automatic caching: Next.js caches the response using built-in fetch caching
  3. Subsequent requests: Use cached patterns until TTL expires
  4. Cache revalidation: Automatically refreshes when cache expires

This approach is optimized for serverless environments where build-time data persistence isn't available.

Usage Examples

We provide several example implementations to help you integrate Spyglasses into your Next.js application:

See the examples directory for more details.

Environment Variables

VariableDescriptionDefault
SPYGLASSES_API_KEYYour Spyglasses API keyRequired
SPYGLASSES_CACHE_TTLCache duration in seconds86400 (24 hours)
SPYGLASSES_DEBUGEnable debug logging (true/false)false
SPYGLASSES_COLLECTOR_ENDPOINTOverride the default endpointOptional

Configuration Options

The middleware accepts the following configuration options:

OptionTypeDefaultDescription
apiKeystringprocess.env.SPYGLASSES_API_KEYYour Spyglasses API key
debugbooleanfalseEnable debug logging
blockAiModelTrainersbooleanfalseWhether to block AI model trainers (GPTBot, Claude, etc.)
customBlocksstring[][]Custom patterns to block
customAllowsstring[][]Custom patterns to allow (overrides blocks)
excludePaths(string \| RegExp)[]Default exclusionsPaths to exclude from monitoring
collectEndpointstringprocess.env.SPYGLASSES_COLLECTOR_ENDPOINTOverride the default endpoint

Advanced Usage

Blocking AI Model Trainers

export default createSpyglassesMiddleware({
  apiKey: process.env.SPYGLASSES_API_KEY,
  debug: process.env.SPYGLASSES_DEBUG === 'true',
  blockAiModelTrainers: true, // Blocks GPTBot, Claude-Bot, etc.
});

Custom Block and Allow Rules

export default createSpyglassesMiddleware({
  apiKey: process.env.SPYGLASSES_API_KEY,
  debug: process.env.SPYGLASSES_DEBUG === 'true',
  customBlocks: [
    'category:Scraper',        // Block all scrapers
    'pattern:SomeBot',         // Block specific bot
  ],
  customAllows: [
    'pattern:Googlebot',       // Always allow Googlebot
  ],
});

Path Exclusions

export default createSpyglassesMiddleware({
  apiKey: process.env.SPYGLASSES_API_KEY,
  debug: process.env.SPYGLASSES_DEBUG === 'true',
  excludePaths: [
    '/health',                 // Exclude health check
    /^\/admin/,               // Exclude admin paths (regex)
  ],
});

License

MIT License - see LICENSE for details

0.5.3

1 month ago

0.5.2

1 month ago

0.5.1

1 month ago

0.5.0

1 month ago

0.4.0

2 months ago

0.3.2

2 months ago

0.3.1

2 months ago

0.3.0

2 months ago

0.1.5

3 months ago

0.1.4

3 months ago

0.1.3

3 months ago

0.1.2

3 months ago

0.1.1

3 months ago

0.1.0

3 months ago

0.0.3

3 months ago

0.0.2

3 months ago

0.0.1

3 months ago