is action call cached automatically?
Answered
Dutch Smoushond posted this in #help-forum
Dutch SmoushondOP
I have a page, where I am fetching data from a server action once to get metadata details and then also call the same action and pass it to a component
Right now the action is being hit twice how can i optimize this?
Right now the action is being hit twice how can i optimize this?
export async function generateMetadata(
{ params }: { params: { roomId: string } },
parent: ResolvingMetadata,
): Promise<Metadata> {
const room = await getRoomDetailsAction(params.roomId[0]);
return {
title: room && room.name ? `${room.name}` : 'Room',
};
}
export default async function Page({ params }: Props) {
const room = await getRoomDetailsAction(params.roomId[0]);
if (room.error) {
return <div>Room not found</div>;
}
return (
<RoomPageClient
room={room}
/>
);
}
Answered by B33fb0n3
You can put it in a
React.cache(() => { your code here })
to let it run only once per request3 Replies
@Dutch Smoushond I have a page, where I am fetching data from a server action once to get metadata details and then also call the same action and pass it to a component
Right now the action is being hit twice how can i optimize this?
ts
export async function generateMetadata(
{ params }: { params: { roomId: string } },
parent: ResolvingMetadata,
): Promise<Metadata> {
const room = await getRoomDetailsAction(params.roomId[0]);
return {
title: room && room.name ? `${room.name}` : 'Room',
};
}
export default async function Page({ params }: Props) {
const room = await getRoomDetailsAction(params.roomId[0]);
if (room.error) {
return <div>Room not found</div>;
}
return (
<RoomPageClient
room={room}
/>
);
}
You can put it in a
React.cache(() => { your code here })
to let it run only once per requestAnswer
@B33fb0n3 You can put it in a `React.cache(() => { your code here })` to let it run only once per request
Dutch SmoushondOP
works, thankyou
sure thing