rustclan
rustclan2y ago

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.
getGuilds: publicProcedure
.input(z.object({ accessToken: z.string().nullish() }))
.query(async (ctx) => {
console.log(1);
if (!ctx.input.accessToken) return null;
const botGuilds = await prisma.guilds.findMany();
const userGuilds = await getUserGuilds(ctx.input.accessToken);
if (!userGuilds || userGuilds.length <= 0) return [];

const guilds = userGuilds.map((g) => {
const findGuild = botGuilds.find((bg) => bg.guildId === g.id);
g.isPremium = findGuild?.premium || false;
g.inServer = !!findGuild;
return g;
});

return guilds.sort((a, b) => {
if (a.inServer && !b.inServer) return -1;
return 1;
});
}),
getGuilds: publicProcedure
.input(z.object({ accessToken: z.string().nullish() }))
.query(async (ctx) => {
console.log(1);
if (!ctx.input.accessToken) return null;
const botGuilds = await prisma.guilds.findMany();
const userGuilds = await getUserGuilds(ctx.input.accessToken);
if (!userGuilds || userGuilds.length <= 0) return [];

const guilds = userGuilds.map((g) => {
const findGuild = botGuilds.find((bg) => bg.guildId === g.id);
g.isPremium = findGuild?.premium || false;
g.inServer = !!findGuild;
return g;
});

return guilds.sort((a, b) => {
if (a.inServer && !b.inServer) return -1;
return 1;
});
}),
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
rustclan
rustclanOP2y ago
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.
Rhys
Rhys2y ago
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
Rhys
Rhys2y ago
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
rustclan
rustclanOP2y ago
Damn... okay. Thank you very much for the information. Much appreciated 🙂 I like your z_user_schema.parse(data.data); function. Very useful.
Rhys
Rhys2y ago
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
Rhys
Rhys2y ago
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&#39;t want to lose Discord API access by exceeding ratelimits so I need to store data in Redis Ideal solution or implementation Store server...
rustclan
rustclanOP2y ago
Awesome! I'm having a look into redis as we speak. Never used it before though, we'll see what happens!

Did you find this page helpful?