I'm taking advantage of Next.JS SSG to improve the loading speed of my website. The point is I need some data to be fetched client-side, as it belongs to the user logged-in.
Let's say we have a YouTube-like website, so we would have the Video page
and a sidebar component, which would be the VideoRelated
:
I'd generate the Video page
static, by using the VideoQuery
at getStaticProps
. It would also fetch (client-side) the completion status of those for the user logged-in, so it would be something like:
export const VideoRelatedList = ({ videoId, relatedVideos }) => {
const { data } = useViewerVideoStatusQuery({ variables: { videoId } });
const relatedVideosViewerStatus = data?.videos;
const videos = React.useMemo(() => {
if (!relatedVideosViewerStatus) return relatedVideos;
return relatedVideos.map((relatedVideo, index) => {
const viewerHasCompleted = relatedVideosViewerStatus[index]?.id === relatedVideo.id && relatedVideosViewerStatus[index]?.viewerHasCompleted;
return { ...relatedVideo, viewerHasCompleted };
});
}, [relatedVideosViewerStatus, relatedVideos]);
return (
<ol>
{videos.map(({ id, name, viewerHasCompleted }, index) => (
<li key={id}>
{name} - {viewerHasCompleted && 'COMPLETED!'}
</li>
))}
</ol>
);
};
What is the best way to combine both data?
Currently, what I'm doing is combining both by using React.memo
but I'm not sure if this is a best practice or if there is a better way of achieving what I want.
from Next.JS Static site generation and client-side Apollo fetching
No comments:
Post a Comment