Open History to see the last commit
1import { ClientOnly, createFileRoute } from '@tanstack/react-router'
2import * as React from 'react'
3import { createServerFn } from '@tanstack/react-start'
4β
5/**
6 * This checks whether the returned payloads from a
7 * server function are the same, regardless of whether the server function is
8 * called directly from the client or from within the server function.
9 * @link https://github.com/TanStack/router/issues/1866
10 * @link https://github.com/TanStack/router/issues/2481
11 */
12β
13export const Route = createFileRoute('/consistent')({
14 component: ConsistentServerFnCalls,
15 loader: async () => {
16 const data = await cons_serverGetFn1({ data: { username: 'TEST' } })
17 console.log('cons_serverGetFn1', data)
18 return { data }
19 },
20})
21β
22const cons_getFn1 = createServerFn()
23 .validator((d: { username: string }) => d)
24 .handler(({ data }) => {
25 return { payload: data }
26 })
27β
28const cons_serverGetFn1 = createServerFn()
29 .validator((d: { username: string }) => d)
30 .handler(async ({ data }) => {
31 return cons_getFn1({ data })
32 })
33β
34const cons_postFn1 = createServerFn({ method: 'POST' })
35 .validator((d: { username: string }) => d)
36 .handler(({ data }) => {
37 return { payload: data }
38 })
39β
40const cons_serverPostFn1 = createServerFn({ method: 'POST' })
41 .validator((d: { username: string }) => d)
42 .handler(({ data }) => {
43 return cons_postFn1({ data })
44 })
45β
46function ConsistentServerFnCalls() {
47 const [getServerResult, setGetServerResult] = React.useState({})
48 const [getDirectResult, setGetDirectResult] = React.useState({})
49β
50 const [postServerResult, setPostServerResult] = React.useState({})
51 const [postDirectResult, setPostDirectResult] = React.useState({})
52β
53 return (
54 <div className="p-2 m-2 grid gap-2">
55 <h3>Consistent Server Fn GET Calls</h3>
56 <p>
57 This component checks whether the returned payloads from server function
58 are the same, regardless of whether the server function is called
59 directly from the client or from within the server function.
60 </p>
61 <div>
62 It should return{' '}
63 <code>
64 <pre data-testid="expected-consistent-server-fns-result">
65 {JSON.stringify({ payload: { username: 'TEST' } })}
66 </pre>
67 </code>
68 </div>
69 <p>
70 {`GET: cons_getFn1 called from server cons_serverGetFn1 returns`}
71 <br />
72 <span data-testid="cons_serverGetFn1-response">
73 {JSON.stringify(getServerResult)}
74 </span>
75 </p>
76 <p>
77 {`GET: cons_getFn1 called directly returns`}
78 <br />
79 <span data-testid="cons_getFn1-response">
80 {JSON.stringify(getDirectResult)}
81 </span>
82 </p>
83 <p>
84 {`POST: cons_postFn1 called from cons_serverPostFn1 returns`}
85 <br />
86 <span data-testid="cons_serverPostFn1-response">
87 {JSON.stringify(postServerResult)}
88 </span>
89 </p>
90 <p>
91 {`POST: cons_postFn1 called directly returns`}
92 <br />
93 <span data-testid="cons_postFn1-response">
94 {JSON.stringify(postDirectResult)}
95 </span>
96 </p>
97 <button
98 data-testid="test-consistent-server-fn-calls-btn"
99 type="button"
100 className="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-xs ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
101 onClick={() => {
102 // GET calls
103 cons_serverGetFn1({ data: { username: 'TEST' } }).then(
104 setGetServerResult,
105 )
106 cons_getFn1({ data: { username: 'TEST' } }).then(setGetDirectResult)
107β
108 // POST calls
109 cons_serverPostFn1({ data: { username: 'TEST' } }).then(
110 setPostServerResult,
111 )
112 cons_postFn1({ data: { username: 'TEST' } }).then(setPostDirectResult)
113β
114 cons_postFn1({ data: { username: 'TEST' } }).then(setPostDirectResult)
115 }}
116 >
117 Test Consistent server function responses
118 </button>
119 <ClientOnly>
120 <span data-testid="consistent-client-hydrated" hidden />
121 </ClientOnly>
122 </div>
123 )
124}