Connect, collaborate, and grow with a community of TypeScript developers
feat: try cache() (#229)
Tom Lienard8ea4161086b6
diff --git a/src/app/challenge/[id]/getChallengeRouteData.ts b/src/app/challenge/[id]/getChallengeRouteData.tsindex 01ad01dc..69e84d92 100644--- a/src/app/challenge/[id]/getChallengeRouteData.ts+++ b/src/app/challenge/[id]/getChallengeRouteData.ts@@ -1,11 +1,12 @@'use server';import { type Session } from 'next-auth';+import { cache } from 'react';import { prisma } from '~/server/db';export type ChallengeRouteData = NonNullable<Awaited<ReturnType<typeof getChallengeRouteData>>>;// this is to data to populate the description tab (default tab on challenge page)-export async function getChallengeRouteData(id: string, session: Session | null) {+export const getChallengeRouteData = cache(async (id: string, session: Session | null) => {const challenge = await prisma.challenge.findFirst({where: { id: +id },include: {@@ -37,4 +38,4 @@ export async function getChallengeRouteData(id: string, session: Session | null)});return challenge;-}+});diff --git a/src/app/challenge/[id]/submissions/page.tsx b/src/app/challenge/[id]/submissions/page.tsxindex 3c5f9a0e..9fb46f57 100644--- a/src/app/challenge/[id]/submissions/page.tsx+++ b/src/app/challenge/[id]/submissions/page.tsx@@ -1,4 +1,5 @@import { notFound } from 'next/navigation';+import { cache } from 'react';import { Submissions } from '~/components/challenge/submissions';import { getServerAuthSession } from '~/server/auth';import { prisma } from '~/server/db';@@ -21,7 +22,7 @@ export default async function SubmissionPage({ params: { id } }: Props) {}export type ChallengeSubmissions = NonNullable<Awaited<ReturnType<typeof getChallengeSubmissions>>>;-export async function getChallengeSubmissions(userId: string, challengeId: string) {+export const getChallengeSubmissions = cache(async (userId: string, challengeId: string) => {const solutions = await prisma.submission.findMany({where: { challengeId: +challengeId, userId },orderBy: [@@ -32,4 +33,4 @@ export async function getChallengeSubmissions(userId: string, challengeId: strin});return solutions;-}+});diff --git a/src/components/challenge/description/index.tsx b/src/components/challenge/description/index.tsxindex 609f9148..0604b002 100644--- a/src/components/challenge/description/index.tsx+++ b/src/components/challenge/description/index.tsx@@ -72,7 +72,6 @@ export function Description({ challenge }: Props) {try {await addOrRemoveBookmark(challengeId, userId, shouldBookmark);setHasBookmarked(shouldBookmark);- router.refresh();} catch (e) {console.error(e);// it errored so reverse the intended changes