Skip to content

fix(nextjs): parses the nextjs router correctly #12321

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

Closed
wants to merge 11 commits into from

Conversation

dohooo
Copy link

@dohooo dohooo commented Jun 1, 2024

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

Fixes #12256

Background

Description

In case to increase the complexity of the original regex. I've added a new parser to handle the specific scenario of Next.js routes.

The core concept is to replace the key characters of Nextj.js routes as encoded characters. For example:

.../app/[locale]/sentry-example-test/(default)/sub/page-xxxxxxx.js:11:11
to 
.../app/%5Blocale%5D/sentry-example-test/(default)/sub/page-xxxxxxx.js:11:11

Why did I choose this way?

Because the original parser always split this stack line to

{
  ...,
  function_name: ".../app/[locale]/sentry-example-test/",
  filename: "default)/sub/page-xxxxxxx.js"
}

But the correct results should be

{
  ...,
  function_name: undefined,
  filename: ".../app/[locale]/sentry-example-test/(default)/sub/page-xxxxxxx.js"
}

And I found the original parser is too complex to extend. So I create a new parser to handle this scenario and made it more proprietary and explicitly used for nextjs routing.

Did I test it?

Yes, I've tested it with the following cases:

  1. This logic will affect the most of the users using nextjs and sentry. So I've modified the original tests to integrate the nextjs parser to ensure that everything works well as before, even though user aren't using nextjs.
  2. I've added a new test to handle specify cases from nextjs.

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

@dohooo dohooo requested a review from AbhiPrasad June 1, 2024 22:57
@dohooo
Copy link
Author

dohooo commented Jun 4, 2024

Can I get a review for this PR?

@AbhiPrasad
Copy link
Member

the stacktrace parsing seems reasonable to me, but I'd like to get @lforst opinion as our resident Next.js expert.

@lforst
Copy link
Contributor

lforst commented Jun 5, 2024

Thanks for the PR, however, I think this is a bit of a bandaid solution and we should instead fix the parsing logic in the chromium stack trace parser.

Started working on fixing the regex instead: #12373

@dohooo
Copy link
Author

dohooo commented Jun 5, 2024

Thanks for the PR, however, I think this is a bit of a bandaid solution and we should instead fix the parsing logic in the chromium stack trace parser.

Started working on fixing the regex instead: #12373

If u want to update the chromium stack trace parser means that if Nextjs update their routes rules, u should to update this complexity regex. I think this isn't a good solution, because not all frameworks will have logic similar to Nextjs. Why should we increase this complexity to the global level and use it to handle all situations? Splitting it is the right way to reduce complexity. Otherwise, these parses should be refactored asap.

@dohooo
Copy link
Author

dohooo commented Jun 5, 2024

If u want to handle more in the top-level parser. U also need to pay attention about the expo-router. I believe there are some similar framework custom routes, and it may be a better solution to spread them into different packages for processing. hope you can consider

@lforst
Copy link
Contributor

lforst commented Jun 5, 2024

I think this isn't a good solution, because not all frameworks will have logic similar to Nextjs.

The reason this bug exists is because our chromium stack trace parser has a bug for frames that are in the global module scope and contain parentheses. See this (currently) failing test case. It absolutely makes sense to fix this in the core module.

I fully agree with you that the regex is too complex and we should replace it with a state machine parser. That thing is 4+ years old.

@dohooo
Copy link
Author

dohooo commented Jun 5, 2024

It absolutely makes sense to fix this in the core module

Ah understand, thx for your explanation.

@dohooo
Copy link
Author

dohooo commented Jun 5, 2024

Is this PR still needed?

@lforst
Copy link
Contributor

lforst commented Jun 5, 2024

Is this PR still needed?

No I think we fixed it in #12373. Thank you for the effort though! Much appreciated!

@dohooo dohooo closed this Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing debug_id for stacktrace frame happened in event handler (onClick)
3 participants