How to defer with call to Prisma in loader #9014
-
Hi All I am trying to defer loading a large(ish) dataset into my page with This works without defer but the page takes ages to appear due to the amount of data coming back into users:
.... So I want to use defer and populate the datagrid when the users come back from defer, but this never returns any data:
I have read somewhere that Prisma doesn't really return a promise correctly (not sure how true that is) but I have even tried this without success:
even
I am sure I am just using defer with Prisma incorrectly, but the "user" in useLoaderData is aways undefined when using defer. Thanks in advance as always. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
As you point out,
export async function loader() {
const users = prisma.user.findMany().then();
return defer({ users });
}
export default function Component() {
const { users } = useLoaderData<typeof loader>();
return (
<>
<h2>Deferred Data</h2>
<Suspense fallback={<div>Loading...</div>}>
<Await resolve={users}>
{(users) => <pre>{JSON.stringify(users, null, 2)}</pre>}
</Await>
</Suspense>
</>
);
} |
Beta Was this translation helpful? Give feedback.
Not sure I follow.
defer
uses<Suspense>
to render the fallback until the promise is resolved (ALL the data is streamed down). It doesn't chunk it up.You can use an empty grid skeleton as your fallback, return a subset of users, and perhaps perform some client fetches to request more data.