-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(react): Fix React Router v6 paramaterization #5515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Make sure paramaterization occurs in scenarios where the full path is not avaliable after all the branches are walked. In those scenarios, we have to construct the paramaterized name based on the previous branches that were analyzed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! One optional suggestion.
Btw: Does RR6 offer regex or route array support, similar to Express? Probably not but I thought I'd ask since I came across some edge cases there.
// eslint-disable-next-line @typescript-eslint/prefer-for-of | ||
for (let x = 0; x < branches.length; x++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of curiosity: why the index for loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to stay consistent with RR5 integration. Not sure if there's any specific reason there though.
sentry-javascript/packages/react/src/reactrouter.tsx
Lines 79 to 84 in 9b7131f
// eslint-disable-next-line @typescript-eslint/prefer-for-of | |
for (let x = 0; x < branches.length; x++) { | |
if (branches[x].match.isExact) { | |
return [branches[x].match.path, 'route']; | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll just keep the explicit for loop, though technically it is extra bytes
packages/react/src/reactrouterv6.tsx
Outdated
// If the route defined on the element is something like | ||
// <Route path="/stores/:storeId/products/:productId" element={<div>Product</div>} /> | ||
// We should check against the branch.pathname for the number of / seperators | ||
if (pathBuilder.split('/').length !== branch.pathname.split('/').length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a safe check? Just asking because I used URL segment counting in my Express router instrumentation and I had to handle a few edge cases. For example, could there be problems with routes starting or ending with a /
while others don't? (which would influence the length because of empty array items after the .split operation).
Just a suggestion but now that I'm thinking about it, we could extract this function
sentry-javascript/packages/tracing/src/integrations/node/express.ts
Lines 387 to 394 in 6ea53ed
/** | |
* Returns number of URL segments of a passed URL. | |
* Also handles URLs of type RegExp | |
*/ | |
function getNumberOfUrlSegments(url: string): number { | |
// split at '/' or at '\/' to split regex urls correctly | |
return url.split(/\\?\//).filter(s => s.length > 0 && s !== ',').length; | |
} |
to our utils package and use it here. But totally optional ofc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point! I'm going to extract out getNumberOfUrlSegments
and use it instead.
Previous versions did offer regex/array support - this one no longer does, so I think we are fine here. Edit: https://reactrouter.com/docs/en/v6/getting-started/faq#what-happened-to-regexp-routes-paths |
return {}; | ||
} | ||
|
||
const match = url.match(/^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/); |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data
size-limit report 📦
|
Fixes #5513
Make sure paramaterization occurs in scenarios where the full path is not available after all the branches are walked. In those scenarios, we have to construct the paramaterized name based on the previous branches that were analyzed.
This patch also creates a new file in
@sentry/utils
-url.ts
, where we store some url / string related utils. This was made so that the react package would get access to thegetNumberOfUrlSegments
util.