Caching a middleware response during batching
Hey there, I do some authorisation checks in middleware (procedure) - the authorisation check is a little bit slow. I'm facing an issue that when the page initially loads, there is a significant amount of TRPC requests sent as a batch and although it's sent as a batch, each path still runs the procedure.
Is there a way to cache the procedure response for the same request, or use the same procedure output for multiple requests during a batch?
2 Replies
create context only runs once for the batch and can be used to store caches or just run some logic and pass the result
You could make a middleware that caches slow procedures
Or use the batch stream link to get rid of the waterfall
And if auth is slow and runs for all queries independently, you can create a lazy getter from the context that is only executed once for the request