-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
fix(gatsby): Wrong route resolved by findPageByPath function #34070
Conversation
This makes sense! Could you add some new tests that show how this fixed routing for the scenarios you described that don't currently work? |
Hey, thanks for the reply @KyleAMathews. I wasn't really sure where to add the tests so I added them on
On cypress, I added tests that open the following path and check if they resolve to the right file:
Finally, I checked that the new version works correctly, whereas the master version fails on these tests. Let me know if you think on different solutions or different places to put those tests. Thanks! |
Hi @pieh, any news about this one? Let me know if you need any changes, Thanks! |
Just to add some color to this PR, this unlocks large enterprise clients (think very large multi-country enterprises) for VTEX. Our team bet on Gatsby and we're super happy with it. This PR would allow the team to move forward with migrating our clients to Gatsby V4, thus further improving their experience. Is there anything we can do to help with the reviewing process? We're here to help! Thanks for all your hard work and for providing such an incredible tool. We're very excited and confident in our bet in Gatsby. |
This looks correct, I will just expand our existing unit tests as well. While I will very likely merge as-is. This will need some iteration in the future to make it more performant and just consume less memory (memory usage note is actually more about what we already have than changes in this PR). When |
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.
Thank you @tlgimenes for implementing the fix and adding E2E test cases to make sure it works and there will be no regressions in the future!
This change was just released in |
* master: (125 commits) chore(release): Publish next fix(gatsby): createNode return promise (gatsbyjs#34399) chore(release): Publish next fix(gatsby): Wrong route resolved by findPageByPath function (gatsbyjs#34070) fix(deps): update typescript to v5 (major) (gatsbyjs#33786) chore(docs): Update processing external images guide (gatsbyjs#34388) chore(deps): update dependency aws-sdk to ^2.1048.0 (gatsbyjs#34365) chore(deps): update dependency autoprefixer to ^10.4.1 for gatsby-plugin-sass (gatsbyjs#34357) chore(deps): update formatting & linting (gatsbyjs#34370) fix(deps): update minor and patch dependencies for gatsby-source-drupal (gatsbyjs#34375) fix(deps): update dependency eslint-plugin-react to ^7.28.0 (gatsbyjs#34372) fix(deps): update dependency resolve-url-loader to ^3.1.4 for gatsby-plugin-sass (gatsbyjs#34361) chore(deps): update dependency typescript to ^4.5.4 (gatsbyjs#34358) chore(docs): Fix links to shared layout component (gatsbyjs#34330) chore: Fix typo (gatsbyjs#34349) chore(examples): use mobx v6 in using-mobx example (gatsbyjs#34351) chore(deps): update dependency rewire to v6 for gatsby-plugin-offline (gatsbyjs#34376) chore(deps): update dependency msw to ^0.36.3 for gatsby-core-utils (gatsbyjs#34367) chore(deps): update dependency msw to ^0.36.3 for gatsby-plugin-gatsby-cloud (gatsbyjs#34368) fix(deps): update dependency graphql to ^15.8.0 for gatsby-codemods (gatsbyjs#34373) ...
Description
This PR uses reach router's best matching route algorithm to resolve pages in
findPageByPath
function so we resolve to the best matching route instead of the first match.Motivation
I'm playing around with Gatsby v4 and the File System Route API I ended up writing a project with the following pages directory:
Both
[...].tsx
and[slug]/p/index.tsx
pages use SSR rendering mode.My intended behavior with this routing layout is for routes like
/banana/p
and/apple/p
to be resolved to the[slug]/p/index.tsx
page and routes like/ananas
or/ananas/yellow
to be resolved to the[...].tsx
page.I noticed that when running
gatsby build && gatsby serve
, the project worked as intended, but when runninggatsby develop
or when deploying it on both Netlify and Gatsby Cloud, my/banana/p
and/apple/p
where being resolved to the[...].tsx
page.Looking on both Netlify and Gatsby implemetations, I've noticed that, for resolving pages, they use this
findPageByPath
function. This function was not respecting reach router's best matching algorithm which led to the wrong page being resolved to.This PR fixes this problem by using reach router's
pick
function. A drawback of this solution is that it may lead to some performance regressions since we parse and pick the best route for each function call. However, I believe the impact should be minimal since usually the number of routes to match should not be huge (~10 routes).I'm open for other solutions and I hope this PR serves as a guide to fix this problem.
Thanks!