Skip to content

Commit 9056a70

Browse files
authored
Merge branch 'main' into fix/issue-8115-context-publication
2 parents f03f4e8 + fa65287 commit 9056a70

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 {
@@ -17,7 +18,9 @@ import type {
1718
RouteLoaderFn,
1819
} from './route'
1920
import type { AnyRedirect } from './redirect'
20-
import type { AnyRouter } from './router'
21+
import type { AnyRouter, RouterCore, TrailingSlashOption } from './router'
22+
import type { RoutePaths } from './routeInfo'
23+
import type { RouterHistory } from '@tanstack/history'
2124

2225
type RouteComponentType =
2326
| 'component'
@@ -2061,71 +2064,101 @@ export async function refreshClientRoute(
20612064
await loadClientRoute(router, { sync: true })
20622065
}
20632066

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

21312164
// --- 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)