Skip to content

Commit a27db7c

Browse files
committed
fix: update review query to use unstable_cached
1 parent 942e591 commit a27db7c

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

core/app/[locale]/(default)/product/[slug]/_components/reviews.tsx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client';
2+
import { unstable_cache } from 'next/cache';
23
import { getFormatter, getTranslations } from 'next-intl/server';
34
import { createLoader, parseAsString, SearchParams } from 'nuqs/server';
45
import { cache } from 'react';
@@ -64,18 +65,48 @@ const ReviewsQuery = graphql(
6465
[ProductReviewSchemaFragment, PaginationFragment],
6566
);
6667

67-
const getReviews = cache(async (productId: number, paginationArgs: object) => {
68-
const { data } = await client.fetch({
69-
document: ReviewsQuery,
70-
variables: { ...paginationArgs, entityId: productId },
71-
fetchOptions: { next: { revalidate } },
72-
});
68+
const getCachedReviews = unstable_cache(
69+
async (locale: string, productId: number, paginationArgs: object) => {
70+
const { data } = await client.fetch({
71+
document: ReviewsQuery,
72+
variables: { ...paginationArgs, entityId: productId },
73+
locale,
74+
fetchOptions: { next: { revalidate } },
75+
});
7376

74-
return data.site.product;
75-
});
77+
return data.site.product;
78+
},
79+
['get-reviews'],
80+
{ revalidate },
81+
);
82+
83+
const getReviews = cache(
84+
async (
85+
locale: string,
86+
productId: number,
87+
paginationArgs: object,
88+
customerAccessToken?: string,
89+
) => {
90+
if (customerAccessToken) {
91+
const { data } = await client.fetch({
92+
document: ReviewsQuery,
93+
variables: { ...paginationArgs, entityId: productId },
94+
customerAccessToken,
95+
locale,
96+
fetchOptions: { cache: 'no-store' },
97+
});
98+
99+
return data.site.product;
100+
}
101+
102+
return getCachedReviews(locale, productId, paginationArgs);
103+
},
104+
);
76105

77106
interface Props {
78107
productId: number;
108+
locale: string;
109+
customerAccessToken?: string;
79110
searchParams: Promise<SearchParams>;
80111
streamableImages: Streamable<{
81112
images: Array<{ src: string; alt: string }>;
@@ -87,6 +118,8 @@ interface Props {
87118

88119
export const Reviews = async ({
89120
productId,
121+
locale,
122+
customerAccessToken,
90123
searchParams,
91124
streamableProduct,
92125
streamableImages,
@@ -103,7 +136,7 @@ export const Reviews = async ({
103136
} = paginationSearchParams;
104137
const paginationArgs = before == null ? { first: 5, after } : { last: 5, before };
105138

106-
return getReviews(productId, paginationArgs);
139+
return getReviews(locale, productId, paginationArgs, customerAccessToken);
107140
});
108141

109142
const streamableReviews = Streamable.from(async () => {

core/app/[locale]/(default)/product/[slug]/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ export default async function Product({ params, searchParams }: Props) {
613613
{showRating && (
614614
<div id="reviews">
615615
<Reviews
616+
customerAccessToken={customerAccessToken}
617+
locale={locale}
616618
productId={productId}
617619
recaptchaSiteKey={recaptchaSiteKey}
618620
searchParams={searchParams}

0 commit comments

Comments
 (0)