trpc query running twice
Hey guys. I have a trpc endpoint which makes a request to the discord api to fetch a list of the users guilds.
It works fine. However, because this is in my dev environment the TRPC query runs two times, which causes the request to fail due to discord ratelimits. I am using nextjs and I am unsure how I can resolve this.
Up until now, I have always used my own api on my server using python, so I could cache things. But with vercel you cannot cache results. Does anybody have any suggestions?
7 Replies
Technically, i could make it so the queries only run once in dev. But, this feature is enabled by default for a reason. Also, doing that is just a band aid fix. What if the user refreshes the page twice? Then my app breaks for a few second and the user gets greeted with a error page. Even though they have already recieved the data from the first request.
For dev here is how I do it:
https://github.com/AnswerOverflow/AnswerOverflow/blob/main/packages/auth/src/discord-oauth.ts
I just create a variable to cache the user servers and that says loaded in memory
For production, I’d you want to deploy serverlessly then you need to use something like Redis to cache the data or deploy on a normal server
GitHub
AnswerOverflow/discord-oauth.ts at main · AnswerOverflow/AnswerOver...
Indexing Discord Help Channel Questions into Google - AnswerOverflow/discord-oauth.ts at main · AnswerOverflow/AnswerOverflow
If you bring that dev caching approach to prod, you’re also gonna need to use a proper cache that pops off data once it gets too large
Damn... okay. Thank you very much for the information. Much appreciated 🙂
I like your
z_user_schema.parse(data.data);
function. Very useful.thank copilot not me
I'm probably going to end up doing the Redis approach that I described so you can watch this repository for when I implement that to use it as a reference
https://github.com/AnswerOverflow/AnswerOverflow/issues/114 this is the github issue you can watch to be closed
GitHub
Store Discord Servers In Redis · Issue #114 · AnswerOverflow/Answer...
What is this for? core Feature As a developer, I don't want to lose Discord API access by exceeding ratelimits so I need to store data in Redis Ideal solution or implementation Store server...
Awesome! I'm having a look into redis as we speak. Never used it before though, we'll see what happens!