Skip to content

Commit b329bc6

Browse files
committed
feat: Remove caching on fetching product inventory data
1 parent 57a3527 commit b329bc6

3 files changed

Lines changed: 67 additions & 14 deletions

File tree

.changeset/eager-nails-bake.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@bigcommerce/catalyst-core": minor
3+
---
4+
5+
Fetch product inventory data with a separate GQL query with no caching
6+
7+
## Migration
8+
The files to be rebased for this change to be applied are:
9+
- core/app/[locale]/(default)/product/[slug]/page-data.ts
10+
- core/app/[locale]/(default)/product/[slug]/page.tsx

core/app/[locale]/(default)/product/[slug]/page-data.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export const getStreamableProductVariant = cache(
255255
document: StreamableProductVariantBySkuQuery,
256256
variables,
257257
customerAccessToken,
258-
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
258+
fetchOptions: { cache: 'no-store' },
259259
});
260260

261261
return data.site.product?.variants;
@@ -306,6 +306,36 @@ const StreamableProductQuery = graphql(
306306
minPurchaseQuantity
307307
maxPurchaseQuantity
308308
warranty
309+
...ProductViewedFragment
310+
...ProductSchemaFragment
311+
}
312+
}
313+
}
314+
`,
315+
[ProductViewedFragment, ProductSchemaFragment],
316+
);
317+
318+
type Variables = VariablesOf<typeof StreamableProductQuery>;
319+
320+
export const getStreamableProduct = cache(
321+
async (variables: Variables, customerAccessToken?: string) => {
322+
const { data } = await client.fetch({
323+
document: StreamableProductQuery,
324+
variables,
325+
customerAccessToken,
326+
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
327+
});
328+
329+
return data.site.product;
330+
},
331+
);
332+
333+
const StreamableProductInventoryQuery = graphql(
334+
`
335+
query StreamableProductInventoryQuery($entityId: Int!) {
336+
site {
337+
product(entityId: $entityId) {
338+
sku
309339
inventory {
310340
hasVariantInventory
311341
isInStock
@@ -320,25 +350,23 @@ const StreamableProductQuery = graphql(
320350
availabilityV2 {
321351
status
322352
}
323-
...ProductViewedFragment
324353
...ProductVariantsInventoryFragment
325-
...ProductSchemaFragment
326354
}
327355
}
328356
}
329357
`,
330-
[ProductViewedFragment, ProductSchemaFragment, ProductVariantsInventoryFragment],
358+
[ProductVariantsInventoryFragment],
331359
);
332360

333-
type Variables = VariablesOf<typeof StreamableProductQuery>;
361+
type ProductInventoryVariables = VariablesOf<typeof StreamableProductQuery>;
334362

335-
export const getStreamableProduct = cache(
336-
async (variables: Variables, customerAccessToken?: string) => {
363+
export const getStreamableProductInventory = cache(
364+
async (variables: ProductInventoryVariables, customerAccessToken?: string) => {
337365
const { data } = await client.fetch({
338-
document: StreamableProductQuery,
366+
document: StreamableProductInventoryQuery,
339367
variables,
340368
customerAccessToken,
341-
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
369+
fetchOptions: { cache: 'no-store' },
342370
});
343371

344372
return data.site.product;

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getProductPricingAndRelatedProducts,
2828
getStreamableInventorySettingsQuery,
2929
getStreamableProduct,
30+
getStreamableProductInventory,
3031
getStreamableProductVariant,
3132
} from './page-data';
3233

@@ -117,8 +118,22 @@ export default async function Product({ params, searchParams }: Props) {
117118

118119
const streamableProductSku = Streamable.from(async () => (await streamableProduct).sku);
119120

121+
const streamableProductInventory = Streamable.from(async () => {
122+
const variables = {
123+
entityId: Number(productId),
124+
};
125+
126+
const product = await getStreamableProductInventory(variables, customerAccessToken);
127+
128+
if (!product) {
129+
return notFound();
130+
}
131+
132+
return product;
133+
});
134+
120135
const streamableProductVariant = Streamable.from(async () => {
121-
const product = await streamableProduct;
136+
const product = await streamableProductInventory;
122137

123138
if (!product.inventory.hasVariantInventory) {
124139
return undefined;
@@ -188,7 +203,7 @@ export default async function Product({ params, searchParams }: Props) {
188203
});
189204

190205
const streameableCtaLabel = Streamable.from(async () => {
191-
const product = await streamableProduct;
206+
const product = await streamableProductInventory;
192207

193208
if (product.availabilityV2.status === 'Unavailable') {
194209
return t('ProductDetails.Submit.unavailable');
@@ -206,7 +221,7 @@ export default async function Product({ params, searchParams }: Props) {
206221
});
207222

208223
const streameableCtaDisabled = Streamable.from(async () => {
209-
const product = await streamableProduct;
224+
const product = await streamableProductInventory;
210225

211226
if (product.availabilityV2.status === 'Unavailable') {
212227
return true;
@@ -253,7 +268,7 @@ export default async function Product({ params, searchParams }: Props) {
253268

254269
const streamableStockDisplayData = Streamable.from(async () => {
255270
const [product, variant, inventorySetting] = await Streamable.all([
256-
streamableProduct,
271+
streamableProductInventory,
257272
streamableProductVariant,
258273
streamableInventorySettings,
259274
]);
@@ -343,7 +358,7 @@ export default async function Product({ params, searchParams }: Props) {
343358

344359
const streamableBackorderDisplayData = Streamable.from(async () => {
345360
const [product, variant, inventorySetting] = await Streamable.all([
346-
streamableProduct,
361+
streamableProductInventory,
347362
streamableProductVariant,
348363
streamableInventorySettings,
349364
]);

0 commit comments

Comments
 (0)