@tanstack/react-start-client 1.114.2 → 1.114.4

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 (49) hide show
  1. package/dist/cjs/StartClient.cjs +2 -2
  2. package/dist/cjs/StartClient.cjs.map +1 -1
  3. package/dist/cjs/StartClient.d.cts +1 -1
  4. package/dist/cjs/createServerFn.cjs +6 -7
  5. package/dist/cjs/createServerFn.cjs.map +1 -1
  6. package/dist/cjs/index.cjs +0 -4
  7. package/dist/cjs/index.cjs.map +1 -1
  8. package/dist/cjs/index.d.cts +0 -3
  9. package/dist/cjs/json.cjs +2 -2
  10. package/dist/cjs/json.cjs.map +1 -1
  11. package/dist/esm/StartClient.d.ts +1 -1
  12. package/dist/esm/StartClient.js +1 -1
  13. package/dist/esm/StartClient.js.map +1 -1
  14. package/dist/esm/createServerFn.js +1 -2
  15. package/dist/esm/createServerFn.js.map +1 -1
  16. package/dist/esm/index.d.ts +0 -3
  17. package/dist/esm/index.js +0 -4
  18. package/dist/esm/index.js.map +1 -1
  19. package/dist/esm/json.js +1 -1
  20. package/dist/esm/json.js.map +1 -1
  21. package/package.json +4 -3
  22. package/src/StartClient.tsx +2 -2
  23. package/src/createServerFn.ts +1 -2
  24. package/src/index.tsx +0 -12
  25. package/src/json.ts +1 -1
  26. package/dist/cjs/headers.cjs +0 -30
  27. package/dist/cjs/headers.cjs.map +0 -1
  28. package/dist/cjs/headers.d.cts +0 -5
  29. package/dist/cjs/serializer.cjs +0 -152
  30. package/dist/cjs/serializer.cjs.map +0 -1
  31. package/dist/cjs/serializer.d.cts +0 -2
  32. package/dist/cjs/ssr-client.cjs +0 -130
  33. package/dist/cjs/ssr-client.cjs.map +0 -1
  34. package/dist/cjs/ssr-client.d.cts +0 -65
  35. package/dist/cjs/tests/transformer.test.d.cts +0 -1
  36. package/dist/esm/headers.d.ts +0 -5
  37. package/dist/esm/headers.js +0 -30
  38. package/dist/esm/headers.js.map +0 -1
  39. package/dist/esm/serializer.d.ts +0 -2
  40. package/dist/esm/serializer.js +0 -152
  41. package/dist/esm/serializer.js.map +0 -1
  42. package/dist/esm/ssr-client.d.ts +0 -65
  43. package/dist/esm/ssr-client.js +0 -130
  44. package/dist/esm/ssr-client.js.map +0 -1
  45. package/dist/esm/tests/transformer.test.d.ts +0 -1
  46. package/src/headers.ts +0 -50
  47. package/src/serializer.ts +0 -177
  48. package/src/ssr-client.tsx +0 -246
  49. package/src/tests/transformer.test.tsx +0 -147
@@ -1,246 +0,0 @@
1
- import { isPlainObject } from '@tanstack/router-core'
2
-
3
- import invariant from 'tiny-invariant'
4
-
5
- import { startSerializer } from './serializer'
6
- import type {
7
- AnyRouter,
8
- ControllablePromise,
9
- MakeRouteMatch,
10
- } from '@tanstack/react-router'
11
-
12
- import type {
13
- DeferredPromiseState,
14
- Manifest,
15
- RouteContextOptions,
16
- } from '@tanstack/router-core'
17
-
18
- declare global {
19
- interface Window {
20
- __TSR_SSR__?: StartSsrGlobal
21
- }
22
- }
23
-
24
- export interface StartSsrGlobal {
25
- matches: Array<SsrMatch>
26
- streamedValues: Record<
27
- string,
28
- {
29
- value: any
30
- parsed: any
31
- }
32
- >
33
- cleanScripts: () => void
34
- dehydrated?: any
35
- initMatch: (match: SsrMatch) => void
36
- resolvePromise: (opts: {
37
- matchId: string
38
- id: number
39
- promiseState: DeferredPromiseState<any>
40
- }) => void
41
- injectChunk: (opts: { matchId: string; id: number; chunk: string }) => void
42
- closeStream: (opts: { matchId: string; id: number }) => void
43
- }
44
-
45
- export interface SsrMatch {
46
- id: string
47
- __beforeLoadContext: string
48
- loaderData?: string
49
- error?: string
50
- extracted?: Array<ClientExtractedEntry>
51
- updatedAt: MakeRouteMatch['updatedAt']
52
- status: MakeRouteMatch['status']
53
- }
54
-
55
- export type ClientExtractedEntry =
56
- | ClientExtractedStream
57
- | ClientExtractedPromise
58
-
59
- export interface ClientExtractedPromise extends ClientExtractedBaseEntry {
60
- type: 'promise'
61
- value?: ControllablePromise<any>
62
- }
63
-
64
- export interface ClientExtractedStream extends ClientExtractedBaseEntry {
65
- type: 'stream'
66
- value?: ReadableStream & { controller?: ReadableStreamDefaultController }
67
- }
68
-
69
- export interface ClientExtractedBaseEntry {
70
- type: string
71
- path: Array<string>
72
- }
73
-
74
- export interface ResolvePromiseState {
75
- matchId: string
76
- id: number
77
- promiseState: DeferredPromiseState<any>
78
- }
79
-
80
- export interface DehydratedRouter {
81
- manifest: Manifest | undefined
82
- dehydratedData: any
83
- }
84
-
85
- export function hydrate(router: AnyRouter) {
86
- invariant(
87
- window.__TSR_SSR__?.dehydrated,
88
- 'Expected to find a dehydrated data on window.__TSR_SSR__.dehydrated... but we did not. Please file an issue!',
89
- )
90
-
91
- const { manifest, dehydratedData } = startSerializer.parse(
92
- window.__TSR_SSR__.dehydrated,
93
- ) as DehydratedRouter
94
-
95
- router.ssr = {
96
- manifest,
97
- serializer: startSerializer,
98
- }
99
-
100
- router.clientSsr = {
101
- getStreamedValue: <T,>(key: string): T | undefined => {
102
- if (router.isServer) {
103
- return undefined
104
- }
105
-
106
- const streamedValue = window.__TSR_SSR__?.streamedValues[key]
107
-
108
- if (!streamedValue) {
109
- return
110
- }
111
-
112
- if (!streamedValue.parsed) {
113
- streamedValue.parsed = router.ssr!.serializer.parse(streamedValue.value)
114
- }
115
-
116
- return streamedValue.parsed
117
- },
118
- }
119
-
120
- // Hydrate the router state
121
- const matches = router.matchRoutes(router.state.location)
122
- // kick off loading the route chunks
123
- const routeChunkPromise = Promise.all(
124
- matches.map((match) => {
125
- const route = router.looseRoutesById[match.routeId]!
126
- return router.loadRouteChunk(route)
127
- }),
128
- )
129
- // Right after hydration and before the first render, we need to rehydrate each match
130
- // First step is to reyhdrate loaderData and __beforeLoadContext
131
- matches.forEach((match) => {
132
- const dehydratedMatch = window.__TSR_SSR__!.matches.find(
133
- (d) => d.id === match.id,
134
- )
135
-
136
- if (dehydratedMatch) {
137
- Object.assign(match, dehydratedMatch)
138
-
139
- // Handle beforeLoadContext
140
- if (dehydratedMatch.__beforeLoadContext) {
141
- match.__beforeLoadContext = router.ssr!.serializer.parse(
142
- dehydratedMatch.__beforeLoadContext,
143
- ) as any
144
- }
145
-
146
- // Handle loaderData
147
- if (dehydratedMatch.loaderData) {
148
- match.loaderData = router.ssr!.serializer.parse(
149
- dehydratedMatch.loaderData,
150
- )
151
- }
152
-
153
- // Handle error
154
- if (dehydratedMatch.error) {
155
- match.error = router.ssr!.serializer.parse(dehydratedMatch.error)
156
- }
157
-
158
- // Handle extracted
159
- ;(match as unknown as SsrMatch).extracted?.forEach((ex) => {
160
- deepMutableSetByPath(match, ['loaderData', ...ex.path], ex.value)
161
- })
162
- } else {
163
- Object.assign(match, {
164
- status: 'success',
165
- updatedAt: Date.now(),
166
- })
167
- }
168
-
169
- return match
170
- })
171
-
172
- router.__store.setState((s) => {
173
- return {
174
- ...s,
175
- matches,
176
- }
177
- })
178
-
179
- // Allow the user to handle custom hydration data
180
- router.options.hydrate?.(dehydratedData)
181
-
182
- // now that all necessary data is hydrated:
183
- // 1) fully reconstruct the route context
184
- // 2) execute `head()` and `scripts()` for each match
185
- router.state.matches.forEach((match) => {
186
- const route = router.looseRoutesById[match.routeId]!
187
-
188
- const parentMatch = router.state.matches[match.index - 1]
189
- const parentContext = parentMatch?.context ?? router.options.context ?? {}
190
-
191
- // `context()` was already executed by `matchRoutes`, however route context was not yet fully reconstructed
192
- // so run it again and merge route context
193
- const contextFnContext: RouteContextOptions<any, any, any, any> = {
194
- deps: match.loaderDeps,
195
- params: match.params,
196
- context: parentContext,
197
- location: router.state.location,
198
- navigate: (opts: any) =>
199
- router.navigate({ ...opts, _fromLocation: router.state.location }),
200
- buildLocation: router.buildLocation,
201
- cause: match.cause,
202
- abortController: match.abortController,
203
- preload: false,
204
- matches,
205
- }
206
- match.__routeContext = route.options.context?.(contextFnContext) ?? {}
207
-
208
- match.context = {
209
- ...parentContext,
210
- ...match.__routeContext,
211
- ...match.__beforeLoadContext,
212
- }
213
-
214
- const assetContext = {
215
- matches: router.state.matches,
216
- match,
217
- params: match.params,
218
- loaderData: match.loaderData,
219
- }
220
- const headFnContent = route.options.head?.(assetContext)
221
-
222
- const scripts = route.options.scripts?.(assetContext)
223
-
224
- match.meta = headFnContent?.meta
225
- match.links = headFnContent?.links
226
- match.headScripts = headFnContent?.scripts
227
- match.scripts = scripts
228
- })
229
-
230
- return routeChunkPromise
231
- }
232
-
233
- function deepMutableSetByPath<T>(obj: T, path: Array<string>, value: any) {
234
- // mutable set by path retaining array and object references
235
- if (path.length === 1) {
236
- ;(obj as any)[path[0]!] = value
237
- }
238
-
239
- const [key, ...rest] = path
240
-
241
- if (Array.isArray(obj)) {
242
- deepMutableSetByPath(obj[Number(key)], rest, value)
243
- } else if (isPlainObject(obj)) {
244
- deepMutableSetByPath((obj as any)[key!], rest, value)
245
- }
246
- }
@@ -1,147 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { startSerializer as tf } from '../serializer'
4
-
5
- describe('transformer.stringify', () => {
6
- it('should stringify dates', () => {
7
- const date = new Date('2021-08-19T20:00:00.000Z')
8
- expect(tf.stringify(date)).toMatchInlineSnapshot(`
9
- "{"$date":"2021-08-19T20:00:00.000Z"}"
10
- `)
11
- })
12
-
13
- it('should stringify undefined', () => {
14
- expect(tf.stringify(undefined)).toMatchInlineSnapshot(`"{"$undefined":0}"`)
15
- })
16
-
17
- it('should stringify object foo="bar"', () => {
18
- expect(tf.stringify({ foo: 'bar' })).toMatchInlineSnapshot(`
19
- "{"foo":"bar"}"
20
- `)
21
- })
22
-
23
- it('should stringify object foo=undefined', () => {
24
- expect(tf.stringify({ foo: undefined })).toMatchInlineSnapshot(
25
- `"{"foo":{"$undefined":0}}"`,
26
- )
27
- })
28
-
29
- it('should stringify object foo=Date', () => {
30
- const date = new Date('2021-08-19T20:00:00.000Z')
31
- expect(tf.stringify({ foo: date })).toMatchInlineSnapshot(`
32
- "{"foo":{"$date":"2021-08-19T20:00:00.000Z"}}"
33
- `)
34
- })
35
-
36
- it('should stringify empty FormData', () => {
37
- const formData = new FormData()
38
- expect(tf.stringify(formData)).toMatchInlineSnapshot(`"{"$formData":{}}"`)
39
- })
40
-
41
- it('should stringify FormData with key-value pairs of foo="bar",name="Sean"', () => {
42
- const formData = new FormData()
43
- formData.append('foo', 'bar')
44
- formData.append('name', 'Sean')
45
- expect(tf.stringify(formData)).toMatchInlineSnapshot(
46
- `"{"$formData":{"foo":"bar","name":"Sean"}}"`,
47
- )
48
- })
49
- it('should stringify FormData with multiple values for the same key', () => {
50
- const formData = new FormData()
51
- formData.append('foo', 'bar')
52
- formData.append('foo', 'baz')
53
- expect(tf.stringify(formData)).toMatchInlineSnapshot(
54
- `"{"$formData":{"foo":["bar","baz"]}}"`,
55
- )
56
- })
57
-
58
- it('should stringify bigint', () => {
59
- const bigint = BigInt('9007199254740992')
60
- expect(tf.stringify(bigint)).toMatchInlineSnapshot(
61
- `"{"$bigint":"9007199254740992"}"`,
62
- )
63
- })
64
- it('should stringify object foo=bigint', () => {
65
- const bigint = BigInt('9007199254740992')
66
- expect(tf.stringify({ foo: bigint })).toMatchInlineSnapshot(
67
- `"{"foo":{"$bigint":"9007199254740992"}}"`,
68
- )
69
- })
70
- })
71
-
72
- describe('transformer.parse', () => {
73
- it('should parse dates', () => {
74
- const date = new Date('2021-08-19T20:00:00.000Z')
75
- const str = tf.stringify(date)
76
- expect(tf.parse(str)).toEqual(date)
77
- })
78
-
79
- it('should parse undefined', () => {
80
- const str = tf.stringify(undefined)
81
- expect(tf.parse(str)).toBeUndefined()
82
- })
83
-
84
- it('should parse object foo="bar"', () => {
85
- const obj = { foo: 'bar' }
86
- const str = tf.stringify(obj)
87
- expect(tf.parse(str)).toEqual(obj)
88
- })
89
-
90
- it('should parse object foo=undefined', () => {
91
- const obj = { foo: undefined }
92
- const str = tf.stringify(obj)
93
- expect(tf.parse(str)).toEqual(obj)
94
- })
95
-
96
- it('should parse object foo=Date', () => {
97
- const date = new Date('2021-08-19T20:00:00.000Z')
98
- const obj = { foo: date }
99
- const str = tf.stringify(obj)
100
- expect(tf.parse(str)).toEqual(obj)
101
- })
102
-
103
- it('should parse empty FormData', () => {
104
- const formData = new FormData()
105
- const str = tf.stringify(formData)
106
- const parsed = tf.parse(str) as FormData
107
- expect(parsed).toBeInstanceOf(FormData)
108
- expect([...parsed.entries()]).toEqual([])
109
- })
110
-
111
- it('should parse FormData with key-value pairs of foo="bar",name="Sean"', () => {
112
- const formData = new FormData()
113
- formData.append('foo', 'bar')
114
- formData.append('name', 'Sean')
115
- const str = tf.stringify(formData)
116
- const parsed = tf.parse(str) as FormData
117
- expect(parsed).toBeInstanceOf(FormData)
118
- expect([...parsed.entries()]).toEqual([
119
- ['foo', 'bar'],
120
- ['name', 'Sean'],
121
- ])
122
- })
123
- it('should parse FormData with multiple values for the same key', () => {
124
- const formData = new FormData()
125
- formData.append('foo', 'bar')
126
- formData.append('foo', 'baz')
127
- const str = tf.stringify(formData)
128
- const parsed = tf.parse(str) as FormData
129
- expect(parsed).toBeInstanceOf(FormData)
130
- expect([...parsed.entries()]).toEqual([
131
- ['foo', 'bar'],
132
- ['foo', 'baz'],
133
- ])
134
- })
135
-
136
- it('should parse bigint', () => {
137
- const bigint = BigInt('9007199254740992')
138
- const str = tf.stringify(bigint)
139
- expect(tf.parse(str)).toEqual(bigint)
140
- })
141
- it('should parse object foo=bigint', () => {
142
- const bigint = BigInt('9007199254740992')
143
- const obj = { foo: bigint }
144
- const str = tf.stringify(obj)
145
- expect(tf.parse(str)).toEqual(obj)
146
- })
147
- })