Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: SSR optimize computation #539

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 26 additions & 31 deletions service/vspo-schedule/web/src/pages/schedule/[status].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useMemo } from "react";
import { Box, Tab, Tabs } from "@mui/material";
import { styled } from "@mui/material/styles";
import { GetServerSideProps } from "next";
Expand Down Expand Up @@ -37,11 +37,12 @@ type DateObject = {
};

type LivestreamsProps = {
livestreamsByDate: Record<string, Livestream[]>;
eventsByDate: Record<string, VspoEvent[]>;
livestreams: Livestream[];
events: VspoEvent[];
lastUpdateTimestamp: number;
liveStatus: string;
locale: string;
timeZone: string;
dateTabsInfo?: {
tabDates: DateObject[];
todayIndex: number;
Expand Down Expand Up @@ -71,12 +72,27 @@ const TabBox = styled(Box)(({ theme }) => ({
}));

const HomePage: NextPageWithLayout<LivestreamsProps> = ({
livestreamsByDate,
eventsByDate,
livestreams,
events,
dateTabsInfo,
timeZone,
}) => {
const router = useRouter();
const { t } = useTranslation("streams");

const livestreamsByDate = useMemo(() => {
return groupBy(livestreams, (livestream) => {
return formatDate(livestream.scheduledStartTime, "yyyy-MM-dd", {
timeZone,
});
});
}, [livestreams, timeZone]);
const eventsByDate = useMemo(() => {
return groupBy(events, (event) => {
return formatDate(event.startedAt, "yyyy-MM-dd", { timeZone });
});
}, [events, timeZone]);

if (router.isFallback) {
return <Loading />;
}
Expand Down Expand Up @@ -155,7 +171,7 @@ export const getServerSideProps: GetServerSideProps<
endedDate.setDate(endedDate.getDate() + 1);
const today = getCurrentUTCDate();

return await fetchLivestreams({
return fetchLivestreams({
limit: params.status === "archive" ? 300 : 50,
lang: locale,
status: params.status,
Expand All @@ -174,7 +190,7 @@ export const getServerSideProps: GetServerSideProps<

// Logic 2: Fetch events
const fetchEventsData = async () => {
return await fetchEvents({ lang: locale });
return fetchEvents({ lang: locale });
};

// Logic 3: Fetch translations and create metadata
Expand Down Expand Up @@ -320,33 +336,12 @@ export const getServerSideProps: GetServerSideProps<
.join(", ") ?? "";
const description = `${t("description")}\n${livestreamDescription}`;

const filteredLivestreams = uniqueLivestreams;

const livestreamsByDate = groupBy(filteredLivestreams, (livestream) => {
try {
return formatDate(livestream.scheduledStartTime, "yyyy-MM-dd", {
timeZone,
});
} catch (err) {
console.error("Invalid date:", livestream.scheduledStartTime);
throw err;
}
});

const eventsByDate = groupBy(events, (event) => {
try {
return formatDate(event.startedAt, "yyyy-MM-dd", { timeZone });
} catch (err) {
console.error("Invalid date:", event.startedAt);
throw err;
}
});

return {
props: {
...translations,
livestreamsByDate,
eventsByDate,
livestreams: uniqueLivestreams,
events,
timeZone,
lastUpdateTimestamp: getCurrentUTCDate().getTime(),
liveStatus: params.status,
locale: locale,
Expand Down