cement4357
cement4357
TTCTheo's Typesafe Cult
Created by gybia on 7/22/2023 in #questions
What is prefetching in TRPC server side?
I don't really think so, prefetch will add the query to the cache, which you then dehydrate and send to the client.
10 replies
TTCTheo's Typesafe Cult
Created by gybia on 7/22/2023 in #questions
What is prefetching in TRPC server side?
Yes, that's totally okay, just in your case, you want to use gSSP or gIP and prefetch ur query there
10 replies
TTCTheo's Typesafe Cult
Created by gybia on 7/22/2023 in #questions
What is prefetching in TRPC server side?
keep in mind that prefetching in your main page might be slower than calling your route staightaway with useQuery or however you name it
10 replies
TTCTheo's Typesafe Cult
Created by gybia on 7/22/2023 in #questions
What is prefetching in TRPC server side?
Prefetching is exactly what it sounds like to be, it is prefetching some kind of query, which you can call back later with prefetched data instead of calling query. It is meant to be used in serverside so ie. in exported function of getServerSideProps - an example:
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const ssg = createServerSideHelpers({
router: appRouter,
ctx: await createContext(),
transformer: SuperJSON,
});

const { page } = ctx.query || 1;

if (page && Number.isNaN(page)) {
return {
redirect: {
destination: '/error',
permanent: false,
},
};
}

await ssg.public.item.getItems.prefetchInfinite({
cursor: 0,
take: 10,
});

return {
props: {
trpcState: ssg.dehydrate(),
},
};
};
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const ssg = createServerSideHelpers({
router: appRouter,
ctx: await createContext(),
transformer: SuperJSON,
});

const { page } = ctx.query || 1;

if (page && Number.isNaN(page)) {
return {
redirect: {
destination: '/error',
permanent: false,
},
};
}

await ssg.public.item.getItems.prefetchInfinite({
cursor: 0,
take: 10,
});

return {
props: {
trpcState: ssg.dehydrate(),
},
};
};
and then you if you fetch the data in the component with exactly the same parameters, you will get prefetched data from gSSP instead of naked useQuery call ie.
trpc.public.item.getItems.useInfiniteQuery({
cursor: 0,
take: 10,
});
trpc.public.item.getItems.useInfiniteQuery({
cursor: 0,
take: 10,
});
not sure if it is gonna work if you are using 'use client' statement tho
10 replies
TTCTheo's Typesafe Cult
Created by cement4357 on 2/24/2023 in #questions
Prefetching more items than requested on client side
I wanted to create something like a step ahead of user - so one more query would be always prefetched
15 replies
TTCTheo's Typesafe Cult
Created by cement4357 on 2/24/2023 in #questions
Prefetching more items than requested on client side
Don't really see any prefetch extension on my components using trpc
15 replies
TTCTheo's Typesafe Cult
Created by cement4357 on 2/24/2023 in #questions
Prefetching more items than requested on client side
So far I was using regular client-side queries and it was pretty much very fast, you mentioned prefetching on the client-side - what exactly did you mean? Would be nice to prefetch more queries if previous one is already loaded!
15 replies
TTCTheo's Typesafe Cult
Created by cement4357 on 2/24/2023 in #questions
Prefetching more items than requested on client side
Yup, was thinking about most of the things with gssp, but prolly I don't even have enough cpu power on my nodes lol
15 replies
TTCTheo's Typesafe Cult
Created by cement4357 on 2/24/2023 in #questions
Prefetching more items than requested on client side
Ah, alright bro, thanks for quick response! I was also wondering about those performance issues prefetching too many items at once!
15 replies