-
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): don't force leading slash for external paths in routes manifest #38639
fix(gatsby): don't force leading slash for external paths in routes manifest #38639
Conversation
Awesome! Can we add a unit test for this code @jenae-janzen? The other unit tests are here: cc @pieh for any guidance if needed. |
Some tests are quite flaky :( - the failing test suites are not even using/testing any of code changes (I did just restart them)
gatsby/packages/gatsby/src/utils/adapter/__tests__/fixtures/state.ts Lines 67 to 73 in 3af35ae
fromPath and then assertions/ tests done in https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/utils/adapter/__tests__/manager.ts#L47
|
@@ -276,7 +276,8 @@ function getRoutesManifest(): RoutesManifest { | |||
|
|||
// TODO: This could be a "addSortedRoute" function that would add route to the list in sorted order. TBD if necessary performance-wise | |||
function addRoute(route: Route): void { | |||
if (!route.path.startsWith(`/`)) { | |||
const externalPathsRegex = new RegExp(`^https?://`, `i`) |
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.
Any performance concerns using a regex here, i.e. if a site has a lot of redirects?
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.
doing route.path.startsWith('http://') || route.path.startsWith('https://')
is faster than using that exact regex (tho doesn't cover case insensitivity) - so I'll adjust to use this.
"RegExp x 1,614,517 ops/sec ±0.62% (96 runs sampled)"
"startWith x 5,467,939 ops/sec ±0.17% (96 runs sampled)"
"Fastest is startWith"
Note here that it seems like initiating new regexp on each addRoute
call is siginificant portion of overhead it seems - after moving regexp to be inited once earlier we get
"RegExp x 2,410,360 ops/sec ±0.99% (95 runs sampled)"
"startWith x 5,037,209 ops/sec ±3.31% (93 runs sampled)"
"Fastest is startWith"
so 2 startWith
calls is still faster then regexp test (the startWith
benchmark case was using 2 calls, not 1, so this will be ~twice as fast)
I'll adjust to use startWith
and get PR merged
…anifest (#38639) (#38646) * don't force leading slash for external redirects * add test * perf: regexp -> double String.startWith checks --------- Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com> (cherry picked from commit 5dbcf9e) Co-authored-by: Jenae Janzen <101715009+jenae-janzen@users.noreply.github.com>
Released in |
Fixes: https://linear.app/netlify/issue/FRA-20/external-redirects-have-a-forced-slash-added
If
route.path
begins withhttp(s)://
, we shouldn't add a leading slash to it.Description
Documentation
Tests
Related Issues
Fixes FRA-20