I'm trying to upgrade a codebase to use cache components. A common pattern in the code is to pass around a ctx argument with db client (regular/transaction), logger etc. These can not be serialised and thus can't be part of the arguments passed to a cache component. I tried to solve this by splitting it into a separate function but still run into this error
let count = 0;
class SomeClass {}
const wrapper = <T extends (...args: any[]) => Promise<any>>(
fn: (ctx: { someClass: SomeClass }) => T,
): T => fn({ someClass: new SomeClass() });
const getCount = wrapper((ctx) =>
// ^
// ⨯ Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.
async () => {
"use cache";
console.log(ctx.someClass);
count++;
return count;
},
);
export default async function CacheCompTestPage() {
const localCount = await getCount();
return (
<div>
<p>Count: {localCount}</p>
</div>
);
}
Is the only way around this to use async local storage?