-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathMatch.tsx
More file actions
555 lines (488 loc) · 17 KB
/
Match.tsx
File metadata and controls
555 lines (488 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import * as React from 'react'
import { useStore } from '@tanstack/react-store'
import {
createControlledPromise,
getLocationChangeInfo,
invariant,
isNotFound,
isRedirect,
rootRouteId,
} from '@tanstack/router-core'
import { isServer } from '@tanstack/router-core/isServer'
import { CatchBoundary, ErrorComponent } from './CatchBoundary'
import { useRouter } from './useRouter'
import { CatchNotFound } from './not-found'
import { matchContext } from './matchContext'
import { SafeFragment } from './SafeFragment'
import { renderRouteNotFound } from './renderRouteNotFound'
import { ScrollRestoration } from './scroll-restoration'
import { ClientOnly } from './ClientOnly'
import type {
AnyRoute,
ParsedLocation,
RootRouteOptions,
} from '@tanstack/router-core'
export const Match = React.memo(function MatchImpl({
matchId,
}: {
matchId: string
}) {
const router = useRouter()
if (isServer ?? router.isServer) {
const match = router.stores.activeMatchStoresById.get(matchId)?.state
if (!match) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`Invariant failed: Could not find match for matchId "${matchId}". Please file an issue!`,
)
}
invariant()
}
const routeId = match.routeId as string
const parentRouteId = (router.routesById[routeId] as AnyRoute).parentRoute
?.id
return (
<MatchView
router={router}
matchId={matchId}
resetKey={router.stores.loadedAt.state}
matchState={{
routeId,
ssr: match.ssr,
_displayPending: match._displayPending,
parentRouteId,
}}
/>
)
}
// Subscribe directly to the match store from the pool.
// The matchId prop is stable for this component's lifetime (set by Outlet),
// and reconcileMatchPool reuses stores for the same matchId.
const matchStore = router.stores.activeMatchStoresById.get(matchId)
if (!matchStore) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`Invariant failed: Could not find match for matchId "${matchId}". Please file an issue!`,
)
}
invariant()
}
// eslint-disable-next-line react-hooks/rules-of-hooks
const resetKey = useStore(router.stores.loadedAt, (loadedAt) => loadedAt)
// eslint-disable-next-line react-hooks/rules-of-hooks
const match = useStore(matchStore, (value) => value)
// eslint-disable-next-line react-hooks/rules-of-hooks
const matchState = React.useMemo(() => {
const routeId = match.routeId as string
const parentRouteId = (router.routesById[routeId] as AnyRoute).parentRoute
?.id
return {
routeId,
ssr: match.ssr,
_displayPending: match._displayPending,
parentRouteId: parentRouteId as string | undefined,
} satisfies MatchViewState
}, [match._displayPending, match.routeId, match.ssr, router.routesById])
return (
<MatchView
router={router}
matchId={matchId}
resetKey={resetKey}
matchState={matchState}
/>
)
})
type MatchViewState = {
routeId: string
ssr: boolean | 'data-only' | undefined
_displayPending: boolean | undefined
parentRouteId: string | undefined
}
function MatchView({
router,
matchId,
resetKey,
matchState,
}: {
router: ReturnType<typeof useRouter>
matchId: string
resetKey: number
matchState: MatchViewState
}) {
const route: AnyRoute = router.routesById[matchState.routeId]
const PendingComponent =
route.options.pendingComponent ?? router.options.defaultPendingComponent
const pendingElement = PendingComponent ? <PendingComponent /> : null
const routeErrorComponent =
route.options.errorComponent ?? router.options.defaultErrorComponent
const routeOnCatch = route.options.onCatch ?? router.options.defaultOnCatch
const routeNotFoundComponent = route.isRoot
? // If it's the root route, use the globalNotFound option, with fallback to the notFoundRoute's component
(route.options.notFoundComponent ??
router.options.notFoundRoute?.options.component)
: route.options.notFoundComponent
const resolvedNoSsr =
matchState.ssr === false || matchState.ssr === 'data-only'
const ResolvedSuspenseBoundary =
// If we're on the root route, allow forcefully wrapping in suspense
(!route.isRoot || route.options.wrapInSuspense || resolvedNoSsr) &&
(route.options.wrapInSuspense ??
PendingComponent ??
((route.options.errorComponent as any)?.preload || resolvedNoSsr))
? React.Suspense
: SafeFragment
const ResolvedCatchBoundary = routeErrorComponent
? CatchBoundary
: SafeFragment
const ResolvedNotFoundBoundary = routeNotFoundComponent
? CatchNotFound
: SafeFragment
const ShellComponent = route.isRoot
? ((route.options as RootRouteOptions).shellComponent ?? SafeFragment)
: SafeFragment
return (
<ShellComponent>
<matchContext.Provider value={matchId}>
<ResolvedSuspenseBoundary fallback={pendingElement}>
<ResolvedCatchBoundary
getResetKey={() => resetKey}
errorComponent={routeErrorComponent || ErrorComponent}
onCatch={(error, errorInfo) => {
// Forward not found errors (we don't want to show the error component for these)
if (isNotFound(error)) throw error
if (process.env.NODE_ENV !== 'production') {
console.warn(`Warning: Error in route match: ${matchId}`)
}
routeOnCatch?.(error, errorInfo)
}}
>
<ResolvedNotFoundBoundary
fallback={(error) => {
// If the current not found handler doesn't exist or it has a
// route ID which doesn't match the current route, rethrow the error
if (
!routeNotFoundComponent ||
(error.routeId && error.routeId !== matchState.routeId) ||
(!error.routeId && !route.isRoot)
)
throw error
return React.createElement(routeNotFoundComponent, error as any)
}}
>
{resolvedNoSsr || matchState._displayPending ? (
<ClientOnly fallback={pendingElement}>
<MatchInner matchId={matchId} />
</ClientOnly>
) : (
<MatchInner matchId={matchId} />
)}
</ResolvedNotFoundBoundary>
</ResolvedCatchBoundary>
</ResolvedSuspenseBoundary>
</matchContext.Provider>
{matchState.parentRouteId === rootRouteId ? (
<>
<OnRendered />
{router.options.scrollRestoration && (isServer ?? router.isServer) ? (
<ScrollRestoration />
) : null}
</>
) : null}
</ShellComponent>
)
}
// On Rendered can't happen above the root layout because it actually
// renders a dummy dom element to track the rendered state of the app.
// We render a script tag with a key that changes based on the current
// location state.__TSR_key. Also, because it's below the root layout, it
// allows us to fire onRendered events even after a hydration mismatch
// error that occurred above the root layout (like bad head/link tags,
// which is common).
function OnRendered() {
const router = useRouter()
const prevLocationRef = React.useRef<undefined | ParsedLocation<{}>>(
undefined,
)
return (
<script
key={router.latestLocation.state.__TSR_key}
suppressHydrationWarning
ref={(el) => {
if (
el &&
(prevLocationRef.current === undefined ||
prevLocationRef.current.href !== router.latestLocation.href)
) {
router.emit({
type: 'onRendered',
...getLocationChangeInfo(
router.stores.location.state,
router.stores.resolvedLocation.state,
),
})
prevLocationRef.current = router.latestLocation
}
}}
/>
)
}
export const MatchInner = React.memo(function MatchInnerImpl({
matchId,
}: {
matchId: string
}): any {
const router = useRouter()
if (isServer ?? router.isServer) {
const match = router.stores.activeMatchStoresById.get(matchId)?.state
if (!match) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`Invariant failed: Could not find match for matchId "${matchId}". Please file an issue!`,
)
}
invariant()
}
const routeId = match.routeId as string
const route = router.routesById[routeId] as AnyRoute
const remountFn =
(router.routesById[routeId] as AnyRoute).options.remountDeps ??
router.options.defaultRemountDeps
const remountDeps = remountFn?.({
routeId,
loaderDeps: match.loaderDeps,
params: match._strictParams,
search: match._strictSearch,
})
const key = remountDeps ? JSON.stringify(remountDeps) : undefined
const Comp = route.options.component ?? router.options.defaultComponent
const out = Comp ? <Comp key={key} /> : <Outlet />
if (match._displayPending) {
throw router.getMatch(match.id)?._nonReactive.displayPendingPromise
}
if (match._forcePending) {
throw router.getMatch(match.id)?._nonReactive.minPendingPromise
}
if (match.status === 'pending') {
throw router.getMatch(match.id)?._nonReactive.loadPromise
}
if (match.status === 'notFound') {
if (!isNotFound(match.error)) {
if (process.env.NODE_ENV !== 'production') {
throw new Error('Invariant failed: Expected a notFound error')
}
invariant()
}
return renderRouteNotFound(router, route, match.error)
}
if (match.status === 'redirected') {
if (!isRedirect(match.error)) {
if (process.env.NODE_ENV !== 'production') {
throw new Error('Invariant failed: Expected a redirect error')
}
invariant()
}
throw router.getMatch(match.id)?._nonReactive.loadPromise
}
if (match.status === 'error') {
const RouteErrorComponent =
(route.options.errorComponent ??
router.options.defaultErrorComponent) ||
ErrorComponent
return (
<RouteErrorComponent
error={match.error as any}
reset={undefined as any}
info={{
componentStack: '',
}}
/>
)
}
return out
}
const matchStore = router.stores.activeMatchStoresById.get(matchId)
if (!matchStore) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`Invariant failed: Could not find match for matchId "${matchId}". Please file an issue!`,
)
}
invariant()
}
// eslint-disable-next-line react-hooks/rules-of-hooks
const match = useStore(matchStore, (value) => value)
const routeId = match.routeId as string
const route = router.routesById[routeId] as AnyRoute
// eslint-disable-next-line react-hooks/rules-of-hooks
const key = React.useMemo(() => {
const remountFn =
(router.routesById[routeId] as AnyRoute).options.remountDeps ??
router.options.defaultRemountDeps
const remountDeps = remountFn?.({
routeId,
loaderDeps: match.loaderDeps,
params: match._strictParams,
search: match._strictSearch,
})
return remountDeps ? JSON.stringify(remountDeps) : undefined
}, [
routeId,
match.loaderDeps,
match._strictParams,
match._strictSearch,
router.options.defaultRemountDeps,
router.routesById,
])
// eslint-disable-next-line react-hooks/rules-of-hooks
const out = React.useMemo(() => {
const Comp = route.options.component ?? router.options.defaultComponent
if (Comp) {
return <Comp key={key} />
}
return <Outlet />
}, [key, route.options.component, router.options.defaultComponent])
if (match._displayPending) {
throw router.getMatch(match.id)?._nonReactive.displayPendingPromise
}
if (match._forcePending) {
throw router.getMatch(match.id)?._nonReactive.minPendingPromise
}
// see also hydrate() in packages/router-core/src/ssr/ssr-client.ts
if (match.status === 'pending') {
// We're pending, and if we have a minPendingMs, we need to wait for it
const pendingMinMs =
route.options.pendingMinMs ?? router.options.defaultPendingMinMs
if (pendingMinMs) {
const routerMatch = router.getMatch(match.id)
if (routerMatch && !routerMatch._nonReactive.minPendingPromise) {
// Create a promise that will resolve after the minPendingMs
if (!(isServer ?? router.isServer)) {
const minPendingPromise = createControlledPromise<void>()
routerMatch._nonReactive.minPendingPromise = minPendingPromise
setTimeout(() => {
minPendingPromise.resolve()
// We've handled the minPendingPromise, so we can delete it
routerMatch._nonReactive.minPendingPromise = undefined
}, pendingMinMs)
}
}
}
if (match._nonReactive.pendingRenderPromise?.status !== 'pending') {
match._nonReactive.pendingRenderPromise = createControlledPromise<void>()
}
throw match._nonReactive.pendingRenderPromise
}
if (match.status === 'notFound') {
if (!isNotFound(match.error)) {
if (process.env.NODE_ENV !== 'production') {
throw new Error('Invariant failed: Expected a notFound error')
}
invariant()
}
return renderRouteNotFound(router, route, match.error)
}
if (match.status === 'redirected') {
// Redirects should be handled by the router transition. If we happen to
// encounter a redirect here, it's a bug. Let's warn, but render nothing.
if (!isRedirect(match.error)) {
if (process.env.NODE_ENV !== 'production') {
throw new Error('Invariant failed: Expected a redirect error')
}
invariant()
}
// warning(
// false,
// 'Tried to render a redirected route match! This is a weird circumstance, please file an issue!',
// )
throw router.getMatch(match.id)?._nonReactive.loadPromise
}
if (match.status === 'error') {
// If we're on the server, we need to use React's new and super
// wonky api for throwing errors from a server side render inside
// of a suspense boundary. This is the only way to get
// renderToPipeableStream to not hang indefinitely.
// We'll serialize the error and rethrow it on the client.
if (isServer ?? router.isServer) {
const RouteErrorComponent =
(route.options.errorComponent ??
router.options.defaultErrorComponent) ||
ErrorComponent
return (
<RouteErrorComponent
error={match.error as any}
reset={undefined as any}
info={{
componentStack: '',
}}
/>
)
}
throw match.error
}
return out
})
/**
* Render the next child match in the route tree. Typically used inside
* a route component to render nested routes.
*
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/outletComponent
*/
export const Outlet = React.memo(function OutletImpl() {
const router = useRouter()
const matchId = React.useContext(matchContext)
let routeId: string | undefined
let parentGlobalNotFound = false
let childMatchId: string | undefined
if (isServer ?? router.isServer) {
const matches = router.stores.activeMatchesSnapshot.state
const parentIndex = matchId
? matches.findIndex((match) => match.id === matchId)
: -1
const parentMatch = parentIndex >= 0 ? matches[parentIndex] : undefined
routeId = parentMatch?.routeId as string | undefined
parentGlobalNotFound = parentMatch?.globalNotFound ?? false
childMatchId =
parentIndex >= 0 ? (matches[parentIndex + 1]?.id as string) : undefined
} else {
// Subscribe directly to the match store from the pool instead of
// the two-level byId → matchStore pattern.
const parentMatchStore = matchId
? router.stores.activeMatchStoresById.get(matchId)
: undefined
// eslint-disable-next-line react-hooks/rules-of-hooks
;[routeId, parentGlobalNotFound] = useStore(parentMatchStore, (match) => [
match?.routeId as string | undefined,
match?.globalNotFound ?? false,
])
// eslint-disable-next-line react-hooks/rules-of-hooks
childMatchId = useStore(router.stores.matchesId, (ids) => {
const index = ids.findIndex((id) => id === matchId)
return ids[index + 1]
})
}
const route = routeId ? router.routesById[routeId] : undefined
const pendingElement = router.options.defaultPendingComponent ? (
<router.options.defaultPendingComponent />
) : null
if (parentGlobalNotFound) {
if (!route) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
'Invariant failed: Could not resolve route for Outlet render',
)
}
invariant()
}
return renderRouteNotFound(router, route, undefined)
}
if (!childMatchId) {
return null
}
const nextMatch = <Match matchId={childMatchId} />
if (routeId === rootRouteId) {
return (
<React.Suspense fallback={pendingElement}>{nextMatch}</React.Suspense>
)
}
return nextMatch
})