Skip to content

Commit b5e7c6c

Browse files
committed
fix: just delete the tags
1 parent bcb6f82 commit b5e7c6c

File tree

3 files changed

+77
-56
lines changed

3 files changed

+77
-56
lines changed

packages/nextjs/src/client/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from '../common';
1515
export { captureUnderscoreErrorException } from '../common/pages-router-instrumentation/_error';
1616
export { browserTracingIntegration } from './browserTracingIntegration';
1717
export { captureRouterTransitionStart } from './routing/appRouterRoutingInstrumentation';
18+
import { removeIsrSsgTraceMetaTags } from './routing/isrRoutingTracing';
1819

1920
let clientIsInitialized = false;
2021

@@ -41,6 +42,10 @@ export function init(options: BrowserOptions): Client | undefined {
4142
}
4243
clientIsInitialized = true;
4344

45+
// Remove cached trace meta tags for ISR/SSG pages before initializing
46+
// This prevents the browser tracing integration from using stale trace IDs
47+
removeIsrSsgTraceMetaTags();
48+
4449
const opts = {
4550
environment: getVercelEnv(true) || process.env.NODE_ENV,
4651
defaultIntegrations: getDefaultIntegrations(options),

packages/nextjs/src/client/routing/appRouterRoutingInstrumentation.ts

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import type { Client, Span } from '@sentry/core';
22
import {
33
browserPerformanceTimeOrigin,
4-
GLOBAL_OBJ,
54
SEMANTIC_ATTRIBUTE_SENTRY_OP,
65
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
76
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
87
} from '@sentry/core';
98
import { startBrowserTracingNavigationSpan, startBrowserTracingPageLoadSpan, WINDOW } from '@sentry/react';
10-
import type { RouteManifest } from '../../config/manifest/types';
119
import { maybeParameterizeRoute } from './parameterization';
10+
import { GLOBAL_OBJ } from '@sentry/core';
1211

1312
export const INCOMPLETE_APP_ROUTER_INSTRUMENTATION_TRANSACTION_NAME = 'incomplete-app-router-transaction';
1413

@@ -34,66 +33,21 @@ let navigationRoutingMode: 'router-patch' | 'transition-start-hook' = 'router-pa
3433

3534
const currentRouterPatchingNavigationSpanRef: NavigationSpanRef = { current: undefined };
3635

37-
/**
38-
* Check if the current route is an ISR/SSG page by looking it up in the route manifest
39-
*/
40-
function isIsrSsgRoute(pathname: string): boolean {
41-
const globalWithManifest = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
42-
_sentryRouteManifest?: string | RouteManifest;
43-
};
44-
45-
const manifestData = globalWithManifest._sentryRouteManifest;
46-
if (!manifestData) {
47-
return false;
48-
}
49-
50-
let manifest: RouteManifest;
51-
if (typeof manifestData === 'string') {
52-
try {
53-
manifest = JSON.parse(manifestData);
54-
} catch {
55-
return false;
56-
}
57-
} else {
58-
manifest = manifestData;
59-
}
60-
61-
if (!manifest.isrRoutes || manifest.isrRoutes.length === 0) {
62-
return false;
63-
}
64-
65-
// Check if the pathname matches any ISR route
66-
// For dynamic routes, we need to match the parameterized pattern
67-
const parameterizedPath = maybeParameterizeRoute(pathname);
68-
const pathToCheck = parameterizedPath || pathname;
69-
70-
return manifest.isrRoutes.includes(pathToCheck);
71-
}
72-
7336
/** Instruments the Next.js app router for pageloads. */
7437
export function appRouterInstrumentPageLoad(client: Client): void {
7538
const parameterizedPathname = maybeParameterizeRoute(WINDOW.location.pathname);
7639
const origin = browserPerformanceTimeOrigin();
7740

78-
// Check if this is an ISR/SSG page
79-
// if so, don't use cached trace meta tags to prevent using cached trace data
80-
const isIsrSsgPage = isIsrSsgRoute(WINDOW.location.pathname);
81-
82-
startBrowserTracingPageLoadSpan(
83-
client,
84-
{
85-
name: parameterizedPathname ?? WINDOW.location.pathname,
86-
// pageload should always start at timeOrigin (and needs to be in s, not ms)
87-
startTime: origin ? origin / 1000 : undefined,
88-
attributes: {
89-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
90-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.nextjs.app_router_instrumentation',
91-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: parameterizedPathname ? 'route' : 'url',
92-
},
41+
startBrowserTracingPageLoadSpan(client, {
42+
name: parameterizedPathname ?? WINDOW.location.pathname,
43+
// pageload should always start at timeOrigin (and needs to be in s, not ms)
44+
startTime: origin ? origin / 1000 : undefined,
45+
attributes: {
46+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
47+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.nextjs.app_router_instrumentation',
48+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: parameterizedPathname ? 'route' : 'url',
9349
},
94-
// For ISR/SSG pages, pass empty trace data to prevent using cached meta tags
95-
isIsrSsgPage ? { sentryTrace: undefined, baggage: undefined } : undefined,
96-
);
50+
});
9751
}
9852

9953
interface NavigationSpanRef {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { RouteManifest } from '../../config/manifest/types';
2+
import { maybeParameterizeRoute } from './parameterization';
3+
import { GLOBAL_OBJ } from '@sentry/core';
4+
5+
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
6+
_sentryRouteManifest: string | RouteManifest;
7+
};
8+
9+
/**
10+
* Check if the current page is an ISR/SSG route by checking the route manifest.
11+
*/
12+
function isIsrSsgRoute(pathname: string): boolean {
13+
const manifestData = globalWithInjectedValues._sentryRouteManifest;
14+
if (!manifestData) {
15+
return false;
16+
}
17+
18+
let manifest: RouteManifest;
19+
if (typeof manifestData === 'string') {
20+
try {
21+
manifest = JSON.parse(manifestData);
22+
} catch {
23+
return false;
24+
}
25+
} else {
26+
manifest = manifestData;
27+
}
28+
29+
if (!manifest.isrRoutes || manifest.isrRoutes.length === 0) {
30+
return false;
31+
}
32+
33+
const parameterizedPath = maybeParameterizeRoute(pathname);
34+
const pathToCheck = parameterizedPath || pathname;
35+
36+
return manifest.isrRoutes.includes(pathToCheck);
37+
}
38+
39+
/**
40+
* Remove sentry-trace and baggage meta tags from the DOM if this is an ISR/SSG page.
41+
* This prevents the browser tracing integration from using stale/cached trace IDs.
42+
*/
43+
export function removeIsrSsgTraceMetaTags(): void {
44+
if (typeof document === 'undefined') {
45+
return;
46+
}
47+
48+
if (!isIsrSsgRoute(window.location.pathname)) {
49+
return;
50+
}
51+
52+
// Remove the meta tags so browserTracingIntegration won't pick them up
53+
const sentryTraceMeta = document.querySelector('meta[name="sentry-trace"]');
54+
if (sentryTraceMeta) {
55+
sentryTraceMeta.remove();
56+
}
57+
58+
const baggageMeta = document.querySelector('meta[name="baggage"]');
59+
if (baggageMeta) {
60+
baggageMeta.remove();
61+
}
62+
}

0 commit comments

Comments
 (0)