Skip to content

Commit 9515437

Browse files
vklimontovichclaude
andcommitted
fix: suppress false "middleware not added" warning on RSC navigations
Add x-nl-active marker header on early-return middleware paths so the Server component can distinguish "middleware not installed" from "middleware intentionally skipped this request." Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5e6258f commit 9515437

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

packages/core/src/middleware.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { NextMiddleware, NextRequest } from "next/server";
22
import { NextResponse, after } from "next/server";
33
import {
44
LAST_PAGE_RENDER_ID_COOKIE,
5+
headerNames,
56
serializeServerComponentContext,
67
} from "./server-component-context";
78
import type {
@@ -80,13 +81,17 @@ export function createNextlyticsMiddleware(
8081

8182
// Skip internal paths, prefetch, and static files
8283
if (reqInfo.isNextjsInternal || reqInfo.isPrefetch || reqInfo.isStaticFile) {
83-
return undefined;
84+
const response = NextResponse.next();
85+
response.headers.set(headerNames.active, "1");
86+
return response;
8487
}
8588

8689
// Skip non-page-navigation, non-API requests (e.g. RSC fetches).
8790
// Soft navigations are tracked via the client /api/event request.
8891
if (!reqInfo.isPageNavigation && !config.isApiPath(pathname)) {
89-
return undefined;
92+
const response = NextResponse.next();
93+
response.headers.set(headerNames.active, "1");
94+
return response;
9095
}
9196

9297
const pageRenderId = generateId();

packages/core/src/server-component-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const headerNames = {
88
search: `${HEADER_PREFIX}search`,
99
pageRenderId: `${HEADER_PREFIX}page-render-id`,
1010
isSoftNavigation: `${HEADER_PREFIX}is-soft-nav`,
11+
active: `${HEADER_PREFIX}active`,
1112
scripts: `${HEADER_PREFIX}scripts`,
1213
} as const;
1314

packages/core/src/server.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ export function Nextlytics(userConfig: NextlyticsConfig): NextlyticsResult {
243243
const ctx = restoreServerComponentContext(headersList);
244244

245245
if (!ctx) {
246-
console.warn("[Nextlytics] nextlyticsMiddleware should be added in order for Server to work");
246+
// x-nl-page-render-id absent → check if middleware is at least active
247+
if (!headersList.get(headerNames.active)) {
248+
console.warn("[Nextlytics] nextlyticsMiddleware should be added in order for Server to work");
249+
}
247250
return <>{children}</>;
248251
}
249252

0 commit comments

Comments
 (0)