perf: geting lazy?, doing less work? generate more (plus some recommendations) #1564
Mergedchorobin(GitHub profile, opens in a new tab)merged 12 commits into
main fromrefs/pull/1564/headmerged by chorobin(GitHub profile, opens in a new tab) - packages/router-generator/tests/generator/nested-layouts/routeTree.expected.ts line 144@@ -138 +144 @@138144 declare module '@tanstack/react-router' {
- Resolvedby chorobin(GitHub profile, opens in a new tab)@@ -583,4 +583,6 @@583583 interface FileRoutesByPath {584584 ${routeNodes585585 .map((routeNode) => {586- return `'${removeTrailingUnderscores(routeNode.routePath)}': {586+ const filePathId = removeTrailingUnderscores(routeNode.routePath)587+ const id = filePathId588+ const fullPath = removeGroups(
- packages/router-generator/src/generator.ts line 593@@ -583,4 +583,11 @@583583 interface FileRoutesByPath {584584 ${routeNodes585585 .map((routeNode) => {586- return `'${removeTrailingUnderscores(routeNode.routePath)}': {586+ const filePathId = removeTrailingUnderscores(routeNode.routePath)587+ const id = filePathId588+ const fullPath = removeGroups(589+ removeUnderscores(removeLayoutSegments(routeNode.routePath)) ?? '',590+ )591+592+ return `'${filePathId}': {593+ id: '${id}'
- packages/react-router/src/fileRoute.ts line 89@@ -150,20 +83,7 @@15083 TFullSearchSchemaInput = ResolveFullSearchSchemaInput<15184 TParentRoute,15285 TSearchSchemaUsed15386 >,15487 TFullSearchSchema = ResolveFullSearchSchema<TParentRoute, TSearchSchema>,15588 TParams = Record<ParsePathParams<TPath>, string>,156- TAllParams = MergeFromFromParent<157- TParentRoute['types']['allParams'],158- TParams159- >,160- TRouteContextReturn extends161- RouteConstraints['TRouteContext'] = RouteContext,162- TRouteContext = [TRouteContextReturn] extends [never]163- ? RouteContext164- : TRouteContextReturn,165- TAllContext = Assign<166- IsAny<TParentRoute['types']['allContext'], {}>,167- TRouteContext168- >,169- TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,89+ TAllParams = ResolveAllParamsFromParent<TParentRoute, TParams>,
SeanCassiere reviewed
packages/react-router/src/link.tsx line 61@@ -56,6 +58,4 @@5658 : never5759 : never586059-export type ParsePathParams<T extends string> = keyof {60- [K in Trim<Split<T>[number], '_'> as K extends `$${infer L}`61- ? L extends ''61+export type ParsePathParams<examples/react/basic-file-based/src/routeTree.gen.ts line 73@@ -73 +73 @@7373 declare module '@tanstack/react-router' {docs/framework/react/guide/type-safety.md line 144Outdated@@ -112,0 +124,21 @@124+<Link to="." search={{ page: 0 }} />125+<Link search={{ page: 0 }} />126+```127+128+**These examples are bad for TS performance**. Thats because `search` resolves to a union of all `search` params for all routes and TS has to check whatever you pass to the `search` prop against this potentially big union. As your application grows, this check time will increase linearly to number of routes and search params. We have done our best to optimise for this case (typescript will typically do this work once and cache it) but the initial check against this large union is expensive. This also applies to `params` and other API's such as `useSearch`, `useParams`, `useNavigate` etc129+130+Instead you should try to narrow to relevant routes with `from` or `to`131+132+```tsx133+<Link from={Route.fullPath} to=".." search={{page: 0}} />134+<Link from="/posts" to=".." search={{page: 0}} />135+```136+137+Remember you can always pass a union to `to` or `from` to narrow the routes you're interested in. You can also pass branches to `from` to be more loose.138+139+```tsx140+const from: '/posts/$postId/deep' | '/posts/' = '/posts/'141+<Link from={from} to='..' />142+```143+144+### Consider using the object syntax of `createChildren`- docs/framework/react/guide/type-safety.md line 116Outdated@@ -110,3 +110,7 @@110110 },111111 })112112 ```113+114+## Performance Recommendations115+116+As your application scales, typescript check times will naturally increase. There are a few things to keep in mind when your application scales to keep your TS check times down.
- docs/framework/react/guide/type-safety.md line 128Outdated@@ -110,3 +110,19 @@110110 },111111 })112112 ```113+114+## Performance Recommendations115+116+As your application scales, typescript check times will naturally increase. There are a few things to keep in mind when your application scales to keep your TS check times down.117+118+### Narrow to relevant routes as much as you possibly can119+120+Consider the following usage of `Link`121+122+```tsx123+<Link to=".." search={{ page: 0 }} />124+<Link to="." search={{ page: 0 }} />125+<Link search={{ page: 0 }} />126+```127+128+**These examples are bad for TS performance**. Thats because `search` resolves to a union of all `search` params for all routes and TS has to check whatever you pass to the `search` prop against this potentially big union. As your application grows, this check time will increase linearly to number of routes and search params. We have done our best to optimise for this case (typescript will typically do this work once and cache it) but the initial check against this large union is expensive. This also applies to `params` and other API's such as `useSearch`, `useParams`, `useNavigate` etc
- @@ -112,0 +117,21 @@117+118+### Narrow to relevant routes as much as you possibly can119+120+Consider the following usage of `Link`121+122+```tsx123+<Link to=".." search={{ page: 0 }} />124+<Link to="." search={{ page: 0 }} />125+<Link search={{ page: 0 }} />126+```127+128+**These examples are bad for TS performance**. Thats because `search` resolves to a union of all `search` params for all routes and TS has to check whatever you pass to the `search` prop against this potentially big union. As your application grows, this check time will increase linearly to number of routes and search params. We have done our best to optimise for this case (typescript will typically do this work once and cache it) but the initial check against this large union is expensive. This also applies to `params` and other API's such as `useSearch`, `useParams`, `useNavigate` etc129+130+Instead you should try to narrow to relevant routes with `from` or `to`131+132+```tsx133+<Link from={Route.fullPath} to=".." search={{page: 0}} />134+<Link from="/posts" to=".." search={{page: 0}} />135+```136+137+Remember you can always pass a union to `to` or `from` to narrow the routes you're interested in. You can also pass branches to `from` to be more loose.
- @@ -112,0 +141,21 @@141+<Link from={from} to='..' />142+```143+144+### Consider using the object syntax of `createChildren`145+146+It's tyipcal of routes to have `params` `search`, `loaders` or `context` that can even reference external dependencies which are also heavy on TS inference. For such applications, using objects for creating the route tree can be more performant than tuples.147+148+`createChildren` also can accept an object. For large route trees with complex routes and external libraries, objects can be much faster for TS to type check as opposed to large tuples. The performance gains depend on your project, what external dependencies you have and how the types for those libraries are written149+150+```tsx151+const routeTree = rootRoute.addChildren({152+ postsRoute: postsRoute.addChildren({ postRoute, postsIndexRoute }),153+ indexRoute,154+})155+```156+157+Note this syntax is more verbose but has better TS performance. With file based routing, the route tree is generated for you so a verbose route tree is not a concern158+159+### Avoid internal types without narrowing160+161+Its common you might want to re-use types exposed. For example you might be tempted to use `LinkProps` like so
- packages/react-router/src/index.tsx line 67Outdated@@ -71,3 +64,4 @@7164 type UseLinkPropsOptions,7265 type ActiveLinkOptions,7366 type LinkProps,67+ type LinkComponentProps,
- packages/react-router/src/link.tsx line 196@@ -184,4 +191,6 @@184191 TTo extends string = '',185192 TMaskFrom extends RoutePaths<TRouter['routeTree']> | string = TFrom,186193 TMaskTo extends string = '',187-> = ToSubOptions<TRouter, TFrom, TTo> & {194+> = ToSubOptions<TRouter, TFrom, TTo> & MaskOptions<TRouter, TMaskFrom, TMaskTo>195+196+export interface MaskOptions<
chorobin(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
☁️ Nx Cloud Report#
CI is running/has finished running commands for commit 236d63c417f90fa44bf95e3fc16bd727c9634286. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.
📂 See all runs for this CI Pipeline Execution
✅ Successfully ran 2 targets
nx affected --targets=test:format,test:eslint,test:unit,test:build,build --parallel=3nx affected --target=test:types --exclude=examples/**Sent with 💌 from NxCloud.