Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Feature/cache optimizations (#644)

Shrikrishna Joisafd5698bedd88
diff --git a/apps/admin/src/app/_components/admin.actions.tsx b/apps/admin/src/app/_components/admin.actions.tsx
index 33e04aec..40c831e6 100644
--- a/apps/admin/src/app/_components/admin.actions.tsx
+++ b/apps/admin/src/app/_components/admin.actions.tsx
@@ -3,6 +3,7 @@
import { getServerAuthSession } from '@repo/auth/server';
import { prisma } from '@repo/db';
import { type Prisma, type Report } from '@repo/db/types';
+import { cache } from 'react';
// FML this was obnoxious to do
export type ChallengeInfo = Omit<Report, 'id' | 'status' | 'type' | 'userId'> & {
@@ -222,7 +223,7 @@ export async function unbanUser(userId: string) {
]);
}
-export async function getChallenge(id: number) {
+export const getChallenge = cache(async (id: number) => {
return prisma.challenge.findFirstOrThrow({
where: {
id,
@@ -242,7 +243,7 @@ export async function getChallenge(id: number) {
},
},
});
-}
+});
export type UploadedImages = Awaited<ReturnType<typeof getUploadedImages>>;
/**
diff --git a/apps/admin/src/app/challenge/[challengeId]/page.tsx b/apps/admin/src/app/challenge/[challengeId]/page.tsx
index 27a7b5e2..5038da82 100644
--- a/apps/admin/src/app/challenge/[challengeId]/page.tsx
+++ b/apps/admin/src/app/challenge/[challengeId]/page.tsx
@@ -1,5 +1,6 @@
import { prisma } from '@repo/db';
import { ChallengeReview } from './challenge-review';
+import { cache } from 'react';
interface Props {
params: {
@@ -13,7 +14,7 @@ export default async function ChallengeReviewPage({ params: { challengeId } }: P
}
export type ChallengeToReview = NonNullable<Awaited<ReturnType<typeof getChallengeToReview>>>;
-async function getChallengeToReview(id: number) {
+const getChallengeToReview = cache(async (id: number) => {
return prisma.challenge.findFirstOrThrow({
where: {
id,
@@ -29,4 +30,4 @@ async function getChallengeToReview(id: number) {
createdAt: 'desc',
},
});
-}
+});
diff --git a/apps/admin/src/app/report/[id]/_components/report/report.action.ts b/apps/admin/src/app/report/[id]/_components/report/report.action.ts
index 08c2bcfa..343d5dbe 100644
--- a/apps/admin/src/app/report/[id]/_components/report/report.action.ts
+++ b/apps/admin/src/app/report/[id]/_components/report/report.action.ts
@@ -1,6 +1,7 @@
'use server';
import { prisma } from '@repo/db';
+import { cache } from 'react';
export type ReportsData = Awaited<ReturnType<typeof getReports>>;
export type InfiniteReports = Awaited<ReturnType<typeof getInfiniteReports>>;
@@ -61,7 +62,7 @@ export async function getReports(lastCursor?: number, take = 3) {
});
}
-export async function getReportedUserInformation(userId: string) {
+export const getReportedUserInformation = cache(async (userId: string) => {
return await prisma.user.findFirstOrThrow({
where: {
id: userId,
@@ -101,4 +102,4 @@ export async function getReportedUserInformation(userId: string) {
},
},
});
-}
+});
diff --git a/apps/admin/src/app/report/[id]/_components/report/solution.report.tsx b/apps/admin/src/app/report/[id]/_components/report/solution.report.tsx
index d2b45727..4689636e 100644
--- a/apps/admin/src/app/report/[id]/_components/report/solution.report.tsx
+++ b/apps/admin/src/app/report/[id]/_components/report/solution.report.tsx
@@ -5,14 +5,15 @@ import { prisma } from '@repo/db';
import { Text } from '@repo/ui/components/typography/typography';
import { Markdown } from '@repo/ui/components/markdown';
import type { ReportWithInfo } from '../../report.action';
+import { cache } from 'react';
-async function getSolutionChallenge(challengeId: number) {
+const getSolutionChallenge = cache(async (challengeId: number) => {
return prisma.challenge.findFirstOrThrow({
where: {
id: challengeId,
},
});
-}
+});
export async function SolutionReport({ report }: { report: NonNullable<ReportWithInfo> }) {
if (report.type !== 'SOLUTION' || !report.solution) return null;
diff --git a/apps/admin/src/app/tracks/[trackId]/page.tsx b/apps/admin/src/app/tracks/[trackId]/page.tsx
index ed962869..d71ce1ba 100644
--- a/apps/admin/src/app/tracks/[trackId]/page.tsx
+++ b/apps/admin/src/app/tracks/[trackId]/page.tsx
@@ -1,5 +1,6 @@
import { prisma } from '@repo/db';
import { UpdateTrackForm } from './_components/update-track-form';
+import { cache } from 'react';
export interface Props {
params: {
@@ -15,7 +16,7 @@ export default async function (props: Props) {
}
export type TrackToManage = NonNullable<Awaited<ReturnType<typeof getTrackById>>>;
-function getTrackById(id: number) {
+const getTrackById = cache((id: number) => {
return prisma.track.findFirstOrThrow({
where: {
id,
@@ -24,7 +25,7 @@ function getTrackById(id: number) {
trackChallenges: true,
},
});
-}
+});
export type ChallengesForTrack = NonNullable<Awaited<ReturnType<typeof getChallenges>>>;
function getChallenges() {
diff --git a/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx b/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx
index 312a023e..0a8b4489 100644
--- a/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx
+++ b/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx
@@ -1,6 +1,7 @@
import { prisma } from '@repo/db';
import { SolutionDetails } from '../_components/solution-detail';
import { getServerAuthSession, type Session } from '@repo/auth/server';
+import { cache } from 'react';
interface Props {
params: {
@@ -36,7 +37,7 @@ export async function generateMetadata({ params: { solutionId } }: Props) {
};
}
-async function getSolution(solutionId: string, session: Session | null) {
+const getSolution = cache(async (solutionId: string, session: Session | null) => {
const solution = await prisma.sharedSolution.findFirstOrThrow({
where: {
id: Number(solutionId),
@@ -63,4 +64,4 @@ async function getSolution(solutionId: string, session: Session | null) {
});
return solution;
-}
+});
diff --git a/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx b/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx
index 5bc2080e..62ccc808 100644
--- a/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx
+++ b/apps/web/src/app/[locale]/challenge/[id]/solutions/page.tsx
@@ -5,6 +5,7 @@ import { notFound } from 'next/navigation';
import { Solutions } from './_components';
import { getChallengeRouteData } from '../getChallengeRouteData';
import { buildMetaForChallenge } from '~/app/metadata';
+import { cache } from 'react';
interface Props {
params: {
@@ -33,7 +34,7 @@ export default async function SolutionPage({ params: { id } }: Props) {
}
export type ChallengeSolutionsRouteData = NonNullable<Awaited<ReturnType<typeof getSolutionsData>>>;
-export async function getSolutionsData(challengeId: string, session: Session | null) {
+export const getSolutionsData = cache(async (challengeId: string, session: Session | null) => {
const data = await prisma.challenge.findFirst({
where: { id: Number(challengeId) },
select: {
@@ -71,4 +72,4 @@ export async function getSolutionsData(challengeId: string, session: Session | n
});
return data;
-}
+});
diff --git a/apps/web/src/app/[locale]/tracks/_components/track.action.ts b/apps/web/src/app/[locale]/tracks/_components/track.action.ts
index f039dfb0..732573ec 100644
--- a/apps/web/src/app/[locale]/tracks/_components/track.action.ts
+++ b/apps/web/src/app/[locale]/tracks/_components/track.action.ts
@@ -2,6 +2,7 @@
import { getServerAuthSession } from '@repo/auth/server';
import { prisma } from '@repo/db';
+import { cache } from 'react';
/**
* Enrolls the session user in the track given a track id.
@@ -47,7 +48,7 @@ export async function unenrollUserFromTrack(id: number) {
* Fetches the track details given a track id.
* @param id The track id.
*/
-export async function getTrackDetails(id: number) {
+export const getTrackDetails = cache(async (id: number) => {
const session = await getServerAuthSession();
return prisma.track.findUnique({
@@ -71,4 +72,4 @@ export async function getTrackDetails(id: number) {
},
},
});
-}
+});
diff --git a/apps/web/src/components/user/dashboard/index.tsx b/apps/web/src/components/user/dashboard/index.tsx
index ae37c67f..80fcf263 100644
--- a/apps/web/src/components/user/dashboard/index.tsx
+++ b/apps/web/src/components/user/dashboard/index.tsx
@@ -18,6 +18,7 @@ import { InProgressTab } from './in-progress-tab';
import { Overview } from './overview';
import { SolutionsTab } from './solutions-tab';
import UserHeader from './user-header';
+import { cache } from 'react';
interface Props {
// TODO: how do do this union type with just letting prisma halp
@@ -27,7 +28,7 @@ interface Props {
}
export type UserData = NonNullable<Awaited<ReturnType<typeof getUserdata>>>;
-async function getUserdata(id: string) {
+const getUserdata = cache(async (id: string) => {
const userData = await prisma.user.findFirst({
where: {
id,
@@ -51,7 +52,7 @@ async function getUserdata(id: string) {
});
return userData;
-}
+});
export async function Dashboard({ user }: Props) {
const userData = await getUserdata(user.id);