Skip to content

Commit 18cfdc8

Browse files
authored
feat: Remove caching on fetching product inventory data (#2801)
* feat: Remove caching on fetching product inventory data * feat: Remove caching on fetching product inventory data - Apply comments
1 parent f5330c7 commit 18cfdc8

File tree

3 files changed

+77
-24
lines changed

3 files changed

+77
-24
lines changed

.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: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export const getProduct = cache(async (entityId: number, customerAccessToken?: s
211211
return data.site;
212212
});
213213

214-
const StreamableProductVariantBySkuQuery = graphql(`
214+
const StreamableProductVariantInventoryBySkuQuery = graphql(`
215215
query ProductVariantBySkuQuery($productId: Int!, $sku: String!) {
216216
site {
217217
product(entityId: $productId) {
@@ -247,15 +247,15 @@ const StreamableProductVariantBySkuQuery = graphql(`
247247
}
248248
`);
249249

250-
type VariantVariables = VariablesOf<typeof StreamableProductVariantBySkuQuery>;
250+
type VariantInventoryVariables = VariablesOf<typeof StreamableProductVariantInventoryBySkuQuery>;
251251

252-
export const getStreamableProductVariant = cache(
253-
async (variables: VariantVariables, customerAccessToken?: string) => {
252+
export const getStreamableProductVariantInventory = cache(
253+
async (variables: VariantInventoryVariables, customerAccessToken?: string) => {
254254
const { data } = await client.fetch({
255-
document: StreamableProductVariantBySkuQuery,
255+
document: StreamableProductVariantInventoryBySkuQuery,
256256
variables,
257257
customerAccessToken,
258-
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
258+
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate: 60 } },
259259
});
260260

261261
return data.site.product?.variants;
@@ -311,6 +311,36 @@ const StreamableProductQuery = graphql(
311311
minPurchaseQuantity
312312
maxPurchaseQuantity
313313
warranty
314+
...ProductViewedFragment
315+
...ProductSchemaFragment
316+
}
317+
}
318+
}
319+
`,
320+
[ProductViewedFragment, ProductSchemaFragment],
321+
);
322+
323+
type Variables = VariablesOf<typeof StreamableProductQuery>;
324+
325+
export const getStreamableProduct = cache(
326+
async (variables: Variables, customerAccessToken?: string) => {
327+
const { data } = await client.fetch({
328+
document: StreamableProductQuery,
329+
variables,
330+
customerAccessToken,
331+
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
332+
});
333+
334+
return data.site.product;
335+
},
336+
);
337+
338+
const StreamableProductInventoryQuery = graphql(
339+
`
340+
query StreamableProductInventoryQuery($entityId: Int!) {
341+
site {
342+
product(entityId: $entityId) {
343+
sku
314344
inventory {
315345
hasVariantInventory
316346
isInStock
@@ -325,25 +355,23 @@ const StreamableProductQuery = graphql(
325355
availabilityV2 {
326356
status
327357
}
328-
...ProductViewedFragment
329358
...ProductVariantsInventoryFragment
330-
...ProductSchemaFragment
331359
}
332360
}
333361
}
334362
`,
335-
[ProductViewedFragment, ProductSchemaFragment, ProductVariantsInventoryFragment],
363+
[ProductVariantsInventoryFragment],
336364
);
337365

338-
type Variables = VariablesOf<typeof StreamableProductQuery>;
366+
type ProductInventoryVariables = VariablesOf<typeof StreamableProductQuery>;
339367

340-
export const getStreamableProduct = cache(
341-
async (variables: Variables, customerAccessToken?: string) => {
368+
export const getStreamableProductInventory = cache(
369+
async (variables: ProductInventoryVariables, customerAccessToken?: string) => {
342370
const { data } = await client.fetch({
343-
document: StreamableProductQuery,
371+
document: StreamableProductInventoryQuery,
344372
variables,
345373
customerAccessToken,
346-
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
374+
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate: 60 } },
347375
});
348376

349377
return data.site.product;

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
getProductPricingAndRelatedProducts,
3030
getStreamableInventorySettingsQuery,
3131
getStreamableProduct,
32-
getStreamableProductVariant,
32+
getStreamableProductInventory,
33+
getStreamableProductVariantInventory,
3334
} from './page-data';
3435

3536
interface Props {
@@ -120,8 +121,22 @@ export default async function Product({ params, searchParams }: Props) {
120121

121122
const streamableProductSku = Streamable.from(async () => (await streamableProduct).sku);
122123

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

126141
if (!product.inventory.hasVariantInventory) {
127142
return undefined;
@@ -132,7 +147,7 @@ export default async function Product({ params, searchParams }: Props) {
132147
sku: product.sku,
133148
};
134149

135-
const variants = await getStreamableProductVariant(variables, customerAccessToken);
150+
const variants = await getStreamableProductVariantInventory(variables, customerAccessToken);
136151

137152
if (!variants) {
138153
return undefined;
@@ -194,7 +209,7 @@ export default async function Product({ params, searchParams }: Props) {
194209
});
195210

196211
const streameableCtaLabel = Streamable.from(async () => {
197-
const product = await streamableProduct;
212+
const product = await streamableProductInventory;
198213

199214
if (product.availabilityV2.status === 'Unavailable') {
200215
return t('ProductDetails.Submit.unavailable');
@@ -212,7 +227,7 @@ export default async function Product({ params, searchParams }: Props) {
212227
});
213228

214229
const streameableCtaDisabled = Streamable.from(async () => {
215-
const product = await streamableProduct;
230+
const product = await streamableProductInventory;
216231

217232
if (product.availabilityV2.status === 'Unavailable') {
218233
return true;
@@ -259,8 +274,8 @@ export default async function Product({ params, searchParams }: Props) {
259274

260275
const streamableStockDisplayData = Streamable.from(async () => {
261276
const [product, variant, inventorySetting] = await Streamable.all([
262-
streamableProduct,
263-
streamableProductVariant,
277+
streamableProductInventory,
278+
streamableProductVariantInventory,
264279
streamableInventorySettings,
265280
]);
266281

@@ -349,8 +364,8 @@ export default async function Product({ params, searchParams }: Props) {
349364

350365
const streamableBackorderDisplayData = Streamable.from(async () => {
351366
const [product, variant, inventorySetting] = await Streamable.all([
352-
streamableProduct,
353-
streamableProductVariant,
367+
streamableProductInventory,
368+
streamableProductVariantInventory,
354369
streamableInventorySettings,
355370
]);
356371

0 commit comments

Comments
 (0)