Repository navigation
Description
Which project does this relate to?
Router
Describe the bug
After a route (it needs to have a lazy route) /_protected/test got preloaded, then we call router.buildRouteTree() to rebuild the route tree again, that route will have a wrong id /_protected/_protected/test.
Your Example Website or App
Steps to Reproduce the Bug or Issue
- Open my StackBlitz example.
- It should automatically run the project. If not, just
npm run start - Open browser devtool's console.
- Click the first button to print
router.routesById, in the console you can confirm all routes' ids are fine. - Click the second button, it preloads the
/_protected/testroute. - Click the third button to run
router.buildRouteTree() - Click the fourth button to print
router.routesByIdagain, in the console you can see the preloaded route/testnow has a wrong id:/_protected/_protected/test
Expected behavior
No matter how many times I rebuild the route tree, the result should be consistent.
I'm doing dynamic route injection, so I need to rebuild the route tree after there is a new route got injected.
Screenshots or Videos
duplicated_id_prefix.mp4
Platform
- OS: [Windows]
- Browser: [Chrome]
- Version: [1.58.3]
Additional context
How the issue happened
- This is currently how we generate id for a route instance in its
init()method:
const customId = options?.id || path // Uses path as id's fallback
// Strip the parentId prefix from the first level of children
let id = isRoot
? rootRouteId
: joinPaths([
this.parentRoute.id === rootRouteId ? '' : this.parentRoute.id,
customId,
])-
During the first initialization of the router, everything is fine because it fall back to the path, and the path doesn't contain a prefix from parent.
-
When we preload the route, the lazy route's options will be assign to the primary route object, so that the primary route object now has an
idin itsoptionsproperty. Related code: https://github.com/TanStack/router/blob/main/packages/react-router/src/router.ts#L2247 -
Now we call
router.buildRouteTree()to manually rebuild it, now thecustomIdwill be/_protected/test, and we are joining it with parent's id again, so we get/_protected/_protected/test.
I believe we should trim the parant prefix when using options.id as customId
Activity
fix(react-router): do not copy `id` from lazy route over to non-lazy …
schiller-manuel commentedon Oct 7, 2024
I finally got around having a closer look.
I have implemented a simpler fix in #2490 which solves the root cause by not copying over the
idfrom lazy route.Here is your example with the package built for this PR:
https://stackblitz.com/edit/vitejs-vite-uesisd
Please have a look and let me know if it works for you.
stevenlyd commentedon Oct 8, 2024
Thank you! Yah that's a much simpler solution. Do we still need those test cases for route id consistency/uniqueness in my PR? If we do need them I'll remove other changes otherwise we can just close it.
schiller-manuel commentedon Oct 8, 2024
those test cases are still valuable
so please extract those
stevenlyd commentedon Oct 8, 2024
No problem, I'll do it later today.
fix(react-router): do not copy `id` from lazy route over to non-lazy …