Skip to content

Commit b615b32

Browse files
committed
use parameterized route in withSentry, if available
1 parent 4902789 commit b615b32

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/nextjs/src/utils/withSentry.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,23 @@ export const withSentry = (origHandler: NextApiHandler, parameterizedRoute?: str
8484
const baggageHeader = req.headers && req.headers.baggage;
8585
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader);
8686

87-
const url = `${req.url}`;
88-
// pull off query string, if any
89-
let reqPath = stripUrlQueryAndFragment(url);
90-
// Replace with placeholder
91-
if (req.query) {
92-
// TODO get this from next if possible, to avoid accidentally replacing non-dynamic parts of the path if
93-
// they happen to match the values of any of the dynamic parts
94-
for (const [key, value] of Object.entries(req.query)) {
95-
reqPath = reqPath.replace(`${value}`, `[${key}]`);
87+
// prefer the parameterized route, if we have it (which we will if we've auto-wrapped the route handler)
88+
let reqPath = parameterizedRoute;
89+
90+
// If not, fake it by just replacing parameter values with their names, hoping that none of them match either
91+
// each other or any hard-coded parts of the path
92+
if (!reqPath) {
93+
const url = `${req.url}`;
94+
// pull off query string, if any
95+
reqPath = stripUrlQueryAndFragment(url);
96+
// Replace with placeholder
97+
if (req.query) {
98+
for (const [key, value] of Object.entries(req.query)) {
99+
reqPath = reqPath.replace(`${value}`, `[${key}]`);
100+
}
96101
}
97102
}
103+
98104
const reqMethod = `${(req.method || 'GET').toUpperCase()} `;
99105

100106
const transaction = startTransaction(

0 commit comments

Comments
 (0)