@tanstack/router-core 1.168.9 → 1.168.11
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/hash-scroll.cjs +1 -1
- package/dist/cjs/hash-scroll.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/load-matches.cjs +6 -6
- package/dist/cjs/load-matches.cjs.map +1 -1
- package/dist/cjs/router.cjs +57 -58
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +3 -1
- package/dist/cjs/scroll-restoration.cjs +1 -1
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/ssr/createRequestHandler.cjs +2 -2
- package/dist/cjs/ssr/createRequestHandler.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/RawStream.cjs +41 -32
- package/dist/cjs/ssr/serializer/RawStream.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/RawStream.d.cts +12 -4
- package/dist/cjs/ssr/serializer/ShallowErrorPlugin.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/ShallowErrorPlugin.d.cts +2 -2
- package/dist/cjs/ssr/serializer/seroval-plugins.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/seroval-plugins.d.cts +2 -1
- package/dist/cjs/ssr/serializer/transformer.cjs +16 -14
- package/dist/cjs/ssr/serializer/transformer.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/transformer.d.cts +24 -23
- package/dist/cjs/ssr/ssr-client.cjs +9 -9
- package/dist/cjs/ssr/ssr-client.cjs.map +1 -1
- package/dist/cjs/ssr/ssr-server.cjs +31 -9
- package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
- package/dist/cjs/ssr/ssr-server.d.cts +3 -2
- package/dist/cjs/ssr/transformStreamWithRouter.cjs +4 -1
- package/dist/cjs/ssr/transformStreamWithRouter.cjs.map +1 -1
- package/dist/cjs/stores.cjs +57 -57
- package/dist/cjs/stores.cjs.map +1 -1
- package/dist/cjs/stores.d.cts +16 -16
- package/dist/esm/hash-scroll.js +1 -1
- package/dist/esm/hash-scroll.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/load-matches.js +6 -6
- package/dist/esm/load-matches.js.map +1 -1
- package/dist/esm/router.d.ts +3 -1
- package/dist/esm/router.js +57 -58
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/scroll-restoration.js +1 -1
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/esm/ssr/createRequestHandler.js +2 -2
- package/dist/esm/ssr/createRequestHandler.js.map +1 -1
- package/dist/esm/ssr/serializer/RawStream.d.ts +12 -4
- package/dist/esm/ssr/serializer/RawStream.js +41 -32
- package/dist/esm/ssr/serializer/RawStream.js.map +1 -1
- package/dist/esm/ssr/serializer/ShallowErrorPlugin.d.ts +2 -2
- package/dist/esm/ssr/serializer/ShallowErrorPlugin.js.map +1 -1
- package/dist/esm/ssr/serializer/seroval-plugins.d.ts +2 -1
- package/dist/esm/ssr/serializer/seroval-plugins.js.map +1 -1
- package/dist/esm/ssr/serializer/transformer.d.ts +24 -23
- package/dist/esm/ssr/serializer/transformer.js +16 -14
- package/dist/esm/ssr/serializer/transformer.js.map +1 -1
- package/dist/esm/ssr/ssr-client.js +9 -9
- package/dist/esm/ssr/ssr-client.js.map +1 -1
- package/dist/esm/ssr/ssr-server.d.ts +3 -2
- package/dist/esm/ssr/ssr-server.js +31 -9
- package/dist/esm/ssr/ssr-server.js.map +1 -1
- package/dist/esm/ssr/transformStreamWithRouter.js +4 -1
- package/dist/esm/ssr/transformStreamWithRouter.js.map +1 -1
- package/dist/esm/stores.d.ts +16 -16
- package/dist/esm/stores.js +58 -58
- package/dist/esm/stores.js.map +1 -1
- package/package.json +3 -3
- package/src/hash-scroll.ts +1 -1
- package/src/index.ts +1 -1
- package/src/load-matches.ts +8 -11
- package/src/router.ts +74 -85
- package/src/scroll-restoration.ts +1 -1
- package/src/ssr/createRequestHandler.ts +4 -5
- package/src/ssr/serializer/RawStream.ts +65 -56
- package/src/ssr/serializer/ShallowErrorPlugin.ts +2 -2
- package/src/ssr/serializer/seroval-plugins.ts +2 -1
- package/src/ssr/serializer/transformer.ts +71 -76
- package/src/ssr/ssr-client.ts +8 -12
- package/src/ssr/ssr-server.ts +39 -7
- package/src/ssr/transformStreamWithRouter.ts +3 -0
- package/src/stores.ts +86 -86
package/src/stores.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createLRUCache } from './lru-cache'
|
|
2
|
-
import { arraysEqual } from './utils'
|
|
2
|
+
import { arraysEqual, functionalUpdate } from './utils'
|
|
3
3
|
|
|
4
4
|
import type { AnyRoute } from './route'
|
|
5
5
|
import type { RouterState } from './router'
|
|
@@ -9,13 +9,13 @@ import type { AnyRedirect } from './redirect'
|
|
|
9
9
|
import type { AnyRouteMatch } from './Matches'
|
|
10
10
|
|
|
11
11
|
export interface RouterReadableStore<TValue> {
|
|
12
|
-
|
|
12
|
+
get: () => TValue
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export interface RouterWritableStore<
|
|
16
16
|
TValue,
|
|
17
17
|
> extends RouterReadableStore<TValue> {
|
|
18
|
-
|
|
18
|
+
set: ((updater: (prev: TValue) => TValue) => void) & ((value: TValue) => void)
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export type RouterBatchFn = (fn: () => void) => void
|
|
@@ -49,11 +49,11 @@ export function createNonReactiveMutableStore<TValue>(
|
|
|
49
49
|
let value = initialValue
|
|
50
50
|
|
|
51
51
|
return {
|
|
52
|
-
get
|
|
52
|
+
get() {
|
|
53
53
|
return value
|
|
54
54
|
},
|
|
55
|
-
|
|
56
|
-
value =
|
|
55
|
+
set(nextOrUpdater: TValue | ((prev: TValue) => TValue)) {
|
|
56
|
+
value = functionalUpdate(nextOrUpdater, value)
|
|
57
57
|
},
|
|
58
58
|
}
|
|
59
59
|
}
|
|
@@ -63,7 +63,7 @@ export function createNonReactiveReadonlyStore<TValue>(
|
|
|
63
63
|
read: () => TValue,
|
|
64
64
|
): RouterReadableStore<TValue> {
|
|
65
65
|
return {
|
|
66
|
-
get
|
|
66
|
+
get() {
|
|
67
67
|
return read()
|
|
68
68
|
},
|
|
69
69
|
}
|
|
@@ -81,24 +81,24 @@ export interface RouterStores<in out TRouteTree extends AnyRoute> {
|
|
|
81
81
|
statusCode: RouterWritableStore<number>
|
|
82
82
|
redirect: RouterWritableStore<AnyRedirect | undefined>
|
|
83
83
|
matchesId: RouterWritableStore<Array<string>>
|
|
84
|
-
|
|
84
|
+
pendingIds: RouterWritableStore<Array<string>>
|
|
85
85
|
/** @internal */
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
86
|
+
cachedIds: RouterWritableStore<Array<string>>
|
|
87
|
+
matches: ReadableStore<Array<AnyRouteMatch>>
|
|
88
|
+
pendingMatches: ReadableStore<Array<AnyRouteMatch>>
|
|
89
|
+
cachedMatches: ReadableStore<Array<AnyRouteMatch>>
|
|
90
|
+
firstId: ReadableStore<string | undefined>
|
|
91
|
+
hasPending: ReadableStore<boolean>
|
|
92
|
+
matchRouteDeps: ReadableStore<{
|
|
93
93
|
locationHref: string
|
|
94
94
|
resolvedLocationHref: string | undefined
|
|
95
95
|
status: RouterState<TRouteTree>['status']
|
|
96
96
|
}>
|
|
97
97
|
__store: RouterReadableStore<RouterState<TRouteTree>>
|
|
98
98
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
matchStores: Map<string, MatchStore>
|
|
100
|
+
pendingMatchStores: Map<string, MatchStore>
|
|
101
|
+
cachedMatchStores: Map<string, MatchStore>
|
|
102
102
|
|
|
103
103
|
/**
|
|
104
104
|
* Get a computed store that resolves a routeId to its current match state.
|
|
@@ -106,13 +106,13 @@ export interface RouterStores<in out TRouteTree extends AnyRoute> {
|
|
|
106
106
|
* The computed depends on matchesId + the individual match store, so
|
|
107
107
|
* subscribers are only notified when the resolved match state changes.
|
|
108
108
|
*/
|
|
109
|
-
|
|
109
|
+
getRouteMatchStore: (
|
|
110
110
|
routeId: string,
|
|
111
111
|
) => RouterReadableStore<AnyRouteMatch | undefined>
|
|
112
112
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
setMatches: (nextMatches: Array<AnyRouteMatch>) => void
|
|
114
|
+
setPending: (nextMatches: Array<AnyRouteMatch>) => void
|
|
115
|
+
setCached: (nextMatches: Array<AnyRouteMatch>) => void
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
export function createRouterStores<TRouteTree extends AnyRoute>(
|
|
@@ -122,9 +122,9 @@ export function createRouterStores<TRouteTree extends AnyRoute>(
|
|
|
122
122
|
const { createMutableStore, createReadonlyStore, batch, init } = config
|
|
123
123
|
|
|
124
124
|
// non reactive utilities
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
const
|
|
125
|
+
const matchStores = new Map<string, MatchStore>()
|
|
126
|
+
const pendingMatchStores = new Map<string, MatchStore>()
|
|
127
|
+
const cachedMatchStores = new Map<string, MatchStore>()
|
|
128
128
|
|
|
129
129
|
// atoms
|
|
130
130
|
const status = createMutableStore(initialState.status)
|
|
@@ -136,43 +136,43 @@ export function createRouterStores<TRouteTree extends AnyRoute>(
|
|
|
136
136
|
const statusCode = createMutableStore(initialState.statusCode)
|
|
137
137
|
const redirect = createMutableStore(initialState.redirect)
|
|
138
138
|
const matchesId = createMutableStore<Array<string>>([])
|
|
139
|
-
const
|
|
140
|
-
const
|
|
139
|
+
const pendingIds = createMutableStore<Array<string>>([])
|
|
140
|
+
const cachedIds = createMutableStore<Array<string>>([])
|
|
141
141
|
|
|
142
142
|
// 1st order derived stores
|
|
143
|
-
const
|
|
144
|
-
readPoolMatches(
|
|
143
|
+
const matches = createReadonlyStore(() =>
|
|
144
|
+
readPoolMatches(matchStores, matchesId.get()),
|
|
145
145
|
)
|
|
146
|
-
const
|
|
147
|
-
readPoolMatches(
|
|
146
|
+
const pendingMatches = createReadonlyStore(() =>
|
|
147
|
+
readPoolMatches(pendingMatchStores, pendingIds.get()),
|
|
148
148
|
)
|
|
149
|
-
const
|
|
150
|
-
readPoolMatches(
|
|
149
|
+
const cachedMatches = createReadonlyStore(() =>
|
|
150
|
+
readPoolMatches(cachedMatchStores, cachedIds.get()),
|
|
151
151
|
)
|
|
152
|
-
const
|
|
153
|
-
const
|
|
154
|
-
matchesId.
|
|
155
|
-
const store =
|
|
156
|
-
return store?.
|
|
152
|
+
const firstId = createReadonlyStore(() => matchesId.get()[0])
|
|
153
|
+
const hasPending = createReadonlyStore(() =>
|
|
154
|
+
matchesId.get().some((matchId) => {
|
|
155
|
+
const store = matchStores.get(matchId)
|
|
156
|
+
return store?.get().status === 'pending'
|
|
157
157
|
}),
|
|
158
158
|
)
|
|
159
|
-
const
|
|
160
|
-
locationHref: location.
|
|
161
|
-
resolvedLocationHref: resolvedLocation.
|
|
162
|
-
status: status.
|
|
159
|
+
const matchRouteDeps = createReadonlyStore(() => ({
|
|
160
|
+
locationHref: location.get().href,
|
|
161
|
+
resolvedLocationHref: resolvedLocation.get()?.href,
|
|
162
|
+
status: status.get(),
|
|
163
163
|
}))
|
|
164
164
|
|
|
165
165
|
// compatibility "big" state store
|
|
166
166
|
const __store = createReadonlyStore(() => ({
|
|
167
|
-
status: status.
|
|
168
|
-
loadedAt: loadedAt.
|
|
169
|
-
isLoading: isLoading.
|
|
170
|
-
isTransitioning: isTransitioning.
|
|
171
|
-
matches:
|
|
172
|
-
location: location.
|
|
173
|
-
resolvedLocation: resolvedLocation.
|
|
174
|
-
statusCode: statusCode.
|
|
175
|
-
redirect: redirect.
|
|
167
|
+
status: status.get(),
|
|
168
|
+
loadedAt: loadedAt.get(),
|
|
169
|
+
isLoading: isLoading.get(),
|
|
170
|
+
isTransitioning: isTransitioning.get(),
|
|
171
|
+
matches: matches.get(),
|
|
172
|
+
location: location.get(),
|
|
173
|
+
resolvedLocation: resolvedLocation.get(),
|
|
174
|
+
statusCode: statusCode.get(),
|
|
175
|
+
redirect: redirect.get(),
|
|
176
176
|
}))
|
|
177
177
|
|
|
178
178
|
// Per-routeId computed store cache.
|
|
@@ -188,21 +188,21 @@ export function createRouterStores<TRouteTree extends AnyRoute>(
|
|
|
188
188
|
RouterReadableStore<AnyRouteMatch | undefined>
|
|
189
189
|
>(64)
|
|
190
190
|
|
|
191
|
-
function
|
|
191
|
+
function getRouteMatchStore(
|
|
192
192
|
routeId: string,
|
|
193
193
|
): RouterReadableStore<AnyRouteMatch | undefined> {
|
|
194
194
|
let cached = matchStoreByRouteIdCache.get(routeId)
|
|
195
195
|
if (!cached) {
|
|
196
196
|
cached = createReadonlyStore(() => {
|
|
197
|
-
// Reading matchesId.
|
|
197
|
+
// Reading matchesId.get() tracks it as a dependency.
|
|
198
198
|
// When matchesId changes (navigation), this computed re-evaluates.
|
|
199
|
-
const ids = matchesId.
|
|
199
|
+
const ids = matchesId.get()
|
|
200
200
|
for (const id of ids) {
|
|
201
|
-
const matchStore =
|
|
201
|
+
const matchStore = matchStores.get(id)
|
|
202
202
|
if (matchStore && matchStore.routeId === routeId) {
|
|
203
|
-
// Reading matchStore.
|
|
203
|
+
// Reading matchStore.get() tracks it as a dependency.
|
|
204
204
|
// When the match store's state changes, this re-evaluates.
|
|
205
|
-
return matchStore.
|
|
205
|
+
return matchStore.get()
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
return undefined
|
|
@@ -223,64 +223,64 @@ export function createRouterStores<TRouteTree extends AnyRoute>(
|
|
|
223
223
|
statusCode,
|
|
224
224
|
redirect,
|
|
225
225
|
matchesId,
|
|
226
|
-
|
|
227
|
-
|
|
226
|
+
pendingIds,
|
|
227
|
+
cachedIds,
|
|
228
228
|
|
|
229
229
|
// derived
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
230
|
+
matches,
|
|
231
|
+
pendingMatches,
|
|
232
|
+
cachedMatches,
|
|
233
|
+
firstId,
|
|
234
|
+
hasPending,
|
|
235
|
+
matchRouteDeps,
|
|
236
236
|
|
|
237
237
|
// non-reactive state
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
238
|
+
matchStores,
|
|
239
|
+
pendingMatchStores,
|
|
240
|
+
cachedMatchStores,
|
|
241
241
|
|
|
242
242
|
// compatibility "big" state
|
|
243
243
|
__store,
|
|
244
244
|
|
|
245
245
|
// per-key computed stores
|
|
246
|
-
|
|
246
|
+
getRouteMatchStore,
|
|
247
247
|
|
|
248
248
|
// methods
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
249
|
+
setMatches,
|
|
250
|
+
setPending,
|
|
251
|
+
setCached,
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
// initialize the active matches
|
|
255
|
-
|
|
255
|
+
setMatches(initialState.matches as Array<AnyRouteMatch>)
|
|
256
256
|
init?.(store)
|
|
257
257
|
|
|
258
258
|
// setters to update non-reactive utilities in sync with the reactive stores
|
|
259
|
-
function
|
|
259
|
+
function setMatches(nextMatches: Array<AnyRouteMatch>) {
|
|
260
260
|
reconcileMatchPool(
|
|
261
261
|
nextMatches,
|
|
262
|
-
|
|
262
|
+
matchStores,
|
|
263
263
|
matchesId,
|
|
264
264
|
createMutableStore,
|
|
265
265
|
batch,
|
|
266
266
|
)
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
function
|
|
269
|
+
function setPending(nextMatches: Array<AnyRouteMatch>) {
|
|
270
270
|
reconcileMatchPool(
|
|
271
271
|
nextMatches,
|
|
272
|
-
|
|
273
|
-
|
|
272
|
+
pendingMatchStores,
|
|
273
|
+
pendingIds,
|
|
274
274
|
createMutableStore,
|
|
275
275
|
batch,
|
|
276
276
|
)
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
-
function
|
|
279
|
+
function setCached(nextMatches: Array<AnyRouteMatch>) {
|
|
280
280
|
reconcileMatchPool(
|
|
281
281
|
nextMatches,
|
|
282
|
-
|
|
283
|
-
|
|
282
|
+
cachedMatchStores,
|
|
283
|
+
cachedIds,
|
|
284
284
|
createMutableStore,
|
|
285
285
|
batch,
|
|
286
286
|
)
|
|
@@ -297,7 +297,7 @@ function readPoolMatches(
|
|
|
297
297
|
for (const id of ids) {
|
|
298
298
|
const matchStore = pool.get(id)
|
|
299
299
|
if (matchStore) {
|
|
300
|
-
matches.push(matchStore.
|
|
300
|
+
matches.push(matchStore.get())
|
|
301
301
|
}
|
|
302
302
|
}
|
|
303
303
|
return matches
|
|
@@ -330,13 +330,13 @@ function reconcileMatchPool(
|
|
|
330
330
|
}
|
|
331
331
|
|
|
332
332
|
existing.routeId = nextMatch.routeId
|
|
333
|
-
if (existing.
|
|
334
|
-
existing.
|
|
333
|
+
if (existing.get() !== nextMatch) {
|
|
334
|
+
existing.set(nextMatch)
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
337
|
|
|
338
|
-
if (!arraysEqual(idStore.
|
|
339
|
-
idStore.
|
|
338
|
+
if (!arraysEqual(idStore.get(), nextIds)) {
|
|
339
|
+
idStore.set(nextIds)
|
|
340
340
|
}
|
|
341
341
|
})
|
|
342
342
|
}
|