tRPCttRPC
Powered by
fossfighterF
tRPC•3y ago•
2 replies
fossfighter

How to properly check the contents of prefetched data?

I have dynamic route with SSG and if coming product slug is not in db I want to return notFound: true after prefetching. How to do it the right way? Right now I'm doing it like this but it looks like hack

export const getStaticProps: GetStaticProps = async (ctx) => {
  const ssg = await createServerSideHelpers({
    router: appRouter,
    ctx: await createContext(),
    transformer: superjson,
  });

  await ssg.public.product.prefetch({
    slug: ctx.params?.slug as string,
  });

  const product = ssg.dehydrate().json.queries[0].state.data;

  if (!product) {
    return {
      notFound: true,
    };
  }

  return {
    props: {
      trpcState: ssg.dehydrate(),
      slug: ctx.params?.slug,
    },
  };
};
export const getStaticProps: GetStaticProps = async (ctx) => {
  const ssg = await createServerSideHelpers({
    router: appRouter,
    ctx: await createContext(),
    transformer: superjson,
  });

  await ssg.public.product.prefetch({
    slug: ctx.params?.slug as string,
  });

  const product = ssg.dehydrate().json.queries[0].state.data;

  if (!product) {
    return {
      notFound: true,
    };
  }

  return {
    props: {
      trpcState: ssg.dehydrate(),
      slug: ctx.params?.slug,
    },
  };
};
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to check if data is being prefetched?
lukasLlukas / ❓-help
3y ago
How to get the Request Body data of QStash
Rasel HossainRRasel Hossain / ❓-help
2y ago
How to properly handle errors on the client
KamilKKamil / ❓-help
9mo ago
Reduce `isPending` time for prefetched Queries?
JordiJJordi / ❓-help
2y ago