Skip to content

Commit fa65287

Browse files
perf(router): remove prebuilt preload locations (#8132)
1 parent e9f63a6 commit fa65287

7 files changed

Lines changed: 121 additions & 79 deletions

File tree

‎.changeset/calm-links-preload.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@tanstack/router-core': patch
3+
'@tanstack/solid-router': patch
4+
'@tanstack/vue-router': patch
5+
---
6+
7+
Build client preload locations on demand and remove the prebuilt-location argument used by framework links.

‎packages/router-core/src/load-client.ts‎

Lines changed: 88 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { GLOBAL_SEROVAL, GLOBAL_TSR } from './ssr/constants'
88
import type { AnySerializationAdapter } from './ssr/serializer/transformer'
99
import type { TsrSsrGlobal } from './ssr/types'
1010
import type { ParsedLocation } from './location'
11+
import type { NavigateOptions } from './link'
1112
import type { AnyRouteMatch } from './Matches'
1213
import type { NotFoundError } from './not-found'
1314
import type {
@@ -18,7 +19,9 @@ import type {
1819
RouteLoaderFn,
1920
} from './route'
2021
import type { AnyRedirect } from './redirect'
21-
import type { AnyRouter } from './router'
22+
import type { AnyRouter, RouterCore, TrailingSlashOption } from './router'
23+
import type { RoutePaths } from './routeInfo'
24+
import type { RouterHistory } from '@tanstack/history'
2225

2326
type RouteComponentType =
2427
| 'component'
@@ -2071,71 +2074,101 @@ export async function refreshClientRoute(
20712074
await loadClientRoute(router, { sync: true })
20722075
}
20732076

2074-
export async function preloadClientRoute(
2075-
router: CoordinatorRouter,
2076-
opts: any,
2077-
redirects = 0,
2078-
builtLocation?: ParsedLocation,
2077+
export async function preloadClientRoute<
2078+
TRouteTree extends AnyRoute,
2079+
TTrailingSlashOption extends TrailingSlashOption,
2080+
TDefaultStructuralSharingOption extends boolean,
2081+
TRouterHistory extends RouterHistory,
2082+
TDehydrated extends Record<string, any> = Record<string, any>,
2083+
TFrom extends RoutePaths<TRouteTree> | string = string,
2084+
TTo extends string | undefined = undefined,
2085+
TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
2086+
TMaskTo extends string = '',
2087+
>(
2088+
router: RouterCore<
2089+
TRouteTree,
2090+
TTrailingSlashOption,
2091+
TDefaultStructuralSharingOption,
2092+
TRouterHistory,
2093+
TDehydrated
2094+
>,
2095+
opts: NavigateOptions<
2096+
RouterCore<
2097+
TRouteTree,
2098+
TTrailingSlashOption,
2099+
TDefaultStructuralSharingOption,
2100+
TRouterHistory,
2101+
TDehydrated
2102+
>,
2103+
TFrom,
2104+
TTo,
2105+
TMaskFrom,
2106+
TMaskTo
2107+
>,
20792108
): Promise<Array<AnyRouteMatch> | undefined> {
20802109
if (
20812110
process.env.NODE_ENV !== 'production' &&
2082-
(router._refreshNextLoad || router._tx?.[6 /* refresh */])
2111+
((router as CoordinatorRouter)._refreshNextLoad ||
2112+
router._tx?.[6 /* refresh */])
20832113
) {
20842114
return
20852115
}
2086-
const location = builtLocation ?? router.buildLocation(opts)
2087-
const base = router._committed
2088-
const controller = new AbortController()
2089-
let matches: Array<AnyRouteMatch>
2090-
try {
2091-
matches = router.matchRoutes(location, {
2092-
_controller: controller,
2093-
})
2094-
acquireMatchResources(matches)
2095-
} catch (cause) {
2096-
controller.abort()
2097-
if (!isNotFound(cause)) {
2098-
console.error(cause)
2099-
}
2100-
return
2101-
}
2102-
;(router._preloads ??= new Map()).set(controller, matches)
2103-
let active: boolean
2104-
try {
2105-
let result: LaneResult
2116+
let location = router.buildLocation(
2117+
opts as Parameters<typeof router.buildLocation>[0],
2118+
)
2119+
for (let redirects = 0; ; redirects++) {
2120+
const base = router._committed
2121+
const controller = new AbortController()
2122+
let matches: Array<AnyRouteMatch>
21062123
try {
2107-
result = await executeClientLane(router, location, matches, [
2108-
controller,
2109-
redirects,
2110-
base,
2111-
true,
2112-
])
2113-
} finally {
2114-
active = router._preloads.delete(controller)
2115-
transferMatchResources(router, matches)
2124+
matches = router.matchRoutes(location, {
2125+
_controller: controller,
2126+
})
2127+
acquireMatchResources(matches)
2128+
} catch (cause) {
21162129
controller.abort()
2130+
if (!isNotFound(cause)) {
2131+
console.error(cause)
2132+
}
2133+
return
21172134
}
2118-
if (!isControl(result)) {
2119-
return result[1 /* matches */]
2120-
}
2121-
if (
2122-
active &&
2123-
result[0 /* kind */] === REDIRECTED &&
2124-
!result[1 /* redirect */].options.reloadDocument
2125-
) {
2126-
return preloadClientRoute(
2127-
router,
2128-
result[1 /* redirect */].options,
2129-
redirects + 1,
2130-
result[2 /* location */],
2131-
)
2132-
}
2133-
} catch (cause) {
2134-
if (!isNotFound(cause)) {
2135-
console.error(cause)
2135+
;(router._preloads ??= new Map()).set(controller, matches)
2136+
let active: boolean
2137+
try {
2138+
let result: LaneResult
2139+
try {
2140+
result = await executeClientLane(router, location, matches, [
2141+
controller,
2142+
redirects,
2143+
base,
2144+
true,
2145+
])
2146+
} finally {
2147+
active = router._preloads.delete(controller)
2148+
transferMatchResources(router, matches)
2149+
controller.abort()
2150+
}
2151+
if (!isControl(result)) {
2152+
return result[1 /* matches */]
2153+
}
2154+
// Only a materialized redirect has a third tuple item.
2155+
if (
2156+
!active ||
2157+
result.length < 3 ||
2158+
(process.env.NODE_ENV !== 'production' &&
2159+
((router as CoordinatorRouter)._refreshNextLoad ||
2160+
router._tx?.[6 /* refresh */]))
2161+
) {
2162+
return
2163+
}
2164+
location = result[2 /* location */]!
2165+
} catch (cause) {
2166+
if (!isNotFound(cause)) {
2167+
console.error(cause)
2168+
}
2169+
return
21362170
}
21372171
}
2138-
return
21392172
}
21402173

21412174
// --- SSR hydration (client entry via @tanstack/router-core/ssr/client) ---

‎packages/router-core/src/router.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ export type PreloadRouteFn<
674674
TTrailingSlashOption extends TrailingSlashOption,
675675
TDefaultStructuralSharingOption extends boolean,
676676
TRouterHistory extends RouterHistory,
677+
TDehydrated extends Record<string, any> = Record<string, any>,
677678
> = <
678679
TFrom extends RoutePaths<TRouteTree> | string = string,
679680
TTo extends string | undefined = undefined,
@@ -685,7 +686,8 @@ export type PreloadRouteFn<
685686
TRouteTree,
686687
TTrailingSlashOption,
687688
TDefaultStructuralSharingOption,
688-
TRouterHistory
689+
TRouterHistory,
690+
TDehydrated
689691
>,
690692
TFrom,
691693
TTo,
@@ -2584,9 +2586,9 @@ export class RouterCore<
25842586
TRouteTree,
25852587
TTrailingSlashOption,
25862588
TDefaultStructuralSharingOption,
2587-
TRouterHistory
2588-
> = (opts: any, builtLocation?: ParsedLocation) =>
2589-
preloadClientRoute(this, opts, 0, builtLocation)
2589+
TRouterHistory,
2590+
TDehydrated
2591+
> = (opts) => preloadClientRoute(this, opts)
25902592

25912593
matchRoute: MatchRouteFn<
25922594
TRouteTree,

‎packages/solid-router/src/link.tsx‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,12 @@ export function useLinkProps<
251251
})
252252

253253
const doPreload = () =>
254-
(
255-
router.preloadRoute as (
256-
opts: typeof options,
257-
builtLocation: ReturnType<typeof router.buildLocation>,
258-
) => ReturnType<typeof router.preloadRoute>
259-
)(options, next()).catch((err: any) => {
260-
console.warn(err)
261-
console.warn(preloadWarning)
262-
})
254+
router
255+
.preloadRoute(options as Parameters<typeof router.preloadRoute>[0])
256+
.catch((err: any) => {
257+
console.warn(err)
258+
console.warn(preloadWarning)
259+
})
263260

264261
const [ref, setRef] = Solid.createSignal<Element | null>(null)
265262

‎packages/solid-router/tests/link.test.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5273,7 +5273,10 @@ describe('Link', () => {
52735273
await waitFor(() =>
52745274
expect(preloadRouteSpy).toHaveBeenCalledTimes(baselineCalls + 3),
52755275
)
5276-
expect(updateSearch).toHaveBeenCalledTimes(baselineSearchCalls)
5276+
expect(updateSearch).toHaveBeenCalledTimes(baselineSearchCalls + 3)
5277+
for (const call of preloadRouteSpy.mock.calls.slice(baselineCalls)) {
5278+
expect(call).toHaveLength(1)
5279+
}
52775280
})
52785281

52795282
test('Router.preload="intent", pendingComponent renders during unresolved route loader', async () => {

‎packages/vue-router/src/link.tsx‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,12 @@ function useLinkPropsImpl(
231231

232232
const doPreload = () => {
233233
const options = getOptions()
234-
return (
235-
router.preloadRoute as (
236-
opts: typeof options,
237-
builtLocation: ReturnType<typeof router.buildLocation>,
238-
) => ReturnType<typeof router.preloadRoute>
239-
)(options, next.value).catch((err: any) => {
240-
console.warn(err)
241-
console.warn(preloadWarning)
242-
})
234+
return router
235+
.preloadRoute(options as Parameters<typeof router.preloadRoute>[0])
236+
.catch((err: any) => {
237+
console.warn(err)
238+
console.warn(preloadWarning)
239+
})
243240
}
244241

245242
let pendingPreload: 'intent' | 'viewport' | undefined

‎packages/vue-router/tests/link.test.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5713,7 +5713,10 @@ describe('Link', () => {
57135713
await waitFor(() =>
57145714
expect(preloadRouteSpy).toHaveBeenCalledTimes(baselineCalls + 3),
57155715
)
5716-
expect(updateSearch).toHaveBeenCalledTimes(baselineSearchCalls)
5716+
expect(updateSearch).toHaveBeenCalledTimes(baselineSearchCalls + 3)
5717+
for (const call of preloadRouteSpy.mock.calls.slice(baselineCalls)) {
5718+
expect(call).toHaveLength(1)
5719+
}
57175720
})
57185721

57195722
test('Router.preload="intent", pendingComponent renders during unresolved route loader', async () => {

0 commit comments

Comments
 (0)