Skip to content

Commit fa8c15b

Browse files
authored
fix(nextjs): Handle pathname being passed in object in instrumentServer (#5782)
In nextjs version 12.2.6, the signature of `findPageComponents` which we wrap in `instrumentServer` changed[1], from accepting individual parameters to accepting an options object. This changes our wrapping to accommodate that, and as a result fixes transactions started by `instrumentServer` being parameterized as `[ object Object ]`. For backwards compatibility with older versions of next, individual parameters also continue to be accepted. [1] vercel/next.js@5f95b6b#diff-2b8e3503c1bebf856610a5551d9c5996435b99f45db389d4eeac1b671518875eR242-R248
1 parent 204af14 commit fa8c15b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/nextjs/src/utils/instrumentServer.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,23 @@ function makeWrappedMethodForGettingParameterizedPath(
317317
origMethod: ApiPageEnsurer | PageComponentFinder,
318318
): WrappedApiPageEnsurer | WrappedPageComponentFinder {
319319
// eslint-disable-next-line @typescript-eslint/no-explicit-any
320-
const wrappedMethod = async function (this: Server, parameterizedPath: string, ...args: any[]): Promise<any> {
320+
const wrappedMethod = async function (
321+
this: Server,
322+
parameterizedPath:
323+
| string // `ensureAPIPage`, `findPageComponents` before nextjs 12.2.6
324+
| { pathname: string }, // `findPageComponents` from nextjs 12.2.6 onward
325+
...args: any[]
326+
): Promise<any> {
321327
const transaction = getActiveTransaction();
322328

323329
// replace specific URL with parameterized version
324330
if (transaction && transaction.metadata.requestPath) {
325331
const origPath = transaction.metadata.requestPath;
326-
const newName = transaction.name.replace(origPath, parameterizedPath);
327-
transaction.setName(newName, 'route');
332+
const newPath = typeof parameterizedPath === 'string' ? parameterizedPath : parameterizedPath.pathname;
333+
if (newPath) {
334+
const newName = transaction.name.replace(origPath, newPath);
335+
transaction.setName(newName, 'route');
336+
}
328337
}
329338

330339
return origMethod.call(this, parameterizedPath, ...args);

0 commit comments

Comments
 (0)