@tanstack/start-client-core 1.121.33 → 1.121.39

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