Skip to content

Commit a49edb2

Browse files
committed
feat(perf): re-use the existing parsed route manifest
1 parent ec73021 commit a49edb2

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
import { WINDOW } from '@sentry/react';
2-
import type { RouteManifest } from '../../config/manifest/types';
3-
import { maybeParameterizeRoute } from './parameterization';
2+
import { getManifest, maybeParameterizeRoute } from './parameterization';
43

5-
const globalWithInjectedValues = WINDOW as typeof WINDOW & {
6-
_sentryRouteManifest: string;
7-
};
4+
const IS_ISR_SSG_ROUTE_CACHE = new Map<string, boolean>();
85

96
/**
107
* Check if the current page is an ISR/SSG route by checking the route manifest.
118
*/
129
function isIsrSsgRoute(pathname: string): boolean {
13-
const manifestData = globalWithInjectedValues._sentryRouteManifest;
14-
if (!manifestData || typeof manifestData !== 'string') {
15-
return false;
16-
}
10+
// Early parameterization to get the cache key
11+
const parameterizedPath = maybeParameterizeRoute(pathname);
12+
const pathToCheck = parameterizedPath || pathname;
1713

18-
let manifest: RouteManifest;
19-
try {
20-
manifest = JSON.parse(manifestData);
21-
} catch {
22-
return false;
14+
// Check cache using the parameterized path as the key
15+
if (IS_ISR_SSG_ROUTE_CACHE.has(pathToCheck)) {
16+
return IS_ISR_SSG_ROUTE_CACHE.get(pathToCheck) as boolean;
2317
}
2418

25-
if (!manifest.isrRoutes || !Array.isArray(manifest.isrRoutes) || manifest.isrRoutes.length === 0) {
19+
// Cache miss get the manifest
20+
const manifest = getManifest();
21+
if (!manifest?.isrRoutes || !Array.isArray(manifest.isrRoutes) || manifest.isrRoutes.length === 0) {
22+
IS_ISR_SSG_ROUTE_CACHE.set(pathToCheck, false);
2623
return false;
2724
}
2825

29-
const parameterizedPath = maybeParameterizeRoute(pathname);
30-
const pathToCheck = parameterizedPath || pathname;
26+
const isIsrSsgRoute = manifest.isrRoutes.includes(pathToCheck);
27+
IS_ISR_SSG_ROUTE_CACHE.set(pathToCheck, isIsrSsgRoute);
3128

32-
return manifest.isrRoutes.includes(pathToCheck);
29+
return isIsrSsgRoute;
3330
}
3431

3532
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function getCompiledRegex(regexString: string): RegExp | null {
7474
* Get and cache the route manifest from the global object.
7575
* @returns The parsed route manifest or null if not available/invalid.
7676
*/
77-
function getManifest(): RouteManifest | null {
77+
export function getManifest(): RouteManifest | null {
7878
if (
7979
!globalWithInjectedManifest?._sentryRouteManifest ||
8080
typeof globalWithInjectedManifest._sentryRouteManifest !== 'string'

0 commit comments

Comments
 (0)