Skip to content

Commit 26b05d7

Browse files
committed
feat(vite): cache rpc response
1 parent e02d632 commit 26b05d7

File tree

3 files changed

+43
-188
lines changed

3 files changed

+43
-188
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const connectionState = reactive<{
1414
})
1515

1616
const rpc = shallowRef<BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>>(undefined!)
17+
const responseCacheMap = new Map<string, unknown>()
1718

1819
export async function connect() {
1920
const runtimeConfig = useRuntimeConfig()
@@ -40,6 +41,15 @@ export async function connect() {
4041
connectionState.error = e
4142
console.error(`[vite-devtools] RPC error on executing "${name}":`)
4243
},
44+
onRequest: async (req, next, resolve) => {
45+
const cacheKey = `${req.m}-${JSON.stringify(req.a)}`
46+
if (responseCacheMap.has(cacheKey)) {
47+
resolve(responseCacheMap.get(cacheKey))
48+
}
49+
else {
50+
responseCacheMap.set(cacheKey, await next(req))
51+
}
52+
},
4353
},
4454
})
4555

0 commit comments

Comments
 (0)