Skip to content

Commit 313ee72

Browse files
jorgemoyaclaude
andcommitted
feat(core): wrap remaining guest queries in unstable_cache
Wrap blog, web pages, gift certificates, public wishlist, change password, and shipping countries queries in unstable_cache for guest visitors. This completes the guest query caching migration. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e7c85c1 commit 313ee72

11 files changed

Lines changed: 234 additions & 112 deletions

File tree

.changeset/guest-cache-content.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bigcommerce/catalyst-core": minor
3+
---
4+
5+
Wrap remaining guest queries (blog, web pages, gift certificates, public wishlist, auth forms, shipping countries) in `unstable_cache` with configurable revalidation.

core/app/[locale]/(default)/(auth)/change-password/page-data.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { unstable_cache } from 'next/cache';
12
import { cache } from 'react';
23

34
import { client } from '~/client';
@@ -24,16 +25,24 @@ const ChangePasswordQuery = graphql(`
2425
}
2526
`);
2627

27-
export const getChangePasswordQuery = cache(async () => {
28-
const response = await client.fetch({
29-
document: ChangePasswordQuery,
30-
fetchOptions: { next: { revalidate } },
31-
});
28+
const getCachedChangePasswordQuery = unstable_cache(
29+
async () => {
30+
const response = await client.fetch({
31+
document: ChangePasswordQuery,
32+
fetchOptions: { cache: 'no-store' },
33+
});
34+
35+
const passwordComplexitySettings =
36+
response.data.site.settings?.customers?.passwordComplexitySettings;
3237

33-
const passwordComplexitySettings =
34-
response.data.site.settings?.customers?.passwordComplexitySettings;
38+
return {
39+
passwordComplexitySettings,
40+
};
41+
},
42+
['change-password-data'],
43+
{ revalidate },
44+
);
3545

36-
return {
37-
passwordComplexitySettings,
38-
};
46+
export const getChangePasswordQuery = cache(async () => {
47+
return getCachedChangePasswordQuery();
3948
});

core/app/[locale]/(default)/blog/[blogId]/page-data.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { unstable_cache } from 'next/cache';
12
import { cache } from 'react';
23

34
import { client } from '~/client';
@@ -38,18 +39,26 @@ const BlogPageQuery = graphql(`
3839

3940
type Variables = VariablesOf<typeof BlogPageQuery>;
4041

41-
export const getBlogPageData = cache(async (variables: Variables) => {
42-
const response = await client.fetch({
43-
document: BlogPageQuery,
44-
variables,
45-
fetchOptions: { next: { revalidate } },
46-
});
42+
const getCachedBlogPageData = unstable_cache(
43+
async (variables: Variables) => {
44+
const response = await client.fetch({
45+
document: BlogPageQuery,
46+
variables,
47+
fetchOptions: { cache: 'no-store' },
48+
});
4749

48-
const { blog } = response.data.site.content;
50+
const { blog } = response.data.site.content;
4951

50-
if (!blog?.post) {
51-
return null;
52-
}
52+
if (!blog?.post) {
53+
return null;
54+
}
5355

54-
return blog;
56+
return blog;
57+
},
58+
['blog-page-data'],
59+
{ revalidate },
60+
);
61+
62+
export const getBlogPageData = cache(async (variables: Variables) => {
63+
return getCachedBlogPageData(variables);
5564
});

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

Lines changed: 41 additions & 12 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 } from 'next-intl/server';
34
import { cache } from 'react';
45

@@ -72,24 +73,32 @@ interface Pagination {
7273
after: string | null;
7374
}
7475

75-
export const getBlog = cache(async () => {
76-
const response = await client.fetch({
77-
document: BlogQuery,
78-
fetchOptions: { next: { revalidate } },
79-
});
76+
const getCachedBlog = unstable_cache(
77+
async () => {
78+
const response = await client.fetch({
79+
document: BlogQuery,
80+
fetchOptions: { cache: 'no-store' },
81+
});
8082

81-
return response.data.site.content.blog;
83+
return response.data.site.content.blog;
84+
},
85+
['blog-data'],
86+
{ revalidate },
87+
);
88+
89+
export const getBlog = cache(async () => {
90+
return getCachedBlog();
8291
});
8392

84-
export const getBlogPosts = cache(
85-
async ({ tag, limit = 9, before, after }: BlogPostsFiltersInput & Pagination) => {
93+
const getCachedBlogPosts = unstable_cache(
94+
async (tag: string | null, limit: number, before: string | null, after: string | null) => {
8695
const filterArgs = tag ? { filters: { tags: [tag] } } : {};
8796
const paginationArgs = before ? { last: limit, before } : { first: limit, after };
8897

8998
const response = await client.fetch({
9099
document: BlogPostsPageQuery,
91100
variables: { ...filterArgs, ...paginationArgs },
92-
fetchOptions: { next: { revalidate } },
101+
fetchOptions: { cache: 'no-store' },
93102
});
94103

95104
const { blog } = response.data.site.content;
@@ -98,15 +107,13 @@ export const getBlogPosts = cache(
98107
return null;
99108
}
100109

101-
const format = await getFormatter();
102-
103110
return {
104111
pageInfo: blog.posts.pageInfo,
105112
posts: removeEdgesAndNodes(blog.posts).map((post) => ({
106113
id: String(post.entityId),
107114
author: post.author,
108115
content: post.plainTextSummary,
109-
date: format.dateTime(new Date(post.publishedDate.utc)),
116+
publishedDateUtc: post.publishedDate.utc,
110117
image: post.thumbnailImage
111118
? {
112119
src: post.thumbnailImage.url,
@@ -118,4 +125,26 @@ export const getBlogPosts = cache(
118125
})),
119126
};
120127
},
128+
['blog-posts'],
129+
{ revalidate },
130+
);
131+
132+
export const getBlogPosts = cache(
133+
async ({ tag, limit = 9, before, after }: BlogPostsFiltersInput & Pagination) => {
134+
const result = await getCachedBlogPosts(tag, limit, before ?? null, after ?? null);
135+
136+
if (!result) {
137+
return null;
138+
}
139+
140+
const format = await getFormatter();
141+
142+
return {
143+
pageInfo: result.pageInfo,
144+
posts: result.posts.map((post) => ({
145+
...post,
146+
date: format.dateTime(new Date(post.publishedDateUtc)),
147+
})),
148+
};
149+
},
121150
);

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { unstable_cache } from 'next/cache';
12
import { cache } from 'react';
23

34
import { getSessionCustomerAccessToken } from '~/auth';
@@ -336,11 +337,19 @@ const SupportedShippingDestinationsQuery = graphql(`
336337
}
337338
`);
338339

339-
export const getShippingCountries = cache(async () => {
340-
const { data } = await client.fetch({
341-
document: SupportedShippingDestinationsQuery,
342-
fetchOptions: { next: { revalidate } },
343-
});
340+
const getCachedShippingCountries = unstable_cache(
341+
async () => {
342+
const { data } = await client.fetch({
343+
document: SupportedShippingDestinationsQuery,
344+
fetchOptions: { cache: 'no-store' },
345+
});
344346

345-
return data.site.settings?.shipping?.supportedShippingDestinations.countries ?? [];
347+
return data.site.settings?.shipping?.supportedShippingDestinations.countries ?? [];
348+
},
349+
['shipping-countries'],
350+
{ revalidate },
351+
);
352+
353+
export const getShippingCountries = cache(async () => {
354+
return getCachedShippingCountries();
346355
});

core/app/[locale]/(default)/gift-certificates/page-data.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { unstable_cache } from 'next/cache';
12
import { cache } from 'react';
23

34
import { client } from '~/client';
@@ -26,16 +27,24 @@ const GiftCertificatesRootQuery = graphql(
2627
[StoreLogoFragment],
2728
);
2829

29-
export const getGiftCertificatesData = cache(async (currencyCode?: CurrencyCode) => {
30-
const response = await client.fetch({
31-
document: GiftCertificatesRootQuery,
32-
variables: { currencyCode },
33-
fetchOptions: { next: { revalidate } },
34-
});
30+
const getCachedGiftCertificatesData = unstable_cache(
31+
async (currencyCode?: CurrencyCode) => {
32+
const response = await client.fetch({
33+
document: GiftCertificatesRootQuery,
34+
variables: { currencyCode },
35+
fetchOptions: { cache: 'no-store' },
36+
});
37+
38+
return {
39+
giftCertificatesEnabled: response.data.site.settings?.giftCertificates?.isEnabled ?? false,
40+
defaultCurrency: response.data.site.settings?.currency.defaultCurrency ?? undefined,
41+
logo: response.data.site.settings ? logoTransformer(response.data.site.settings) : '',
42+
};
43+
},
44+
['gift-certificates-data'],
45+
{ revalidate },
46+
);
3547

36-
return {
37-
giftCertificatesEnabled: response.data.site.settings?.giftCertificates?.isEnabled ?? false,
38-
defaultCurrency: response.data.site.settings?.currency.defaultCurrency ?? undefined,
39-
logo: response.data.site.settings ? logoTransformer(response.data.site.settings) : '',
40-
};
48+
export const getGiftCertificatesData = cache(async (currencyCode?: CurrencyCode) => {
49+
return getCachedGiftCertificatesData(currencyCode);
4150
});

core/app/[locale]/(default)/gift-certificates/purchase/page-data.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { unstable_cache } from 'next/cache';
12
import { cache } from 'react';
23

34
import { client } from '~/client';
@@ -29,17 +30,25 @@ const GiftCertificatePurchaseSettingsQuery = graphql(
2930
[GiftCertificateSettingsFragment, StoreLogoFragment],
3031
);
3132

32-
export const getGiftCertificatePurchaseData = cache(async (currencyCode?: CurrencyCode) => {
33-
const response = await client.fetch({
34-
document: GiftCertificatePurchaseSettingsQuery,
35-
variables: { currencyCode },
36-
fetchOptions: { next: { revalidate } },
37-
});
33+
const getCachedGiftCertificatePurchaseData = unstable_cache(
34+
async (currencyCode?: CurrencyCode) => {
35+
const response = await client.fetch({
36+
document: GiftCertificatePurchaseSettingsQuery,
37+
variables: { currencyCode },
38+
fetchOptions: { cache: 'no-store' },
39+
});
40+
41+
return {
42+
giftCertificateSettings: response.data.site.settings?.giftCertificates ?? null,
43+
logo: response.data.site.settings ? logoTransformer(response.data.site.settings) : '',
44+
storeName: response.data.site.settings?.storeName ?? undefined,
45+
defaultCurrency: response.data.site.settings?.currency.defaultCurrency ?? undefined,
46+
};
47+
},
48+
['gift-certificate-purchase-data'],
49+
{ revalidate },
50+
);
3851

39-
return {
40-
giftCertificateSettings: response.data.site.settings?.giftCertificates ?? null,
41-
logo: response.data.site.settings ? logoTransformer(response.data.site.settings) : '',
42-
storeName: response.data.site.settings?.storeName ?? undefined,
43-
defaultCurrency: response.data.site.settings?.currency.defaultCurrency ?? undefined,
44-
};
52+
export const getGiftCertificatePurchaseData = cache(async (currencyCode?: CurrencyCode) => {
53+
return getCachedGiftCertificatePurchaseData(currencyCode);
4554
});

core/app/[locale]/(default)/webpages/[id]/contact/page-data.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { unstable_cache } from 'next/cache';
12
import { cache } from 'react';
23

34
import { client } from '~/client';
@@ -31,12 +32,20 @@ const ContactPageQuery = graphql(
3132

3233
type Variables = VariablesOf<typeof ContactPageQuery>;
3334

34-
export const getWebpageData = cache(async (variables: Variables) => {
35-
const { data } = await client.fetch({
36-
document: ContactPageQuery,
37-
variables,
38-
fetchOptions: { next: { revalidate } },
39-
});
35+
const getCachedWebpageData = unstable_cache(
36+
async (variables: Variables) => {
37+
const { data } = await client.fetch({
38+
document: ContactPageQuery,
39+
variables,
40+
fetchOptions: { cache: 'no-store' },
41+
});
42+
43+
return data;
44+
},
45+
['contact-webpage-data'],
46+
{ revalidate },
47+
);
4048

41-
return data;
49+
export const getWebpageData = cache(async (variables: Variables) => {
50+
return getCachedWebpageData(variables);
4251
});

core/app/[locale]/(default)/webpages/[id]/layout.tsx

Lines changed: 31 additions & 22 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 { setRequestLocale } from 'next-intl/server';
34
import { cache } from 'react';
45

@@ -45,34 +46,42 @@ interface PageLink {
4546
href: string;
4647
}
4748

48-
const getWebPageChildren = cache(async (id: string): Promise<PageLink[]> => {
49-
const { data } = await client.fetch({
50-
document: WebPageChildrenQuery,
51-
variables: { id: decodeURIComponent(id) },
52-
fetchOptions: { next: { revalidate } },
53-
});
49+
const getCachedWebPageChildren = unstable_cache(
50+
async (id: string): Promise<PageLink[]> => {
51+
const { data } = await client.fetch({
52+
document: WebPageChildrenQuery,
53+
variables: { id: decodeURIComponent(id) },
54+
fetchOptions: { cache: 'no-store' },
55+
});
5456

55-
if (!data.node) {
56-
return [];
57-
}
57+
if (!data.node) {
58+
return [];
59+
}
5860

59-
if (!('children' in data.node)) {
60-
return [];
61-
}
61+
if (!('children' in data.node)) {
62+
return [];
63+
}
6264

63-
const { children } = data.node;
65+
const { children } = data.node;
6466

65-
return removeEdgesAndNodes(children).reduce((acc: PageLink[], child) => {
66-
if ('path' in child) {
67-
return [...acc, { label: child.name, href: child.path }];
68-
}
67+
return removeEdgesAndNodes(children).reduce((acc: PageLink[], child) => {
68+
if ('path' in child) {
69+
return [...acc, { label: child.name, href: child.path }];
70+
}
6971

70-
if ('link' in child) {
71-
return [...acc, { label: child.name, href: child.link }];
72-
}
72+
if ('link' in child) {
73+
return [...acc, { label: child.name, href: child.link }];
74+
}
7375

74-
return acc;
75-
}, []);
76+
return acc;
77+
}, []);
78+
},
79+
['webpage-children'],
80+
{ revalidate },
81+
);
82+
83+
const getWebPageChildren = cache(async (id: string): Promise<PageLink[]> => {
84+
return getCachedWebPageChildren(id);
7685
});
7786

7887
export default async function WebPageLayout({ params, children }: Props) {

0 commit comments

Comments
 (0)