CodingWithJamal
CodingWithJamal15mo ago

Disable trpc route cahce?

import { protectedProcedure, router } from "@/lib/trpc/server/trpc";
import { TRPCError } from "@trpc/server";

export const clientRouter = router({
get: protectedProcedure.query(async ({ ctx }) => {
const { id: user_id } = ctx.session.user!;

if (!user_id)
throw new TRPCError({
code: "NOT_FOUND",
message: "No user session found",
});

let { data: client, error } = await ctx.supabase
.from("clients")
.select("*")
.eq("user_id", user_id)
.single();

if (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error.message,
});
}

return client;
}),
});
import { protectedProcedure, router } from "@/lib/trpc/server/trpc";
import { TRPCError } from "@trpc/server";

export const clientRouter = router({
get: protectedProcedure.query(async ({ ctx }) => {
const { id: user_id } = ctx.session.user!;

if (!user_id)
throw new TRPCError({
code: "NOT_FOUND",
message: "No user session found",
});

let { data: client, error } = await ctx.supabase
.from("clients")
.select("*")
.eq("user_id", user_id)
.single();

if (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: error.message,
});
}

return client;
}),
});
I have this code to fetch some data from my db and return it, however if i call the endpoint again, the data is the same even if the db is different? Does trpc cache the old data on new request? If so i dont want this
5 Replies
CodingWithJamal
CodingWithJamalOP15mo ago
However when i go to http://localhost:3000/api/trpc/getClient and test i get the fresh data on every request?
Ahmed Elsakaan
Ahmed Elsakaan15mo ago
this is because of react-query’s caching if you set cacheTime to 0 in your useQuery call options, it won’t cache the response
CodingWithJamal
CodingWithJamalOP15mo ago
but im using
export const trpc = experimental_createTRPCNextAppDirServer<AppRouter>({
config() {
return {
transformer: superjson,
links: [
loggerLink({
enabled: () => process.env.NODE_ENV === "development",
}),
experimental_nextHttpLink({
batch: true,
url: getUrl(),
headers() {
return {
cookie: cookies().toString(),
"x-trpc-source": "rsc-http",
};
},
}),
],
};
},
});
export const trpc = experimental_createTRPCNextAppDirServer<AppRouter>({
config() {
return {
transformer: superjson,
links: [
loggerLink({
enabled: () => process.env.NODE_ENV === "development",
}),
experimental_nextHttpLink({
batch: true,
url: getUrl(),
headers() {
return {
cookie: cookies().toString(),
"x-trpc-source": "rsc-http",
};
},
}),
],
};
},
});
The Nextjs server trpc so it only has the query method on it
Ahmed Elsakaan
Ahmed Elsakaan15mo ago
hmm idk about that one tbh, @julius ? app router master
julius
julius15mo ago
The ”new”, experimental, nextHttpLink does set cache tags, so that’s why you’re seeing that behavior, it’s expected. You can set a revalidation time on how long you want the fetch request to be cached for in the link options, or just use the old/normal httpLink/httpBatchLink if you dont want any caching