Suspense doesn't work in App router
Answered
Himalayan posted this in #help-forum
HimalayanOP
I am trying to implement very basic data fetching pattern where server streams in data using a suspense boundary but it fails in build. This works perfectly fine in development environment but the build fails as component is returning a promise which doesn't make sense because it is already handled by suspense boundary wrapping it. please let me know if this is a bug or just me being dumb.
async function Titles ({ UserID }: { UserID: string }){
const titles = await await prisma.messageGroup.findMany({
where:{userId: UserID,}})
return (
<>
<div className="flex flex-col mt-2">
{titles.map((title, index) =>
<Link key={index} className="text-purple-500 mt-4" href={`/chat/${title.id}`}>{title.Title}</Link>
)}
</div>
</>
)
}
export default async function ChatLayout(props: {
children: React.ReactNode,
}){
const session = await getServerSession(authConfig);
if (!session) {
return (
<>
<div className="pt-16 min-h-screen">
<NotAllowedChat />
</div>
</>
)
}
return (
<>
<div className="pt-16 min-h-screen flex">
<div className="w-1/6 flex flex-col items-center">
<Sidebar session={session}/>
<Suspense fallback={<p>Loading...</p>}>
<Titles UserID={session.user.id}/>
</Suspense>
</div>
<GoodDivider />
{props.children}
</div>
</>
);
}14 Replies
try updating typescript versino and @types/react if so
Answer
@aardani try updating typescript versino and @types/react if so
HimalayanOP
Yes, it says that Titles returns a Promise<Element> instead of returning a jsx element
Yes, those are fixed in the latest typescript version
HimalayanOP
Thank you so much, I’ll implement it
@aardani Build fails as in, it gives you an ESLint error?
actually its a typescript error
Right. My bad
@Himalayan I am trying to implement very basic data fetching pattern where server streams in data using a suspense boundary but it fails in build. This works perfectly fine in development environment but the build fails as component is returning a promise which doesn't make sense because it is already handled by suspense boundary wrapping it. please let me know if this is a bug or just me being dumb. tsx
async function Titles ({ UserID }: { UserID: string }){
const titles = await await prisma.messageGroup.findMany({
where:{userId: UserID,}})
return (
<>
<div className="flex flex-col mt-2">
{titles.map((title, index) =>
<Link key={index} className="text-purple-500 mt-4" href={`/chat/${title.id}`}>{title.Title}</Link>
)}
</div>
</>
)
}
export default async function ChatLayout(props: {
children: React.ReactNode,
}){
const session = await getServerSession(authConfig);
if (!session) {
return (
<>
<div className="pt-16 min-h-screen">
<NotAllowedChat />
</div>
</>
)
}
return (
<>
<div className="pt-16 min-h-screen flex">
<div className="w-1/6 flex flex-col items-center">
<Sidebar session={session}/>
<Suspense fallback={<p>Loading...</p>}>
<Titles UserID={session.user.id}/>
</Suspense>
</div>
<GoodDivider />
{props.children}
</div>
</>
);
}
New Zealand Heading Dog
I'm not sure if this matters or not, or if it's just a typo here but not in your code, but there are 2 "await" calls on the line that begins with:
const titles = ...Just thought I'd let you know, just incase it might be causing an error of some sort.@New Zealand Heading Dog I'm not sure if this matters or not, or if it's just a typo here but not in your code, but there are 2 "await" calls on the line that begins with: const titles = ...Just thought I'd let you know, just incase it might be causing an error of some sort.
HimalayanOP
thanks for the heads up
@joulev actually its a typescript error
HimalayanOP
the main issue this lol
I upgraded typescript and @types/react to --latest flag and the errors went away
thank you so much guys
sometimes these errors gets frustrating
thanks for helping