diff --git a/apps/docs/content/docs/meta.json b/apps/docs/content/docs/meta.json
index d6dcd98..a971749 100644
--- a/apps/docs/content/docs/meta.json
+++ b/apps/docs/content/docs/meta.json
@@ -6,6 +6,7 @@
"index",
"---Usage---",
"pageviews",
- "events"
+ "events",
+ "server-side-collection"
]
}
\ No newline at end of file
diff --git a/apps/docs/content/docs/server-side-collection.mdx b/apps/docs/content/docs/server-side-collection.mdx
new file mode 100644
index 0000000..8264f70
--- /dev/null
+++ b/apps/docs/content/docs/server-side-collection.mdx
@@ -0,0 +1,131 @@
+---
+title: Server-side collection (beta)
+description: Count AI agents and robots that request your pages without running the script
+---
+
+AI agents and robots request your pages without running the script, so the script cannot count them.
+With server-side collection, `trackRequest` reports each page request your Next.js server receives to Simple Analytics, which decides whether an AI agent, a robot or a person made it.
+Requests from people are discarded, because the script already counts them.
+
+
+ AI agents appear in your reports and count toward your usage. Robots stay hidden by default and do not count toward your usage.
+ The beta covers how we classify and show this data.
+
+
+## Set it up with an AI agent
+
+Using Claude Code, Codex, Cursor or another AI agent?
+Ask it, with your website's hostname in Simple Analytics instead of `example.com`:
+
+```txt
+Set up Simple Analytics server-side collection for example.com, follow https://docs.simpleanalytics.com/server-side-collection.md
+```
+
+The guide has the agent use `trackRequest` from this package, ask you which query parameters to report, and test the setup without storing anything.
+
+## Set it up yourself
+
+
+
+
+### Set the environment variables
+
+Server-side collection is off until you turn it on.
+Set these environment variables for your production deployment, and remove the second one to turn it off again:
+
+```txt title=".env"
+SIMPLE_ANALYTICS_HOSTNAME=example.com
+SIMPLE_ANALYTICS_SERVER_SIDE_COLLECTION=1
+```
+
+
+
+
+
+### Call `trackRequest` in your proxy
+
+Add `trackRequest` to `proxy.ts` (`middleware.ts` before Next.js 16), next to your project's `app` or `pages` folder.
+If the file already exists, add the `event.waitUntil` line to the top of its function.
+
+
+
+
+```ts title="proxy.ts"
+import { type NextFetchEvent, type NextRequest, NextResponse } from "next/server";
+import { trackRequest } from "@simpleanalytics/next/server";
+
+export function proxy(request: NextRequest, event: NextFetchEvent) {
+ event.waitUntil(trackRequest({ request }));
+
+ return NextResponse.next();
+}
+```
+
+
+
+
+
+```ts title="middleware.ts"
+import { type NextFetchEvent, type NextRequest, NextResponse } from "next/server";
+import { trackRequest } from "@simpleanalytics/next/server";
+
+export function middleware(request: NextRequest, event: NextFetchEvent) {
+ event.waitUntil(trackRequest({ request }));
+
+ return NextResponse.next();
+}
+```
+
+
+
+
+`event.waitUntil` lets the page respond without waiting for the report.
+If your file has a `matcher`, make sure it matches every page you want counted.
+
+
+
+
+## What it reports
+
+- Only `GET` requests. It skips stylesheets, JavaScript, images, fonts, audio, video and JSON by their path, and the paths the plugin uses for the script.
+- The hostname you set, the path, and only these query parameters: `utm_source`, `utm_medium`, `utm_campaign`, `utm_content`, `utm_term`, `source`, `medium`, `campaign`, `content`, `term` and `ref`.
+- The request headers `User-Agent`, `Referer`, `Accept`, `Accept-Language`, `Sec-Fetch-Mode`, `Sec-Fetch-Dest`, `Sec-Fetch-Site`, `Sec-Fetch-User`, `Sec-CH-UA`, `Signature-Agent` and `From`, and the address headers `X-Forwarded-For`, `CF-Connecting-IP` and `True-Client-IP`. Simple Analytics uses addresses in memory only and never stores them.
+- Never cookies or authorization headers.
+
+It runs before Next.js answers, so it does not know the response: pages that do not exist are reported too.
+It sends from production deployments only, gives up after 2 seconds, never retries, and never throws.
+
+
+ Pages that a CDN or cache in front of your Next.js server answers never reach your proxy, and a static export (`output: "export"`) has no server to run it.
+
+
+## Options
+
+| Option | Default | Description |
+| ---------------------- | ---------------------------------------------------------- | ----------------------------------------------------------------------------- |
+| `request` | Required | The request your `proxy` or `middleware` function receives |
+| `hostname` | `SIMPLE_ANALYTICS_HOSTNAME` | The hostname of your website in Simple Analytics |
+| `serverSideCollection` | `SIMPLE_ANALYTICS_SERVER_SIDE_COLLECTION` is `1` or `true` | Turns server-side collection on or off |
+| `allowParams` | `[]` | Query parameters to report besides the ones listed above, such as `["gclid"]` |
+| `testMode` | `SIMPLE_ANALYTICS_TEST_MODE` is `1` or `true` | Asks Simple Analytics to store nothing and prints its reply |
+
+## Test mode
+
+Test mode checks your setup without storing anything, also on your own computer.
+Start your app with test mode on:
+
+```bash
+SIMPLE_ANALYTICS_SERVER_SIDE_COLLECTION=1 SIMPLE_ANALYTICS_TEST_MODE=1 npm run dev
+```
+
+Request a page the way an AI agent does:
+
+```bash
+curl -A "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko); compatible; ChatGPT-User/1.0; +https://openai.com/bot" http://localhost:3000/
+```
+
+Your server console then shows what was sent, including the addresses, and the reply from Simple Analytics, which explains what it would store.
+
+
+ Turn test mode off when you are done: while it is on, nothing is stored and visitors' addresses appear in your server console.
+
diff --git a/package.json b/package.json
index ddbb710..c33eff9 100644
--- a/package.json
+++ b/package.json
@@ -5,6 +5,7 @@
"dev": "turbo run dev",
"build:package": "turbo run build --filter=@simpleanalytics/next",
"lint": "turbo run lint",
+ "test": "pnpm --filter @simpleanalytics/next test",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"devDependencies": {
diff --git a/packages/analytics/README.md b/packages/analytics/README.md
index 429fafb..473146b 100644
--- a/packages/analytics/README.md
+++ b/packages/analytics/README.md
@@ -115,3 +115,50 @@ export async function exampleAction() {
return { success: true };
}
```
+
+### Server-side collection (beta)
+
+AI agents and robots request your pages without running the script, so the script cannot count them. With server-side collection, `trackRequest` reports each page request your Next.js server receives to Simple Analytics, which decides whether an AI agent, a robot or a person made it. Requests from people are discarded, because the script already counts them.
+
+AI agents appear in your reports and count toward your usage. Robots stay hidden by default and do not count toward your usage. The beta covers how we classify and show this data.
+
+#### Set it up with an AI agent
+
+Using Claude Code, Codex, Cursor or another AI agent? Ask it, with your website's hostname in Simple Analytics instead of `example.com`:
+
+```txt
+Set up Simple Analytics server-side collection for example.com, follow https://docs.simpleanalytics.com/server-side-collection.md
+```
+
+The guide has the agent use `trackRequest` from this package and test the setup without storing anything.
+
+#### Set it up yourself
+
+Server-side collection is off until you turn it on. Set these environment variables for your production deployment, and remove the second one to turn it off again:
+
+```txt
+SIMPLE_ANALYTICS_HOSTNAME=example.com
+SIMPLE_ANALYTICS_SERVER_SIDE_COLLECTION=1
+```
+
+Then call `trackRequest` in `proxy.ts` (`middleware.ts` before Next.js 16), next to your project's `app` or `pages` folder. If the file already exists, add the `event.waitUntil` line to the top of its function.
+
+```typescript
+import {
+ type NextFetchEvent,
+ type NextRequest,
+ NextResponse,
+} from "next/server";
+import { trackRequest } from "@simpleanalytics/next/server";
+
+// Before Next.js 16: name the file middleware.ts and the function middleware.
+export function proxy(request: NextRequest, event: NextFetchEvent) {
+ event.waitUntil(trackRequest({ request }));
+
+ return NextResponse.next();
+}
+```
+
+`event.waitUntil` lets the page respond without waiting for the report. If your file has a `matcher`, make sure it matches every page you want counted.
+
+See [server-side collection](https://simpleanalytics-next-docs.vercel.app/docs/server-side-collection) in the documentation for what it reports, all options and how to check your setup with test mode.
diff --git a/packages/analytics/package.json b/packages/analytics/package.json
index c1d3ed9..08319b4 100644
--- a/packages/analytics/package.json
+++ b/packages/analytics/package.json
@@ -1,6 +1,6 @@
{
"name": "@simpleanalytics/next",
- "version": "1.1.1",
+ "version": "1.2.0",
"description": "Simple Analytics for Next.js",
"keywords": [
"analytics",
@@ -23,6 +23,7 @@
},
"scripts": {
"build": "tsup",
+ "test": "node --experimental-strip-types --disable-warning=ExperimentalWarning --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --test test/*.test.mjs",
"format": "prettier --write ."
},
"dependencies": {
diff --git a/packages/analytics/src/server/index.ts b/packages/analytics/src/server/index.ts
index 280c3b7..cc51868 100644
--- a/packages/analytics/src/server/index.ts
+++ b/packages/analytics/src/server/index.ts
@@ -1 +1,2 @@
export { trackEvent, trackPageview } from "./simple-analytics";
+export { trackRequest } from "./track-request";
diff --git a/packages/analytics/src/server/request-payload.ts b/packages/analytics/src/server/request-payload.ts
new file mode 100644
index 0000000..01a2b1b
--- /dev/null
+++ b/packages/analytics/src/server/request-payload.ts
@@ -0,0 +1,132 @@
+export const DEFAULT_ALLOWED_PARAMS = [
+ "utm_source",
+ "utm_medium",
+ "utm_campaign",
+ "utm_content",
+ "utm_term",
+ "source",
+ "medium",
+ "campaign",
+ "content",
+ "term",
+ "ref",
+];
+
+// Paths the plugin rewrites to the Simple Analytics script and queue.
+export const PROXY_PATHS = /^\/(proxy\.js|auto-events\.js|simple\/.*)$/;
+
+// Stylesheets, JavaScript, images, fonts, audio, video and JSON (with source
+// maps and web app manifests).
+const SKIPPED_EXTENSIONS =
+ /\.(css|js|mjs|cjs|apng|avif|bmp|gif|ico|jpe?g|png|svg|tiff?|webp|eot|otf|ttf|woff2?|aac|flac|m4a|mp3|oga|ogg|opus|wav|weba|m4v|mov|mp4|ogv|webm|json|map|webmanifest)$/i;
+
+function isSkipped(request: Request, path: string) {
+ return (
+ request.method !== "GET" ||
+ SKIPPED_EXTENSIONS.test(path) ||
+ // Next.js build assets and the image optimizer.
+ path.includes("/_next/") ||
+ PROXY_PATHS.test(path) ||
+ // Pages Router JSON data, which Next.js shows middleware under the page
+ // path. App Router payloads cannot be told apart: Next.js removes their
+ // markers before middleware runs.
+ request.headers.has("x-nextjs-data")
+ );
+}
+
+// Request headers copied into the body, named in lower case with underscores.
+// They cannot travel under their own names: fetch sets its own Accept,
+// Accept-Language, User-Agent and Sec-Fetch-Mode on the request to the queue.
+const COPIED_HEADERS = {
+ user_agent: "User-Agent",
+ referer: "Referer",
+ accept: "Accept",
+ accept_language: "Accept-Language",
+ sec_fetch_mode: "Sec-Fetch-Mode",
+ sec_fetch_dest: "Sec-Fetch-Dest",
+ sec_fetch_site: "Sec-Fetch-Site",
+ sec_fetch_user: "Sec-Fetch-User",
+ sec_ch_ua: "Sec-CH-UA",
+ signature_agent: "Signature-Agent",
+ from: "From",
+} as const;
+
+type CopiedHeaderField = keyof typeof COPIED_HEADERS;
+
+// Address headers travel to the queue under their own names, unchanged.
+// Next.js gives proxy code no connection address, but it sets X-Forwarded-For
+// to that address when a request arrives without one, and Vercel sets it to
+// the visitor's address.
+const ADDRESS_HEADERS = [
+ "X-Forwarded-For",
+ "CF-Connecting-IP",
+ "True-Client-IP",
+] as const;
+
+export type RequestPayload = {
+ version: 1;
+ platform: "nextjs";
+ hostname: string;
+ path: string;
+ query?: string;
+ test?: true;
+} & { [Field in CopiedHeaderField]?: string };
+
+function filterQuery(searchParams: URLSearchParams, allowParams: string[]) {
+ const allowed = new Set([...DEFAULT_ALLOWED_PARAMS, ...allowParams]);
+ const query = new URLSearchParams();
+
+ for (const [name, value] of searchParams) {
+ if (allowed.has(name)) {
+ query.append(name, value);
+ }
+ }
+
+ return query.toString();
+}
+
+export function buildRequestReport(
+ request: Request,
+ options: {
+ hostname: string;
+ allowParams?: string[] | undefined;
+ test?: boolean | undefined;
+ },
+) {
+ const url = new URL(request.url);
+
+ if (isSkipped(request, url.pathname)) {
+ return undefined;
+ }
+
+ const query = filterQuery(url.searchParams, options.allowParams ?? []);
+
+ const body: RequestPayload = {
+ version: 1,
+ platform: "nextjs",
+ hostname: options.hostname,
+ path: url.pathname,
+ ...(query && { query }),
+ ...(options.test && { test: true }),
+ };
+
+ for (const field of Object.keys(COPIED_HEADERS) as CopiedHeaderField[]) {
+ const value = request.headers.get(COPIED_HEADERS[field]);
+
+ if (value !== null) {
+ body[field] = value;
+ }
+ }
+
+ const headers: Record = {};
+
+ for (const name of ADDRESS_HEADERS) {
+ const value = request.headers.get(name);
+
+ if (value) {
+ headers[name] = value;
+ }
+ }
+
+ return { body, headers };
+}
diff --git a/packages/analytics/src/server/simple-analytics.ts b/packages/analytics/src/server/simple-analytics.ts
index 52e0779..8c97ea6 100644
--- a/packages/analytics/src/server/simple-analytics.ts
+++ b/packages/analytics/src/server/simple-analytics.ts
@@ -16,6 +16,7 @@ import {
} from "./utils";
import { parseHeaders } from "./headers";
import { parseUtmParameters } from "./utm";
+import { PROXY_PATHS } from "./request-payload";
type TrackEventOptions = TrackingOptions & (ServerContext | HeaderOnlyContext);
@@ -78,8 +79,6 @@ export async function trackEvent(
}
}
-const PROXY_PATHS = /^\/(proxy\.js|auto-events\.js|simple\/.*)$/;
-
type TrackPageviewOptions = TrackingOptions & ServerContext;
export async function trackPageview(options: TrackPageviewOptions) {
diff --git a/packages/analytics/src/server/track-request.ts b/packages/analytics/src/server/track-request.ts
new file mode 100644
index 0000000..729e552
--- /dev/null
+++ b/packages/analytics/src/server/track-request.ts
@@ -0,0 +1,118 @@
+import "server-only";
+
+import { buildRequestReport } from "./request-payload";
+import { isBuildTime, isProduction } from "./utils";
+
+const QUEUE_URL = "https://queue.simpleanalyticscdn.com/requests";
+const TIMEOUT_MS = 2000;
+
+function isOn(value: string | undefined) {
+ return value === "1" || value === "true";
+}
+
+function logTestMode(message: string) {
+ console.log(`Simple Analytics test mode: ${message}`);
+}
+
+/**
+ * Reports a page request for server-side collection, so AI agents and robots
+ * that do not run the script are counted. Call it from `proxy.ts` (or
+ * `middleware.ts` before Next.js 16) with `event.waitUntil` so the page does
+ * not wait for it. It never throws.
+ */
+export async function trackRequest(options: {
+ /** The incoming request in `proxy.ts` or `middleware.ts`. */
+ request: Request;
+ /** Defaults to the `SIMPLE_ANALYTICS_HOSTNAME` environment variable. */
+ hostname?: string | undefined;
+ /**
+ * Turns server-side collection on or off. Defaults to the
+ * `SIMPLE_ANALYTICS_SERVER_SIDE_COLLECTION` environment variable, which is
+ * on when set to `1` or `true`. Off by default.
+ */
+ serverSideCollection?: boolean | undefined;
+ /** Query parameters to report besides the UTM parameters and `ref`. */
+ allowParams?: string[] | undefined;
+ /**
+ * Asks the queue to store nothing and prints its reply on the server
+ * console. Defaults to the `SIMPLE_ANALYTICS_TEST_MODE` environment
+ * variable, which is on when set to `1` or `true`.
+ */
+ testMode?: boolean | undefined;
+}) {
+ const testMode =
+ options.testMode ?? isOn(process.env.SIMPLE_ANALYTICS_TEST_MODE);
+
+ try {
+ const enabled =
+ options.serverSideCollection ??
+ isOn(process.env.SIMPLE_ANALYTICS_SERVER_SIDE_COLLECTION);
+
+ if (!enabled) {
+ if (testMode) {
+ logTestMode(
+ "server-side collection is off. Set SIMPLE_ANALYTICS_SERVER_SIDE_COLLECTION=1 to turn it on.",
+ );
+ }
+ return;
+ }
+
+ const hostname = options.hostname ?? process.env.SIMPLE_ANALYTICS_HOSTNAME;
+
+ if (!hostname) {
+ if (testMode) {
+ logTestMode(
+ "no hostname. Set SIMPLE_ANALYTICS_HOSTNAME to the hostname of your website in Simple Analytics.",
+ );
+ }
+ return;
+ }
+
+ // Outside test mode, only production deployments report requests.
+ if (isBuildTime() || (!testMode && !isProduction())) {
+ return;
+ }
+
+ const report = buildRequestReport(options.request, {
+ hostname,
+ allowParams: options.allowParams,
+ test: testMode,
+ });
+
+ if (!report) {
+ return;
+ }
+
+ // One attempt, abandoned after 2 seconds, including reading the reply.
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS);
+
+ try {
+ const response = await fetch(QUEUE_URL, {
+ method: "POST",
+ headers: { "Content-Type": "application/json", ...report.headers },
+ body: JSON.stringify(report.body),
+ signal: controller.signal,
+ });
+
+ if (testMode) {
+ logTestMode(
+ [
+ `sent ${JSON.stringify(report.body)}`,
+ `with address headers ${JSON.stringify(report.headers)}`,
+ `queue replied ${response.status}: ${await response.text()}`,
+ ].join("\n"),
+ );
+ } else {
+ await response.body?.cancel();
+ }
+ } finally {
+ clearTimeout(timeout);
+ }
+ } catch (error) {
+ // A failed report never affects the page. Only test mode mentions it.
+ if (testMode) {
+ logTestMode(`the request was not reported: ${error}`);
+ }
+ }
+}
diff --git a/packages/analytics/test/request-payload.test.mjs b/packages/analytics/test/request-payload.test.mjs
new file mode 100644
index 0000000..c5c6108
--- /dev/null
+++ b/packages/analytics/test/request-payload.test.mjs
@@ -0,0 +1,237 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+
+import { buildRequestReport } from "../src/server/request-payload.ts";
+
+const hostname = "example.com";
+
+function get(url, headers = {}) {
+ return new Request(url, { headers });
+}
+
+test("a page request reports the hostname setting, not the Host header", () => {
+ const report = buildRequestReport(
+ get("https://www.example.org/pricing", { Host: "www.example.org" }),
+ { hostname },
+ );
+
+ assert.deepEqual(report.body, {
+ version: 1,
+ platform: "nextjs",
+ hostname: "example.com",
+ path: "/pricing",
+ });
+});
+
+test("the query keeps only allowed parameters, in their original order", () => {
+ const report = buildRequestReport(
+ get(
+ "https://example.com/pricing?utm_source=chatgpt.com&ref=ai&token=synthetic-secret&source=demo&email=person%40example.invalid&utm_medium=social&utm_campaign=launch&utm_content=hero&utm_term=analytics&medium=m&campaign=c&content=x&term=t",
+ ),
+ { hostname },
+ );
+
+ assert.equal(report.body.path, "/pricing");
+ assert.equal(
+ report.body.query,
+ "utm_source=chatgpt.com&ref=ai&source=demo&utm_medium=social&utm_campaign=launch&utm_content=hero&utm_term=analytics&medium=m&campaign=c&content=x&term=t",
+ );
+});
+
+test("the query is left out when no allowed parameter is present", () => {
+ const report = buildRequestReport(
+ get("https://example.com/account?token=synthetic-secret&UTM_SOURCE=upper"),
+ { hostname },
+ );
+
+ assert.equal("query" in report.body, false);
+});
+
+test("allowParams adds parameters to the default list", () => {
+ const report = buildRequestReport(
+ get("https://example.com/?gclid=abc&utm_source=newsletter&session=1"),
+ { hostname, allowParams: ["gclid"] },
+ );
+
+ assert.equal(report.body.query, "gclid=abc&utm_source=newsletter");
+});
+
+test("copied request headers travel in the body under their snake_case names", () => {
+ const report = buildRequestReport(
+ get("https://example.com/pricing", {
+ "User-Agent": "Mozilla/5.0 (compatible; ChatGPT-User/1.0)",
+ Referer: "https://chatgpt.com/",
+ Accept: "text/html,application/xhtml+xml",
+ "Accept-Language": "en-US",
+ "Sec-Fetch-Mode": "navigate",
+ "Sec-Fetch-Dest": "document",
+ "Sec-Fetch-Site": "cross-site",
+ "Sec-Fetch-User": "?1",
+ "Sec-CH-UA": '"Chromium";v="130"',
+ "Signature-Agent": '"https://chatgpt.com"',
+ From: "robot@example.invalid",
+ Cookie: "session=synthetic-secret",
+ Authorization: "Bearer synthetic-secret",
+ "X-Forwarded-For": "203.0.113.7",
+ }),
+ { hostname },
+ );
+
+ assert.deepEqual(report.body, {
+ version: 1,
+ platform: "nextjs",
+ hostname: "example.com",
+ path: "/pricing",
+ user_agent: "Mozilla/5.0 (compatible; ChatGPT-User/1.0)",
+ referer: "https://chatgpt.com/",
+ accept: "text/html,application/xhtml+xml",
+ accept_language: "en-US",
+ sec_fetch_mode: "navigate",
+ sec_fetch_dest: "document",
+ sec_fetch_site: "cross-site",
+ sec_fetch_user: "?1",
+ sec_ch_ua: '"Chromium";v="130"',
+ signature_agent: '"https://chatgpt.com"',
+ from: "robot@example.invalid",
+ });
+});
+
+test("a header missing from the request is left out, an empty one is kept", () => {
+ const report = buildRequestReport(
+ get("https://example.com/", { "User-Agent": "" }),
+ { hostname },
+ );
+
+ assert.equal(report.body.user_agent, "");
+ assert.equal("referer" in report.body, false);
+ assert.equal("accept" in report.body, false);
+});
+
+test("address headers pass on unchanged as headers, never in the body", () => {
+ const report = buildRequestReport(
+ get("https://example.com/pricing", {
+ "X-Forwarded-For": "198.51.100.23, 203.0.113.7",
+ "CF-Connecting-IP": "198.51.100.23",
+ "True-Client-IP": "198.51.100.24",
+ "X-Real-IP": "192.0.2.1",
+ Cookie: "session=synthetic-secret",
+ Authorization: "Bearer synthetic-secret",
+ }),
+ { hostname },
+ );
+
+ assert.deepEqual(report.headers, {
+ "X-Forwarded-For": "198.51.100.23, 203.0.113.7",
+ "CF-Connecting-IP": "198.51.100.23",
+ "True-Client-IP": "198.51.100.24",
+ });
+ assert.deepEqual(report.body, {
+ version: 1,
+ platform: "nextjs",
+ hostname: "example.com",
+ path: "/pricing",
+ });
+});
+
+test("no address header is invented when the request has none", () => {
+ const report = buildRequestReport(get("https://example.com/pricing"), {
+ hostname,
+ });
+
+ assert.deepEqual(report.headers, {});
+});
+
+test("only GET requests are reported", () => {
+ for (const method of ["POST", "HEAD", "PUT", "DELETE", "OPTIONS"]) {
+ const request = new Request("https://example.com/pricing", { method });
+
+ assert.equal(buildRequestReport(request, { hostname }), undefined, method);
+ }
+});
+
+test("stylesheets, JavaScript, images, fonts, audio, video and JSON are skipped by path", () => {
+ const skipped = [
+ "/styles/site.css",
+ "/app.js",
+ "/module.mjs",
+ "/legacy.cjs",
+ "/app.js.map",
+ "/logo.png",
+ "/photo.JPG",
+ "/photo.jpeg",
+ "/animation.gif",
+ "/hero.webp",
+ "/hero.avif",
+ "/icon.svg",
+ "/favicon.ico",
+ "/fonts/inter.woff2",
+ "/fonts/inter.woff",
+ "/fonts/inter.ttf",
+ "/fonts/inter.otf",
+ "/podcast.mp3",
+ "/sound.wav",
+ "/sound.ogg",
+ "/clip.mp4",
+ "/clip.webm",
+ "/api/data.json",
+ "/manifest.webmanifest",
+ "/_next/static/chunks/main.js",
+ "/_next/image",
+ "/docs/_next/image",
+ "/simple/append",
+ ];
+
+ for (const path of skipped) {
+ const request = get(`https://example.com${path}?w=64`);
+
+ assert.equal(buildRequestReport(request, { hostname }), undefined, path);
+ }
+});
+
+test("pages, unknown paths and other files are reported", () => {
+ const reported = [
+ "/",
+ "/pricing",
+ "/blog/v1.2/release-notes",
+ "/page-that-does-not-exist",
+ "/about.html",
+ "/robots.txt",
+ "/sitemap.xml",
+ "/feed.rss",
+ "/whitepaper.pdf",
+ "/llms.txt",
+ ];
+
+ for (const path of reported) {
+ const report = buildRequestReport(get(`https://example.com${path}`), {
+ hostname,
+ });
+
+ assert.equal(report?.body.path, path);
+ }
+});
+
+test("Pages Router data requests are skipped as JSON", () => {
+ // Next.js hands middleware these /_next/data//pricing.json requests
+ // with the page path, and keeps the x-nextjs-data header.
+ const request = get("https://example.com/pricing", { "x-nextjs-data": "1" });
+
+ assert.equal(buildRequestReport(request, { hostname }), undefined);
+});
+
+test("test mode asks the queue to store nothing and explain", () => {
+ const request = get("https://example.com/pricing?utm_source=chatgpt.com");
+
+ assert.deepEqual(buildRequestReport(request, { hostname, test: true }).body, {
+ version: 1,
+ platform: "nextjs",
+ hostname: "example.com",
+ path: "/pricing",
+ query: "utm_source=chatgpt.com",
+ test: true,
+ });
+ assert.equal(
+ "test" in buildRequestReport(request, { hostname, test: false }).body,
+ false,
+ );
+});