Skip to content

Commit c7f6c6e

Browse files
authored
feat: cache rpc responses for cacheable functions (#290)
1 parent af23df7 commit c7f6c6e

15 files changed

+50
-3
lines changed

packages/core/src/client/webcomponents/state/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export async function createDocksContext(
244244
return getWhenContext()
245245
},
246246
},
247-
rpc,
247+
rpc: markRaw(rpc),
248248
clientType,
249249
})
250250

packages/core/src/node/rpc/internal/rpc-server-list.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const rpcServerList = defineRpcFunction({
1111
Array.from(context.rpc.definitions.entries())
1212
.map(([name, fn]) => [name, {
1313
type: fn.type,
14+
cacheable: fn.cacheable || false,
1415
}]),
1516
)
1617
},

packages/kit/src/client/rpc.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { RpcCacheOptions } from '@vitejs/devtools-rpc'
12
import type { WebSocketRpcClientOptions } from '@vitejs/devtools-rpc/presets/ws/client'
23
import type { BirpcOptions, BirpcReturn } from 'birpc'
34
import type { ConnectionMeta, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, EventEmitter, RpcSharedStateHost } from '../types'
45
import type { DevToolsClientRpcHost, DevToolsRpcContext, RpcClientEvents } from './docks'
5-
import { RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
6+
import { RpcCacheManager, RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
67
import {
78
DEVTOOLS_CONNECTION_META_FILENAME,
89
DEVTOOLS_MOUNT_PATH,
@@ -25,6 +26,7 @@ export interface DevToolsRpcClientOptions {
2526
authToken?: string
2627
wsOptions?: Partial<WebSocketRpcClientOptions>
2728
rpcOptions?: Partial<BirpcOptions<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions, boolean>>
29+
cacheOptions?: boolean | Partial<RpcCacheOptions>
2830
}
2931

3032
export type DevToolsRpcClientCall = BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>['$call']
@@ -86,6 +88,10 @@ export interface DevToolsRpcClient {
8688
* The shared state host
8789
*/
8890
sharedState: RpcSharedStateHost
91+
/**
92+
* The RPC cache manager
93+
*/
94+
cacheManager: RpcCacheManager
8995
}
9096

9197
export interface DevToolsRpcClientMode {
@@ -149,6 +155,7 @@ export async function getDevToolsRpcClient(
149155
const {
150156
baseURL = DEVTOOLS_MOUNT_PATH,
151157
rpcOptions = {},
158+
cacheOptions = false,
152159
} = options
153160
const events = createEventEmitter<RpcClientEvents>()
154161
const bases = Array.isArray(baseURL) ? baseURL : [baseURL]
@@ -188,6 +195,7 @@ export async function getDevToolsRpcClient(
188195
}
189196
}
190197

198+
const cacheManager = new RpcCacheManager({ functions: [], ...(typeof options.cacheOptions === 'object' ? options.cacheOptions : {}) })
191199
const context: DevToolsRpcContext = {
192200
rpc: undefined!,
193201
}
@@ -229,7 +237,25 @@ export async function getDevToolsRpcClient(
229237
connectionMeta,
230238
events,
231239
clientRpc,
232-
rpcOptions,
240+
rpcOptions: {
241+
...rpcOptions,
242+
async onRequest(req, next, resolve) {
243+
await rpcOptions.onRequest?.call(this, req, next, resolve)
244+
if (cacheOptions && cacheManager?.validate(req.m)) {
245+
const cached = cacheManager.cached(req.m, req.a)
246+
if (cached) {
247+
return resolve(cached)
248+
}
249+
else {
250+
const res = await next(req)
251+
cacheManager?.apply(req, res)
252+
}
253+
}
254+
else {
255+
await next(req)
256+
}
257+
},
258+
},
233259
wsOptions: options.wsOptions,
234260
})
235261

@@ -252,6 +278,7 @@ export async function getDevToolsRpcClient(
252278
callOptional: mode.callOptional,
253279
client: clientRpc,
254280
sharedState: undefined!,
281+
cacheManager,
255282
}
256283

257284
rpc.sharedState = createRpcSharedStateClientHost(rpc)

packages/rolldown/src/app/composables/rpc.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export async function connect() {
2424
DEVTOOLS_MOUNT_PATH,
2525
runtimeConfig.app.baseURL,
2626
],
27+
cacheOptions: true,
2728
connectionMeta: runtimeConfig.app.connection,
2829
wsOptions: {
2930
onConnected: () => {
@@ -48,6 +49,13 @@ export async function connect() {
4849
},
4950
})
5051

52+
const functions = await rpc.value.call('devtoolskit:internal:rpc:server:list')
53+
const cacheableFunctions = Object.keys(functions).filter(name => functions[name]?.cacheable)
54+
55+
rpc.value.cacheManager.updateOptions({
56+
functions: [...cacheableFunctions],
57+
})
58+
5159
connectionState.connected = true
5260
}
5361
catch (e) {

packages/rolldown/src/node/rpc/functions/rolldown-get-asset-details.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getLogsManager } from '../utils'
44
export const rolldownGetAssetDetails = defineRpcFunction({
55
name: 'vite:rolldown:get-asset-details',
66
type: 'query',
7+
cacheable: true,
78
setup: (context) => {
89
const manager = getLogsManager(context)
910
return {

packages/rolldown/src/node/rpc/functions/rolldown-get-assets-list.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getLogsManager } from '../utils'
44
export const rolldownGetAssetsList = defineRpcFunction({
55
name: 'vite:rolldown:get-assets-list',
66
type: 'query',
7+
cacheable: true,
78
setup: (context) => {
89
const manager = getLogsManager(context)
910
return {

packages/rolldown/src/node/rpc/functions/rolldown-get-chunk-info.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getLogsManager } from '../utils'
44
export const rolldownGetChunkInfo = defineRpcFunction({
55
name: 'vite:rolldown:get-chunk-info',
66
type: 'query',
7+
cacheable: true,
78
setup: (context) => {
89
const manager = getLogsManager(context)
910
return {

packages/rolldown/src/node/rpc/functions/rolldown-get-chunks-graph.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getLogsManager } from '../utils'
44
export const rolldownGetChunksGraph = defineRpcFunction({
55
name: 'vite:rolldown:get-chunks-graph',
66
type: 'query',
7+
cacheable: true,
78
setup: (context) => {
89
const manager = getLogsManager(context)
910
return {

packages/rolldown/src/node/rpc/functions/rolldown-get-module-info.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getLogsManager } from '../utils'
55
export const rolldownGetModuleInfo = defineRpcFunction({
66
name: 'vite:rolldown:get-module-info',
77
type: 'query',
8+
cacheable: true,
89
setup: (context) => {
910
const manager = getLogsManager(context)
1011
return {

packages/rolldown/src/node/rpc/functions/rolldown-get-package-details.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getPackagesManifest } from './rolldown-get-packages'
55
export const rolldownGetPackageDetails = defineRpcFunction({
66
name: 'vite:rolldown:get-package-details',
77
type: 'query',
8+
cacheable: true,
89
setup: (context) => {
910
const manager = getLogsManager(context)
1011
return {

0 commit comments

Comments
 (0)