Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
75 replies
Johnson

ssg doesn't work for new data

When a user creates a new post, it doesn't generate a new site. I thought I could achieve that through revalidate but it doesn't seem to work. This is the code of post/[id].tsx:
export const getStaticPaths: GetStaticPaths = async () => {
  const posts = await prisma.post.findMany();
  // TODO: this can cause problems if we run this the first time the database is reset
  if (posts) {
    return {
      paths: posts.map((post) => ({
        params: {
          id: post.id,
        },
      })),
      fallback: false,
    };
  } else {
    return {
      paths: [],
      fallback: false,
    };
  }
};

export const getStaticProps = async (
  // this is the id from the page
  context: GetStaticPropsContext<{ id: string }>
) => {
  const ssg = createProxySSGHelpers({
    router: appRouter,
    transformer: superjson,
    ctx: await createContextInner({ session: null }),
  });

  await ssg.post.getAllPosts.prefetch();

  return {
    props: {
      trpcState: ssg.dehydrate(),
      id: context.params?.id,
    },
    revalidate: 1,
  };
};

It works in dev mode (due to dev mode), but it doesn't work in production (vercel)
Was this page helpful?