Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

index.tsx

167 lines4.8 KB

1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { createServerFn } from '@tanstack/react-start'
4import { emitServerEvent } from '../devtools'
5​
6// Server function that emits a devtools event.
7// With Nitro v3, this runs in an isolated worker thread.
8// Previously, the devtools event would be lost because globalThis.__TANSTACK_EVENT_TARGET__
9// doesn't exist in the worker. With network transport fallback, it reaches the devtools panel.
10const greet = createServerFn({ method: 'GET' }).handler(async () => {
11 const message = `Hello from server at ${new Date().toLocaleTimeString()}`
12 emitServerEvent('greet()', { message })
13 return message
14})
15​
16const generateNumber = createServerFn({ method: 'GET' }).handler(async () => {
17 const number = Math.floor(Math.random() * 1000)
18 emitServerEvent('generateNumber()', { number })
19 return number
20})
21​
22const fetchData = createServerFn({ method: 'POST' })
23 .inputValidator((d: string) => d)
24 .handler(async ({ data }) => {
25 const result = { query: data, results: Math.floor(Math.random() * 100) }
26 emitServerEvent('fetchData()', result)
27 return result
28 })
29​
30export const Route = createFileRoute('/')({
31 component: App,
32 loader: async () => {
33 emitServerEvent('loader(/)', { route: '/' })
34 return { loadedAt: new Date().toISOString() }
35 },
36})
37​
38function App() {
39 const loaderData = Route.useLoaderData()
40 const [results, setResults] = useState<Array<string>>([])
41​
42 const addResult = (text: string) => {
43 setResults((prev) =>
44 [`[${new Date().toLocaleTimeString()}] ${text}`, ...prev].slice(0, 20),
45 )
46 }
47​
48 return (
49 <div style={{ maxWidth: '800px', margin: '0 auto', padding: '40px 20px' }}>
50 <h1 style={{ fontSize: '28px', marginBottom: '8px' }}>
51 Nitro v3 Devtools Test
52 </h1>
53 <p style={{ color: '#666', marginBottom: '32px' }}>
54 Each button calls a server function running in Nitro's isolated worker
55 thread. Open the TanStack Devtools panel (bottom-right) and switch to
56 the "Server Events" tab to see events arriving from the server.
57 </p>
58​
59 <div
60 style={{
61 display: 'flex',
62 gap: '12px',
63 marginBottom: '24px',
64 flexWrap: 'wrap',
65 }}
66 >
67 <button
68 onClick={async () => {
69 const msg = await greet()
70 addResult(msg)
71 }}
72 style={{
73 padding: '12px 24px',
74 fontSize: '14px',
75 borderRadius: '8px',
76 border: 'none',
77 background: '#0ea5e9',
78 color: 'white',
79 cursor: 'pointer',
80 fontWeight: 600,
81 }}
82 >
83 Call greet()
84 </button>
85 <button
86 onClick={async () => {
87 const num = await generateNumber()
88 addResult(`Random number: ${num}`)
89 }}
90 style={{
91 padding: '12px 24px',
92 fontSize: '14px',
93 borderRadius: '8px',
94 border: 'none',
95 background: '#8b5cf6',
96 color: 'white',
97 cursor: 'pointer',
98 fontWeight: 600,
99 }}
100 >
101 Call generateNumber()
102 </button>
103 <button
104 onClick={async () => {
105 const data = await fetchData({ data: 'test query' })
106 addResult(`Fetched: ${JSON.stringify(data)}`)
107 }}
108 style={{
109 padding: '12px 24px',
110 fontSize: '14px',
111 borderRadius: '8px',
112 border: 'none',
113 background: '#10b981',
114 color: 'white',
115 cursor: 'pointer',
116 fontWeight: 600,
117 }}
118 >
119 Call fetchData()
120 </button>
121 </div>
122​
123 <div
124 style={{
125 padding: '16px',
126 borderRadius: '8px',
127 background: '#f8fafc',
128 border: '1px solid #e2e8f0',
129 marginBottom: '24px',
130 }}
131 >
132 <div
133 style={{ fontSize: '12px', color: '#94a3b8', marginBottom: '4px' }}
134 >
135 Loader data (also emits server event on navigation):
136 </div>
137 <code style={{ fontSize: '13px' }}>{JSON.stringify(loaderData)}</code>
138 </div>
139​
140 {results.length > 0 && (
141 <div>
142 <h3 style={{ fontSize: '16px', marginBottom: '8px' }}>
143 Server responses:
144 </h3>
145 <div
146 style={{
147 fontFamily: 'monospace',
148 fontSize: '13px',
149 background: '#1e293b',
150 color: '#e2e8f0',
151 padding: '16px',
152 borderRadius: '8px',
153 maxHeight: '300px',
154 overflow: 'auto',
155 }}
156 >
157 {results.map((r, i) => (
158 <div key={i} style={{ marginBottom: '4px' }}>
159 {r}
160 </div>
161 ))}
162 </div>
163 </div>
164 )}
165 </div>
166 )