tRPC Query being called client side even with dehydrated state

I have a trpc v10 prefetch in gssp, it's being passed in correctly and hydrated properly, but the query is still being called client side
5 Replies
ryanagillie
ryanagillieOP•3y ago
Can't figure out if this is intentional, but I was under the impression the fetch wouldn't be called client side if it was dehydrated simple setup
const exampleRouter = t.router({
hello: t.procedure
.input(
z.object({
text: z.string().nullish(),
}).nullish(),
)
.query(({ input }) => ({
greeting: `hello: ${input?.text ?? 'world'}`,
})),
});
export default exampleRouter;
const exampleRouter = t.router({
hello: t.procedure
.input(
z.object({
text: z.string().nullish(),
}).nullish(),
)
.query(({ input }) => ({
greeting: `hello: ${input?.text ?? 'world'}`,
})),
});
export default exampleRouter;
const ssrHelper = (ctx: GetServerSidePropsContext) => createProxySSGHelpers({
router: appRouter,
ctx: createContext(ctx),
transformer: superjson,
});
export default ssrHelper;
const ssrHelper = (ctx: GetServerSidePropsContext) => createProxySSGHelpers({
router: appRouter,
ctx: createContext(ctx),
transformer: superjson,
});
export default ssrHelper;
const Home: NextPage = () => {
const { data, status } = trpc.example.hello.useQuery();
console.log(data, status);

return (
<h1>{data!.greeting}</h1>
)
}
export default Home;

export const getServerSideProps: GetServerSideProps = async (ctx) => {
const ssr = ssrHelper(ctx);
await ssr.example.hello.prefetch();

return {
props: {
trpcState: ssr.dehydrate(),
},
};
};
const Home: NextPage = () => {
const { data, status } = trpc.example.hello.useQuery();
console.log(data, status);

return (
<h1>{data!.greeting}</h1>
)
}
export default Home;

export const getServerSideProps: GetServerSideProps = async (ctx) => {
const ssr = ssrHelper(ctx);
await ssr.example.hello.prefetch();

return {
props: {
trpcState: ssr.dehydrate(),
},
};
};
ryanagillie
ryanagillieOP•3y ago
ryanagillie
ryanagillieOP•3y ago
status is never "loading", so it's being passed in correctly just not sure why it's being called client side as well unless its some kind of cache validation thing
TkDodo 🔮
TkDodo 🔮•3y ago
SSR | TanStack Query Docs
React Query supports two ways of prefetching data on the server and passing that to the queryClient. Prefetch the data yourself and pass it in as initialData
ryanagillie
ryanagillieOP•3y ago
Amazing, thank you for finding this!
Want results from more Discord servers?
Add your server