11import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client' ;
2+ import { unstable_cache } from 'next/cache' ;
23import { getFormatter , getTranslations } from 'next-intl/server' ;
34import { createLoader , parseAsString , SearchParams } from 'nuqs/server' ;
45import { 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
77106interface 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
88119export 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 ( ) => {
0 commit comments