Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

⚡️ Handle 404 in prefetch (#2219)

Luke Velladaca222252ce
diff --git a/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/layout.tsx b/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/layout.tsx
index 315eae3c0..d5647c919 100644
--- a/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/layout.tsx
+++ b/apps/web/src/app/[locale]/(app)/(optional-space)/poll/[urlId]/layout.tsx
@@ -1,19 +1,12 @@
import { prisma } from "@rallly/database";
import { dehydrate, HydrationBoundary } from "@tanstack/react-query";
+import { TRPCError } from "@trpc/server";
import type { Metadata } from "next";
import { notFound, redirect } from "next/navigation";
-import { cache } from "react";
import { PollLayout } from "@/components/layouts/poll-layout";
import { createAuthenticatedSSRHelper } from "@/trpc/server/create-ssr-helper";
-const getPoll = cache((urlId: string) =>
- prisma.poll.findUnique({
- where: { id: urlId },
- select: { title: true },
- }),
-);
-
export default async function Layout(
props: React.PropsWithChildren<{ params: Promise<{ urlId: string }> }>,
) {
@@ -23,15 +16,13 @@ export default async function Layout(
const trpc = await createAuthenticatedSSRHelper();
- const p = await getPoll(params.urlId);
-
- if (!p) {
- notFound();
- }
-
- // Prefetch all queries used in PollLayout
const [poll] = await Promise.all([
- trpc.polls.get.fetch({ urlId: params.urlId }),
+ trpc.polls.get.fetch({ urlId: params.urlId }).catch((e) => {
+ if (e instanceof TRPCError && e.code === "NOT_FOUND") {
+ notFound();
+ }
+ throw e;
+ }),
trpc.polls.participants.list.prefetch({ pollId: params.urlId }),
trpc.polls.getWatchers.prefetch({ pollId: params.urlId }),
trpc.polls.comments.list.prefetch({ pollId: params.urlId }),
@@ -52,7 +43,10 @@ export async function generateMetadata(props: {
params: Promise<{ locale: string; urlId: string }>;
}): Promise<Metadata> {
const params = await props.params;
- const poll = await getPoll(params.urlId);
+ const poll = await prisma.poll.findUnique({
+ where: { id: params.urlId },
+ select: { title: true },
+ });
if (!poll) {
notFound();