You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/kit/rpc.md
+99-5Lines changed: 99 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,11 +60,12 @@ const plugin: Plugin = {
60
60
61
61
### Function Types
62
62
63
-
| Type | Description | Caching |
64
-
|------|-------------|---------|
65
-
|`query`| Fetch data, read operations | Can be cached |
66
-
|`action`| Side effects, mutations | Not cached |
67
-
|`static`| Constant data that never changes | Cached indefinitely |
63
+
| Type | Description | Caching | Dump Support |
64
+
|------|-------------|---------|--------------|
65
+
|`query`| Fetch data, read operations | Can be cached | ✓ (manual) |
66
+
|`static`| Constant data that never changes | Cached indefinitely | ✓ (automatic) |
67
+
|`action`| Side effects, mutations | Not cached | ✗ |
68
+
|`event`| Emit events, no response | Not cached | ✗ |
68
69
69
70
### Handler Arguments
70
71
@@ -104,6 +105,99 @@ setup: (ctx) => {
104
105
}
105
106
```
106
107
108
+
> [!IMPORTANT]
109
+
> For build mode compatibility, compute data in the setup function using the context rather than relying on runtime global state. This allows the dump feature to pre-compute results at build time.
110
+
111
+
### Dump Feature for Build Mode
112
+
113
+
When using `vite devtools build` to create a static DevTools build, the server cannot execute functions at runtime. The **dump feature** solves this by pre-computing RPC results at build time.
114
+
115
+
#### How It Works
116
+
117
+
1. At build time, `dumpFunctions()` executes your RPC handlers with predefined arguments
118
+
2. Results are stored in `.vdt-rpc-dump.json` in the build output
119
+
3. The static client reads from this JSON file instead of making live RPC calls
120
+
121
+
#### Static Functions (Recommended)
122
+
123
+
Functions with `type: 'static'` are **automatically dumped** with no arguments:
124
+
125
+
```ts
126
+
const getConfig =defineRpcFunction({
127
+
name: 'my-plugin:get-config',
128
+
type: 'static', // Auto-dumped with inputs: [[]]
129
+
setup: ctx=> ({
130
+
handler: async () => ({
131
+
root: ctx.viteConfig.root,
132
+
plugins: ctx.viteConfig.plugins.map(p=>p.name),
133
+
}),
134
+
}),
135
+
})
136
+
```
137
+
138
+
This works in both dev mode (live) and build mode (pre-computed).
139
+
140
+
#### Query Functions with Dumps
141
+
142
+
For `query` functions that need arguments, define `dump` in the setup:
inputs: moduleIds.map(id=> [id]), // Pre-compute for all modules
159
+
fallback: null, // Return null for unknown modules
160
+
},
161
+
}
162
+
},
163
+
})
164
+
```
165
+
166
+
#### Recommendations for Plugin Authors
167
+
168
+
To ensure your DevTools work in build mode:
169
+
170
+
1.**Prefer `type: 'static'`** for functions that return constant data
171
+
2.**Return context-based data in setup** rather than accessing global state in handlers
172
+
3.**Define dumps in setup function** for query functions that need pre-computation
173
+
4.**Use fallback values** for graceful degradation when arguments don't match
174
+
175
+
```ts
176
+
// ✓ Good: Returns static data, works in build mode
177
+
const getPluginInfo =defineRpcFunction({
178
+
name: 'my-plugin:info',
179
+
type: 'static',
180
+
setup: ctx=> ({
181
+
handler: async () => ({
182
+
version: '1.0.0',
183
+
root: ctx.viteConfig.root,
184
+
}),
185
+
}),
186
+
})
187
+
188
+
// ✗ Avoid: Depends on runtime server state
189
+
const getLiveMetrics =defineRpcFunction({
190
+
name: 'my-plugin:metrics',
191
+
type: 'query', // No dump - won't work in build mode
192
+
handler: async () => {
193
+
returngetCurrentMetrics() // Requires live server
194
+
},
195
+
})
196
+
```
197
+
198
+
> [!TIP]
199
+
> If your data genuinely needs live server state, use `type: 'query'` without dumps. The function will work in dev mode but gracefully fail in build mode.
0 commit comments