rustclanR
tRPC3y ago
9 replies
rustclan

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;
      });
    }),

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?
Was this page helpful?