@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.
Files changed (79) hide show
  1. package/dist/cjs/hash-scroll.cjs +1 -1
  2. package/dist/cjs/hash-scroll.cjs.map +1 -1
  3. package/dist/cjs/index.d.cts +1 -1
  4. package/dist/cjs/load-matches.cjs +6 -6
  5. package/dist/cjs/load-matches.cjs.map +1 -1
  6. package/dist/cjs/router.cjs +57 -58
  7. package/dist/cjs/router.cjs.map +1 -1
  8. package/dist/cjs/router.d.cts +3 -1
  9. package/dist/cjs/scroll-restoration.cjs +1 -1
  10. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  11. package/dist/cjs/ssr/createRequestHandler.cjs +2 -2
  12. package/dist/cjs/ssr/createRequestHandler.cjs.map +1 -1
  13. package/dist/cjs/ssr/serializer/RawStream.cjs +41 -32
  14. package/dist/cjs/ssr/serializer/RawStream.cjs.map +1 -1
  15. package/dist/cjs/ssr/serializer/RawStream.d.cts +12 -4
  16. package/dist/cjs/ssr/serializer/ShallowErrorPlugin.cjs.map +1 -1
  17. package/dist/cjs/ssr/serializer/ShallowErrorPlugin.d.cts +2 -2
  18. package/dist/cjs/ssr/serializer/seroval-plugins.cjs.map +1 -1
  19. package/dist/cjs/ssr/serializer/seroval-plugins.d.cts +2 -1
  20. package/dist/cjs/ssr/serializer/transformer.cjs +16 -14
  21. package/dist/cjs/ssr/serializer/transformer.cjs.map +1 -1
  22. package/dist/cjs/ssr/serializer/transformer.d.cts +24 -23
  23. package/dist/cjs/ssr/ssr-client.cjs +9 -9
  24. package/dist/cjs/ssr/ssr-client.cjs.map +1 -1
  25. package/dist/cjs/ssr/ssr-server.cjs +31 -9
  26. package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
  27. package/dist/cjs/ssr/ssr-server.d.cts +3 -2
  28. package/dist/cjs/ssr/transformStreamWithRouter.cjs +4 -1
  29. package/dist/cjs/ssr/transformStreamWithRouter.cjs.map +1 -1
  30. package/dist/cjs/stores.cjs +57 -57
  31. package/dist/cjs/stores.cjs.map +1 -1
  32. package/dist/cjs/stores.d.cts +16 -16
  33. package/dist/esm/hash-scroll.js +1 -1
  34. package/dist/esm/hash-scroll.js.map +1 -1
  35. package/dist/esm/index.d.ts +1 -1
  36. package/dist/esm/load-matches.js +6 -6
  37. package/dist/esm/load-matches.js.map +1 -1
  38. package/dist/esm/router.d.ts +3 -1
  39. package/dist/esm/router.js +57 -58
  40. package/dist/esm/router.js.map +1 -1
  41. package/dist/esm/scroll-restoration.js +1 -1
  42. package/dist/esm/scroll-restoration.js.map +1 -1
  43. package/dist/esm/ssr/createRequestHandler.js +2 -2
  44. package/dist/esm/ssr/createRequestHandler.js.map +1 -1
  45. package/dist/esm/ssr/serializer/RawStream.d.ts +12 -4
  46. package/dist/esm/ssr/serializer/RawStream.js +41 -32
  47. package/dist/esm/ssr/serializer/RawStream.js.map +1 -1
  48. package/dist/esm/ssr/serializer/ShallowErrorPlugin.d.ts +2 -2
  49. package/dist/esm/ssr/serializer/ShallowErrorPlugin.js.map +1 -1
  50. package/dist/esm/ssr/serializer/seroval-plugins.d.ts +2 -1
  51. package/dist/esm/ssr/serializer/seroval-plugins.js.map +1 -1
  52. package/dist/esm/ssr/serializer/transformer.d.ts +24 -23
  53. package/dist/esm/ssr/serializer/transformer.js +16 -14
  54. package/dist/esm/ssr/serializer/transformer.js.map +1 -1
  55. package/dist/esm/ssr/ssr-client.js +9 -9
  56. package/dist/esm/ssr/ssr-client.js.map +1 -1
  57. package/dist/esm/ssr/ssr-server.d.ts +3 -2
  58. package/dist/esm/ssr/ssr-server.js +31 -9
  59. package/dist/esm/ssr/ssr-server.js.map +1 -1
  60. package/dist/esm/ssr/transformStreamWithRouter.js +4 -1
  61. package/dist/esm/ssr/transformStreamWithRouter.js.map +1 -1
  62. package/dist/esm/stores.d.ts +16 -16
  63. package/dist/esm/stores.js +58 -58
  64. package/dist/esm/stores.js.map +1 -1
  65. package/package.json +3 -3
  66. package/src/hash-scroll.ts +1 -1
  67. package/src/index.ts +1 -1
  68. package/src/load-matches.ts +8 -11
  69. package/src/router.ts +74 -85
  70. package/src/scroll-restoration.ts +1 -1
  71. package/src/ssr/createRequestHandler.ts +4 -5
  72. package/src/ssr/serializer/RawStream.ts +65 -56
  73. package/src/ssr/serializer/ShallowErrorPlugin.ts +2 -2
  74. package/src/ssr/serializer/seroval-plugins.ts +2 -1
  75. package/src/ssr/serializer/transformer.ts +71 -76
  76. package/src/ssr/ssr-client.ts +8 -12
  77. package/src/ssr/ssr-server.ts +39 -7
  78. package/src/ssr/transformStreamWithRouter.ts +3 -0
  79. 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
- readonly state: TValue
12
+ get: () => TValue
13
13
  }
14
14
 
15
15
  export interface RouterWritableStore<
16
16
  TValue,
17
17
  > extends RouterReadableStore<TValue> {
18
- setState: (updater: (prev: TValue) => TValue) => void
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 state() {
52
+ get() {
53
53
  return value
54
54
  },
55
- setState(updater: (prev: TValue) => TValue) {
56
- value = updater(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 state() {
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
- pendingMatchesId: RouterWritableStore<Array<string>>
84
+ pendingIds: RouterWritableStore<Array<string>>
85
85
  /** @internal */
86
- cachedMatchesId: RouterWritableStore<Array<string>>
87
- activeMatchesSnapshot: ReadableStore<Array<AnyRouteMatch>>
88
- pendingMatchesSnapshot: ReadableStore<Array<AnyRouteMatch>>
89
- cachedMatchesSnapshot: ReadableStore<Array<AnyRouteMatch>>
90
- firstMatchId: ReadableStore<string | undefined>
91
- hasPendingMatches: ReadableStore<boolean>
92
- matchRouteReactivity: ReadableStore<{
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
- activeMatchStoresById: Map<string, MatchStore>
100
- pendingMatchStoresById: Map<string, MatchStore>
101
- cachedMatchStoresById: Map<string, MatchStore>
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
- getMatchStoreByRouteId: (
109
+ getRouteMatchStore: (
110
110
  routeId: string,
111
111
  ) => RouterReadableStore<AnyRouteMatch | undefined>
112
112
 
113
- setActiveMatches: (nextMatches: Array<AnyRouteMatch>) => void
114
- setPendingMatches: (nextMatches: Array<AnyRouteMatch>) => void
115
- setCachedMatches: (nextMatches: Array<AnyRouteMatch>) => void
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 activeMatchStoresById = new Map<string, MatchStore>()
126
- const pendingMatchStoresById = new Map<string, MatchStore>()
127
- const cachedMatchStoresById = new Map<string, MatchStore>()
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 pendingMatchesId = createMutableStore<Array<string>>([])
140
- const cachedMatchesId = createMutableStore<Array<string>>([])
139
+ const pendingIds = createMutableStore<Array<string>>([])
140
+ const cachedIds = createMutableStore<Array<string>>([])
141
141
 
142
142
  // 1st order derived stores
143
- const activeMatchesSnapshot = createReadonlyStore(() =>
144
- readPoolMatches(activeMatchStoresById, matchesId.state),
143
+ const matches = createReadonlyStore(() =>
144
+ readPoolMatches(matchStores, matchesId.get()),
145
145
  )
146
- const pendingMatchesSnapshot = createReadonlyStore(() =>
147
- readPoolMatches(pendingMatchStoresById, pendingMatchesId.state),
146
+ const pendingMatches = createReadonlyStore(() =>
147
+ readPoolMatches(pendingMatchStores, pendingIds.get()),
148
148
  )
149
- const cachedMatchesSnapshot = createReadonlyStore(() =>
150
- readPoolMatches(cachedMatchStoresById, cachedMatchesId.state),
149
+ const cachedMatches = createReadonlyStore(() =>
150
+ readPoolMatches(cachedMatchStores, cachedIds.get()),
151
151
  )
152
- const firstMatchId = createReadonlyStore(() => matchesId.state[0])
153
- const hasPendingMatches = createReadonlyStore(() =>
154
- matchesId.state.some((matchId) => {
155
- const store = activeMatchStoresById.get(matchId)
156
- return store?.state.status === 'pending'
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 matchRouteReactivity = createReadonlyStore(() => ({
160
- locationHref: location.state.href,
161
- resolvedLocationHref: resolvedLocation.state?.href,
162
- status: status.state,
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.state,
168
- loadedAt: loadedAt.state,
169
- isLoading: isLoading.state,
170
- isTransitioning: isTransitioning.state,
171
- matches: activeMatchesSnapshot.state,
172
- location: location.state,
173
- resolvedLocation: resolvedLocation.state,
174
- statusCode: statusCode.state,
175
- redirect: redirect.state,
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 getMatchStoreByRouteId(
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.state tracks it as a dependency.
197
+ // Reading matchesId.get() tracks it as a dependency.
198
198
  // When matchesId changes (navigation), this computed re-evaluates.
199
- const ids = matchesId.state
199
+ const ids = matchesId.get()
200
200
  for (const id of ids) {
201
- const matchStore = activeMatchStoresById.get(id)
201
+ const matchStore = matchStores.get(id)
202
202
  if (matchStore && matchStore.routeId === routeId) {
203
- // Reading matchStore.state tracks it as a dependency.
203
+ // Reading matchStore.get() tracks it as a dependency.
204
204
  // When the match store's state changes, this re-evaluates.
205
- return matchStore.state
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
- pendingMatchesId,
227
- cachedMatchesId,
226
+ pendingIds,
227
+ cachedIds,
228
228
 
229
229
  // derived
230
- activeMatchesSnapshot,
231
- pendingMatchesSnapshot,
232
- cachedMatchesSnapshot,
233
- firstMatchId,
234
- hasPendingMatches,
235
- matchRouteReactivity,
230
+ matches,
231
+ pendingMatches,
232
+ cachedMatches,
233
+ firstId,
234
+ hasPending,
235
+ matchRouteDeps,
236
236
 
237
237
  // non-reactive state
238
- activeMatchStoresById,
239
- pendingMatchStoresById,
240
- cachedMatchStoresById,
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
- getMatchStoreByRouteId,
246
+ getRouteMatchStore,
247
247
 
248
248
  // methods
249
- setActiveMatches,
250
- setPendingMatches,
251
- setCachedMatches,
249
+ setMatches,
250
+ setPending,
251
+ setCached,
252
252
  }
253
253
 
254
254
  // initialize the active matches
255
- setActiveMatches(initialState.matches as Array<AnyRouteMatch>)
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 setActiveMatches(nextMatches: Array<AnyRouteMatch>) {
259
+ function setMatches(nextMatches: Array<AnyRouteMatch>) {
260
260
  reconcileMatchPool(
261
261
  nextMatches,
262
- activeMatchStoresById,
262
+ matchStores,
263
263
  matchesId,
264
264
  createMutableStore,
265
265
  batch,
266
266
  )
267
267
  }
268
268
 
269
- function setPendingMatches(nextMatches: Array<AnyRouteMatch>) {
269
+ function setPending(nextMatches: Array<AnyRouteMatch>) {
270
270
  reconcileMatchPool(
271
271
  nextMatches,
272
- pendingMatchStoresById,
273
- pendingMatchesId,
272
+ pendingMatchStores,
273
+ pendingIds,
274
274
  createMutableStore,
275
275
  batch,
276
276
  )
277
277
  }
278
278
 
279
- function setCachedMatches(nextMatches: Array<AnyRouteMatch>) {
279
+ function setCached(nextMatches: Array<AnyRouteMatch>) {
280
280
  reconcileMatchPool(
281
281
  nextMatches,
282
- cachedMatchStoresById,
283
- cachedMatchesId,
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.state)
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.state !== nextMatch) {
334
- existing.setState(() => nextMatch)
333
+ if (existing.get() !== nextMatch) {
334
+ existing.set(nextMatch)
335
335
  }
336
336
  }
337
337
 
338
- if (!arraysEqual(idStore.state, nextIds)) {
339
- idStore.setState(() => nextIds)
338
+ if (!arraysEqual(idStore.get(), nextIds)) {
339
+ idStore.set(nextIds)
340
340
  }
341
341
  })
342
342
  }