Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/guest-cache-layout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

Wrap layout-level guest queries (home page, header, footer, SEO canonical, reCAPTCHA, root layout metadata) in `unstable_cache` with configurable revalidation. Authenticated requests continue to bypass the cache.
5 changes: 5 additions & 0 deletions .changeset/guest-cache-product.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

Wrap product page guest queries in `unstable_cache` with configurable revalidation. Includes product details, pricing, inventory, reviews, and related products. Authenticated requests continue to bypass the cache.
5 changes: 5 additions & 0 deletions .changeset/locale-param-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-client": patch
---

Add optional `locale` parameter to `client.fetch()`. This allows locale to be passed explicitly for use in cached contexts where `getLocale()` is unavailable.
5 changes: 5 additions & 0 deletions .changeset/locale-param-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

Wrap IP forwarding headers (`X-Forwarded-For`, `True-Client-IP`) in try/catch for safety inside `unstable_cache` contexts where `headers()` is unavailable.
29 changes: 25 additions & 4 deletions core/app/[locale]/(default)/page-data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unstable_cache } from 'next/cache';
import { cache } from 'react';

import { client } from '~/client';
Expand Down Expand Up @@ -77,15 +78,35 @@ const HomePageQuery = graphql(
[FeaturedProductsCarouselFragment, FeaturedProductsListFragment],
);

export const getPageData = cache(
async (currencyCode?: CurrencyCode, customerAccessToken?: string) => {
const getCachedPageData = unstable_cache(
async (locale: string, currencyCode?: CurrencyCode) => {
const { data } = await client.fetch({
document: HomePageQuery,
customerAccessToken,
variables: { currencyCode },
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
locale,
fetchOptions: { cache: 'no-store' },
});

return data;
},
['home-page-data'],
{ revalidate },
);

export const getPageData = cache(
async (locale: string, currencyCode?: CurrencyCode, customerAccessToken?: string) => {
if (customerAccessToken) {
const { data } = await client.fetch({
document: HomePageQuery,
customerAccessToken,
variables: { currencyCode },
locale,
fetchOptions: { cache: 'no-store' },
});

return data;
}

return getCachedPageData(locale, currencyCode);
},
);
2 changes: 1 addition & 1 deletion core/app/[locale]/(default)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default async function Home({ params }: Props) {
const customerAccessToken = await getSessionCustomerAccessToken();
const currencyCode = await getPreferredCurrencyCode();

return getPageData(currencyCode, customerAccessToken);
return getPageData(locale, currencyCode, customerAccessToken);
});

const streamableFeaturedProducts = Streamable.from(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client';
import { unstable_cache } from 'next/cache';
import { getFormatter, getTranslations } from 'next-intl/server';
import { createLoader, parseAsString, SearchParams } from 'nuqs/server';
import { cache } from 'react';
Expand Down Expand Up @@ -64,14 +65,22 @@ const ReviewsQuery = graphql(
[ProductReviewSchemaFragment, PaginationFragment],
);

const getReviews = cache(async (productId: number, paginationArgs: object) => {
const { data } = await client.fetch({
document: ReviewsQuery,
variables: { ...paginationArgs, entityId: productId },
fetchOptions: { next: { revalidate } },
});
const getCachedReviews = unstable_cache(
async (productId: number, paginationArgs: object) => {
const { data } = await client.fetch({
document: ReviewsQuery,
variables: { ...paginationArgs, entityId: productId },
fetchOptions: { cache: 'no-store' },
});

return data.site.product;
return data.site.product;
},
['product-reviews'],
{ revalidate },
);

const getReviews = cache(async (productId: number, paginationArgs: object) => {
return getCachedReviews(productId, paginationArgs);
});

interface Props {
Expand Down
Loading
Loading