-
-
Notifications
You must be signed in to change notification settings - Fork 649
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
Add test cases for route id consistency #2401
Merged
schiller-manuel
merged 14 commits into
TanStack:main
from
stevenlyd:fix-route-id-fallback
Oct 15, 2024
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d31f452
tweaked the fallback order
2e8df8e
added getLastPathSegment util function
d047bb9
removed unused import
75dd866
added test case for getLastPathSegment()
0e4b005
trim parent id prefix when creating lazy routes
b6b8229
added test cases
9712655
removed unused import
4c3148a
made test case more strict
ca44a1b
use it.each
bbaf3d6
Added test for route id uniqueness
931f127
added test cases to check if the option.id for lazyRoute is trimmed
8265151
revert changes, keep only the tests
1c587cd
Merge branch 'main' into fix-route-id-fallback
b718c22
removed unused import
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createLazyFileRoute, createLazyRoute } from '../../src' | ||
|
||
export function Route(id: string) { | ||
return createLazyRoute(id)({ | ||
component: () => <h1>I'm a normal route</h1>, | ||
}) | ||
} | ||
|
||
export function FileRoute(id: string) { | ||
return createLazyFileRoute(id as any)({ | ||
component: () => <h1>I'm a normal file route</h1>, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 am wondering about why the last path segment should be used as
id
.this might work, but aren't there cases where this is not unique?
e.g. consider the following routes:
both would end up with the same
id
, right?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.
Yes, you're right that both
/foo/bar
and/hello/bar
have the same last path segment bar. However, this only affects theoptions.id
of the lazy route object. From what I understand, theoptions.id
in the lazy route object is mainly used during the preloading of a route. When a route is preloaded, these options are assigned back to the primary route object like this:I believe that the
options.id
should represent the route's own ID, not the full ID that includes prefixes from parent routes. This seems to be consistent with how IDs and paths got updated for primary route objects inrouteTree.gen.ts
. For example:I guess the reason why we assign the full ID at the beginning but update it to a trimmed version later is that the code generator needs to consume that full path or full ID information when auto-generating the route tree.
If I am not mistaken, it's precisely because the
id
andpath
in primary route objects are updated to their trimmed versions inrouteTree.gen.ts
that TSR can function properly. Otherwise if they are in full version, theroute.id
we generate initially would be incorrect, containing duplicate parent prefixes. However, we currently don't trim theoptions.id
for the lazy route object, which can cause issues when it gets assigned back to the primary route object.Of course, these incorrect
options.id
values only take effect when we runrouter.buildRouteTree
again. Since few people rerunrouter.buildRouteTree
after the router is instantiated, this issue hasn't been discovered until now.From my understanding,
route.options.id
represents the route's own ID, whereasroute.id
is the full ID that exists only in the primary route object and is generated whenroute.init
is called. This process combines thecustomId
with the parent's full ID as a prefix. Additionally, thecustomId
is derived from either thepath
(notfullPath
) oroptions.id
, depending on whether the route is pathless.As a result, the route's ID ultimately become
/foo/bar
and/hello/bar
, maintaining uniqueness even if the last segments are the same.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.
can you add a test for this as well please?
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.
Sure, you mean a test that checks if the lazy route object getting the trimmed version of id?
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.
and that IDs are still unique then
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've added tests for route ID uniqueness, and also added routes ending with same last segments to that test router.
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.
And also added tests that check if the lazy route's
options.id
is trimmed.