Middleware

MIT

Next.js middleware

vector5() from @vector5/middleware/next. Edge-friendly, fire-and-forget, matcher-aware.

Setup

middleware.ts
import { vector5 } from '@vector5/middleware/next'

export const middleware = vector5({
  endpoint: process.env.V5_HIT_URL!,
  domain: 'example.com',
})

export const config = {
  matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
}

The helper reads user-agent, referer, and x-forwarded-for, then returns without producing a NextResponse. Next.js continues the request as usual.

Matcher

Exclude static assets or you will record every chunk as a crawler hit. The matcher above is a good default. Also skip /api/* if those routes are not pages you care about.

Compose with your own middleware

middleware.ts
import { NextResponse } from 'next/server'
import { vector5 } from '@vector5/middleware/next'

const track = vector5({
  endpoint: process.env.V5_HIT_URL!,
  domain: 'example.com',
})

export function middleware(request: Request) {
  track(request)
  return NextResponse.next()
}

Something off? Open an issue · Edit on GitHub