How to pass fetched data to client components without streaming when cacheComponents is enabled? #88058
-
SummaryHello. I'm fetching and caching data in a function using the "use cache" directive. I'm calling and awaiting the function in a server component, but when I want to pass the fetched data to a client component, it wants me to wrap the client component in a Suspense tag, but I don't want to stream the fetched data; I want it to be prerendered and be interactive at the client side. Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Suspense is just the mechanism React uses when something can suspend. Make sure you pass plain serializable data (no Promises, no class instances, no Dates without conversion, no BigInt, no Response objects). Server component: import ClientView from "./ClientView";
export default async function Page() {
const data = await getData(); // uses "use cache" inside getData
return <ClientView data={data} />;
}Client component: "use client";
export default function ClientView({ data }: { data: any }) {
// fully interactive, no Suspense needed
return <pre>{JSON.stringify(data, null, 2)}</pre>;
}If Next still asks for Suspense, it’s a signal that normalize it: const json = await res.json();
return JSON.parse(JSON.stringify(json)); // brute-force normalization |
Beta Was this translation helpful? Give feedback.
Hi @AmirHosseinNP
<Suspense>does not automatically mean streaming.Suspense is just the mechanism React uses when something can suspend.
If your page is pre-rendered (SSG) or server-rendered without streaming, Suspense can still be present and your output can still be fully prerendered HTML. The fallback is only meaningful if something actually suspends at runtime.
Make sure you pass plain serializable data (no Promises, no class instances, no Dates without conversion, no BigInt, no Response objects).
Server component: