@tanstack/react-router 1.167.5 → 1.168.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Match.cjs +118 -52
- package/dist/cjs/Match.cjs.map +1 -1
- package/dist/cjs/Matches.cjs +20 -20
- package/dist/cjs/Matches.cjs.map +1 -1
- package/dist/cjs/Scripts.cjs +36 -32
- package/dist/cjs/Scripts.cjs.map +1 -1
- package/dist/cjs/Transitioner.cjs +10 -16
- package/dist/cjs/Transitioner.cjs.map +1 -1
- package/dist/cjs/headContentUtils.cjs +147 -59
- package/dist/cjs/headContentUtils.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.dev.cjs +1 -1
- package/dist/cjs/link.cjs +34 -29
- package/dist/cjs/link.cjs.map +1 -1
- package/dist/cjs/not-found.cjs +20 -2
- package/dist/cjs/not-found.cjs.map +1 -1
- package/dist/cjs/router.cjs +2 -1
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/routerStores.cjs +21 -0
- package/dist/cjs/routerStores.cjs.map +1 -0
- package/dist/cjs/routerStores.d.cts +7 -0
- package/dist/cjs/ssr/RouterClient.cjs +1 -1
- package/dist/cjs/ssr/RouterClient.cjs.map +1 -1
- package/dist/cjs/ssr/renderRouterToStream.cjs +2 -2
- package/dist/cjs/ssr/renderRouterToStream.cjs.map +1 -1
- package/dist/cjs/ssr/renderRouterToString.cjs +1 -1
- package/dist/cjs/ssr/renderRouterToString.cjs.map +1 -1
- package/dist/cjs/useCanGoBack.cjs +7 -2
- package/dist/cjs/useCanGoBack.cjs.map +1 -1
- package/dist/cjs/useLocation.cjs +21 -2
- package/dist/cjs/useLocation.cjs.map +1 -1
- package/dist/cjs/useMatch.cjs +29 -9
- package/dist/cjs/useMatch.cjs.map +1 -1
- package/dist/cjs/useRouterState.cjs +2 -2
- package/dist/cjs/useRouterState.cjs.map +1 -1
- package/dist/esm/Match.js +118 -52
- package/dist/esm/Match.js.map +1 -1
- package/dist/esm/Matches.js +21 -21
- package/dist/esm/Matches.js.map +1 -1
- package/dist/esm/Scripts.js +36 -32
- package/dist/esm/Scripts.js.map +1 -1
- package/dist/esm/Transitioner.js +10 -16
- package/dist/esm/Transitioner.js.map +1 -1
- package/dist/esm/headContentUtils.js +148 -60
- package/dist/esm/headContentUtils.js.map +1 -1
- package/dist/esm/index.dev.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/link.js +34 -29
- package/dist/esm/link.js.map +1 -1
- package/dist/esm/not-found.js +20 -2
- package/dist/esm/not-found.js.map +1 -1
- package/dist/esm/router.js +2 -1
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/routerStores.d.ts +7 -0
- package/dist/esm/routerStores.js +20 -0
- package/dist/esm/routerStores.js.map +1 -0
- package/dist/esm/ssr/RouterClient.js +1 -1
- package/dist/esm/ssr/RouterClient.js.map +1 -1
- package/dist/esm/ssr/renderRouterToStream.js +2 -2
- package/dist/esm/ssr/renderRouterToStream.js.map +1 -1
- package/dist/esm/ssr/renderRouterToString.js +1 -1
- package/dist/esm/ssr/renderRouterToString.js.map +1 -1
- package/dist/esm/useCanGoBack.js +6 -2
- package/dist/esm/useCanGoBack.js.map +1 -1
- package/dist/esm/useLocation.js +20 -2
- package/dist/esm/useLocation.js.map +1 -1
- package/dist/esm/useMatch.js +29 -9
- package/dist/esm/useMatch.js.map +1 -1
- package/dist/esm/useRouterState.js +2 -2
- package/dist/esm/useRouterState.js.map +1 -1
- package/dist/llms/rules/api.d.ts +1 -1
- package/dist/llms/rules/api.js +3 -9
- package/package.json +2 -2
- package/src/Match.tsx +218 -78
- package/src/Matches.tsx +45 -25
- package/src/Scripts.tsx +72 -44
- package/src/Transitioner.tsx +24 -16
- package/src/headContentUtils.tsx +210 -27
- package/src/link.tsx +66 -71
- package/src/not-found.tsx +41 -4
- package/src/router.ts +2 -1
- package/src/routerStores.ts +26 -0
- package/src/ssr/RouterClient.tsx +1 -1
- package/src/ssr/renderRouterToStream.tsx +2 -2
- package/src/ssr/renderRouterToString.tsx +1 -1
- package/src/useCanGoBack.ts +14 -2
- package/src/useLocation.tsx +32 -5
- package/src/useMatch.tsx +61 -21
- package/src/useRouterState.tsx +4 -2
|
@@ -36,7 +36,7 @@ export const renderRouterToStream = async ({
|
|
|
36
36
|
stream as unknown as ReadableStream,
|
|
37
37
|
)
|
|
38
38
|
return new Response(responseStream as any, {
|
|
39
|
-
status: router.
|
|
39
|
+
status: router.stores.statusCode.state,
|
|
40
40
|
headers: responseHeaders,
|
|
41
41
|
})
|
|
42
42
|
}
|
|
@@ -79,7 +79,7 @@ export const renderRouterToStream = async ({
|
|
|
79
79
|
reactAppPassthrough,
|
|
80
80
|
)
|
|
81
81
|
return new Response(responseStream as any, {
|
|
82
|
-
status: router.
|
|
82
|
+
status: router.stores.statusCode.state,
|
|
83
83
|
headers: responseHeaders,
|
|
84
84
|
})
|
|
85
85
|
}
|
package/src/useCanGoBack.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useStore } from '@tanstack/react-store'
|
|
2
|
+
import { isServer } from '@tanstack/router-core/isServer'
|
|
3
|
+
import { useRouter } from './useRouter'
|
|
2
4
|
|
|
3
5
|
export function useCanGoBack() {
|
|
4
|
-
|
|
6
|
+
const router = useRouter()
|
|
7
|
+
|
|
8
|
+
if (isServer ?? router.isServer) {
|
|
9
|
+
return router.stores.location.state.state.__TSR_index !== 0
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
|
|
13
|
+
return useStore(
|
|
14
|
+
router.stores.location,
|
|
15
|
+
(location) => location.state.__TSR_index !== 0,
|
|
16
|
+
)
|
|
5
17
|
}
|
package/src/useLocation.tsx
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useStore } from '@tanstack/react-store'
|
|
2
|
+
import { useRef } from 'react'
|
|
3
|
+
import { replaceEqualDeep } from '@tanstack/router-core'
|
|
4
|
+
import { isServer } from '@tanstack/router-core/isServer'
|
|
5
|
+
import { useRouter } from './useRouter'
|
|
2
6
|
import type {
|
|
3
7
|
StructuralSharingOption,
|
|
4
8
|
ValidateSelected,
|
|
@@ -45,8 +49,31 @@ export function useLocation<
|
|
|
45
49
|
opts?: UseLocationBaseOptions<TRouter, TSelected, TStructuralSharing> &
|
|
46
50
|
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
|
|
47
51
|
): UseLocationResult<TRouter, TSelected> {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
const router = useRouter<TRouter>()
|
|
53
|
+
|
|
54
|
+
if (isServer ?? router.isServer) {
|
|
55
|
+
const location = router.stores.location.state
|
|
56
|
+
return (
|
|
57
|
+
opts?.select ? opts.select(location as any) : location
|
|
58
|
+
) as UseLocationResult<TRouter, TSelected>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const previousResult =
|
|
62
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
|
|
63
|
+
useRef<ValidateSelected<TRouter, TSelected, TStructuralSharing>>(undefined)
|
|
64
|
+
|
|
65
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
|
|
66
|
+
return useStore(router.stores.location, (location) => {
|
|
67
|
+
const selected = (
|
|
68
|
+
opts?.select ? opts.select(location as any) : location
|
|
69
|
+
) as ValidateSelected<TRouter, TSelected, TStructuralSharing>
|
|
70
|
+
|
|
71
|
+
if (opts?.structuralSharing ?? router.options.defaultStructuralSharing) {
|
|
72
|
+
const shared = replaceEqualDeep(previousResult.current, selected)
|
|
73
|
+
previousResult.current = shared
|
|
74
|
+
return shared
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return selected
|
|
78
|
+
}) as UseLocationResult<TRouter, TSelected>
|
|
52
79
|
}
|
package/src/useMatch.tsx
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
|
+
import { useStore } from '@tanstack/react-store'
|
|
3
|
+
import { replaceEqualDeep } from '@tanstack/router-core'
|
|
4
|
+
import { isServer } from '@tanstack/router-core/isServer'
|
|
2
5
|
import invariant from 'tiny-invariant'
|
|
3
|
-
import { useRouterState } from './useRouterState'
|
|
4
6
|
import { dummyMatchContext, matchContext } from './matchContext'
|
|
7
|
+
import { useRouter } from './useRouter'
|
|
5
8
|
import type {
|
|
6
9
|
StructuralSharingOption,
|
|
7
10
|
ValidateSelected,
|
|
@@ -16,6 +19,12 @@ import type {
|
|
|
16
19
|
ThrowOrOptional,
|
|
17
20
|
} from '@tanstack/router-core'
|
|
18
21
|
|
|
22
|
+
const dummyStore = {
|
|
23
|
+
state: undefined,
|
|
24
|
+
get: () => undefined,
|
|
25
|
+
subscribe: () => () => {},
|
|
26
|
+
} as any
|
|
27
|
+
|
|
19
28
|
export interface UseMatchBaseOptions<
|
|
20
29
|
TRouter extends AnyRouter,
|
|
21
30
|
TFrom,
|
|
@@ -96,28 +105,59 @@ export function useMatch<
|
|
|
96
105
|
TStructuralSharing
|
|
97
106
|
>,
|
|
98
107
|
): ThrowOrOptional<UseMatchResult<TRouter, TFrom, TStrict, TSelected>, TThrow> {
|
|
108
|
+
const router = useRouter<TRouter>()
|
|
99
109
|
const nearestMatchId = React.useContext(
|
|
100
110
|
opts.from ? dummyMatchContext : matchContext,
|
|
101
111
|
)
|
|
102
112
|
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
113
|
+
const key = opts.from ?? nearestMatchId
|
|
114
|
+
const matchStore = key
|
|
115
|
+
? opts.from
|
|
116
|
+
? router.stores.getMatchStoreByRouteId(key)
|
|
117
|
+
: router.stores.activeMatchStoresById.get(key)
|
|
118
|
+
: undefined
|
|
119
|
+
|
|
120
|
+
if (isServer ?? router.isServer) {
|
|
121
|
+
const match = matchStore?.state
|
|
122
|
+
invariant(
|
|
123
|
+
!((opts.shouldThrow ?? true) && !match),
|
|
124
|
+
`Could not find ${opts.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
if (match === undefined) {
|
|
128
|
+
return undefined as any
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return (opts.select ? opts.select(match as any) : match) as any
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const previousResult =
|
|
135
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
|
|
136
|
+
React.useRef<ValidateSelected<TRouter, TSelected, TStructuralSharing>>(
|
|
137
|
+
undefined,
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static
|
|
141
|
+
return useStore(matchStore ?? dummyStore, (match) => {
|
|
142
|
+
invariant(
|
|
143
|
+
!((opts.shouldThrow ?? true) && !match),
|
|
144
|
+
`Could not find ${opts.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`,
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
if (match === undefined) {
|
|
148
|
+
return undefined
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const selected = (
|
|
152
|
+
opts.select ? opts.select(match as any) : match
|
|
153
|
+
) as ValidateSelected<TRouter, TSelected, TStructuralSharing>
|
|
154
|
+
|
|
155
|
+
if (opts.structuralSharing ?? router.options.defaultStructuralSharing) {
|
|
156
|
+
const shared = replaceEqualDeep(previousResult.current, selected)
|
|
157
|
+
previousResult.current = shared
|
|
158
|
+
return shared
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return selected
|
|
162
|
+
}) as any
|
|
123
163
|
}
|
package/src/useRouterState.tsx
CHANGED
|
@@ -57,7 +57,9 @@ export function useRouterState<
|
|
|
57
57
|
// Avoid subscribing to the store (and any structural sharing work) on the server.
|
|
58
58
|
const _isServer = isServer ?? router.isServer
|
|
59
59
|
if (_isServer) {
|
|
60
|
-
const state = router.state as RouterState<
|
|
60
|
+
const state = router.stores.__store.state as RouterState<
|
|
61
|
+
TRouter['routeTree']
|
|
62
|
+
>
|
|
61
63
|
return (opts?.select ? opts.select(state) : state) as UseRouterStateResult<
|
|
62
64
|
TRouter,
|
|
63
65
|
TSelected
|
|
@@ -69,7 +71,7 @@ export function useRouterState<
|
|
|
69
71
|
useRef<ValidateSelected<TRouter, TSelected, TStructuralSharing>>(undefined)
|
|
70
72
|
|
|
71
73
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
72
|
-
return useStore(router.__store, (state) => {
|
|
74
|
+
return useStore(router.stores.__store, (state) => {
|
|
73
75
|
if (opts?.select) {
|
|
74
76
|
if (opts.structuralSharing ?? router.options.defaultStructuralSharing) {
|
|
75
77
|
const newSlice = replaceEqualDeep(
|