Skip to content

Commit 8db1794

Browse files
committed
refactor: use isMatchingPattern util for consistent SDK behavior
1 parent 40cd0ec commit 8db1794

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

packages/nextjs/src/config/withSentryConfig/getFinalConfigObjectUtils.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseSemver } from '@sentry/core';
1+
import { isMatchingPattern, parseSemver } from '@sentry/core';
22
import { getSentryRelease } from '@sentry/node';
33
import { createRouteManifest } from '../manifest/createRouteManifest';
44
import type { RouteManifest } from '../manifest/types';
@@ -134,13 +134,7 @@ export function filterRouteManifest(manifest: RouteManifest, excludeFilter: Excl
134134
return excludeFilter(route);
135135
}
136136

137-
return excludeFilter.some(pattern => {
138-
if (typeof pattern === 'string') {
139-
return route === pattern;
140-
}
141-
142-
return !!route.match(pattern);
143-
});
137+
return excludeFilter.some(pattern => isMatchingPattern(route, pattern));
144138
};
145139

146140
return {

packages/nextjs/test/config/manifest/excludeRoutesFromManifest.test.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,40 @@ describe('routeManifestInjection.exclude', () => {
2828
});
2929

3030
describe('with string patterns', () => {
31-
it('should exclude exact string matches', () => {
31+
it('should exclude routes containing the string pattern (substring match)', () => {
3232
const result = filterRouteManifest(mockManifest, ['/admin']);
3333

34+
// All routes containing '/admin' are excluded
35+
expect(result.staticRoutes.map(r => r.path)).toEqual(['/', '/about', '/internal/secret', '/public/page']);
36+
expect(result.dynamicRoutes.map(r => r.path)).toEqual(['/users/:id', '/secret-feature/:id']);
37+
expect(result.isrRoutes).toEqual(['/blog', '/internal/stats']);
38+
});
39+
40+
it('should exclude routes matching multiple string patterns', () => {
41+
const result = filterRouteManifest(mockManifest, ['/about', '/blog']);
42+
3443
expect(result.staticRoutes.map(r => r.path)).toEqual([
3544
'/',
36-
'/about',
37-
'/admin/dashboard', // Not excluded - not exact match
45+
'/admin',
46+
'/admin/dashboard',
3847
'/internal/secret',
3948
'/public/page',
4049
]);
50+
expect(result.isrRoutes).toEqual(['/admin/reports', '/internal/stats']);
4151
});
4252

43-
it('should exclude multiple exact matches', () => {
44-
const result = filterRouteManifest(mockManifest, ['/admin', '/about', '/blog']);
53+
it('should match substrings anywhere in the route', () => {
54+
// 'secret' matches '/internal/secret' and '/secret-feature/:id'
55+
const result = filterRouteManifest(mockManifest, ['secret']);
4556

4657
expect(result.staticRoutes.map(r => r.path)).toEqual([
4758
'/',
59+
'/about',
60+
'/admin',
4861
'/admin/dashboard',
49-
'/internal/secret',
5062
'/public/page',
5163
]);
52-
expect(result.isrRoutes).toEqual(['/admin/reports', '/internal/stats']);
64+
expect(result.dynamicRoutes.map(r => r.path)).toEqual(['/users/:id', '/admin/users/:id']);
5365
});
5466
});
5567

@@ -82,19 +94,8 @@ describe('routeManifestInjection.exclude', () => {
8294
expect(result.dynamicRoutes.map(r => r.path)).toEqual(['/users/:id', '/admin/users/:id']);
8395
});
8496

85-
it('should handle regex with global flag consistently across multiple routes', () => {
86-
// Regex with `g` flag has stateful lastIndex - ensure it works correctly
87-
const globalRegex = /admin/g;
88-
const result = filterRouteManifest(mockManifest, [globalRegex]);
89-
90-
// All admin routes should be excluded, not just every other one
91-
expect(result.staticRoutes.map(r => r.path)).toEqual(['/', '/about', '/internal/secret', '/public/page']);
92-
expect(result.dynamicRoutes.map(r => r.path)).toEqual(['/users/:id', '/secret-feature/:id']);
93-
expect(result.isrRoutes).toEqual(['/blog', '/internal/stats']);
94-
});
95-
96-
it('should handle regex with global and case-insensitive flags', () => {
97-
const result = filterRouteManifest(mockManifest, [/ADMIN/gi]);
97+
it('should handle case-insensitive regex', () => {
98+
const result = filterRouteManifest(mockManifest, [/ADMIN/i]);
9899

99100
expect(result.staticRoutes.map(r => r.path)).toEqual(['/', '/about', '/internal/secret', '/public/page']);
100101
expect(result.dynamicRoutes.map(r => r.path)).toEqual(['/users/:id', '/secret-feature/:id']);

0 commit comments

Comments
 (0)