Skip to content

Commit

Permalink
feat(core): add default revalidate target
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemoya committed Feb 16, 2024
1 parent 650256a commit 5985f47
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ AUTH_SECRET=
# https://turbo.build/repo/docs/core-concepts/remote-caching#artifact-integrity-and-authenticity-verification
# This can also be generated with `openssl rand -hex 32`, but do not re-use the value from AUTH_SECRET
TURBO_REMOTE_CACHE_SIGNATURE_KEY=

# NextJS will persists cached queries in Data Cache
# This sets a sensible revalidation target for cached requests
NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET=3600
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-best-selling-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export const getBestSellingProducts = cache(
const query = graphql(GET_BEST_SELLING_PRODUCTS_QUERY);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const response = await client.fetch({
document: query,
variables: { first, imageWidth, imageHeight },
customerId,
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

const { site } = response.data;
Expand Down
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-category-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export const getCategoryTree = cache(async (categoryId?: number) => {
const query = graphql(GET_CATEGORY_TREE_QUERY);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const response = await client.fetch({
document: query,
variables: { categoryId },
customerId,
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

return response.data.site.categoryTree;
Expand Down
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ export const getCategory = cache(
const query = graphql(GET_CATEGORY_QUERY);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const paginationArgs = before ? { last: limit, before } : { first: limit, after };

const response = await client.fetch({
document: query,
variables: { categoryId, breadcrumbDepth, ...paginationArgs },
customerId,
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

const category = response.data.site.category;
Expand Down
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-featured-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export const getFeaturedProducts = cache(
const query = graphql(GET_FEATURED_PRODUCTS_QUERY);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const response = await client.fetch({
document: query,
variables: { first, imageWidth, imageHeight },
customerId,
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

const { site } = response.data;
Expand Down
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@ const getInternalProduct = async (productId: number, optionValueIds?: OptionValu
const query = graphql(GET_PRODUCT_QUERY);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const response = await client.fetch({
document: query,
variables: { productId, optionValueIds },
customerId,
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

const product = response.data.site.product;
Expand Down
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ export const getProducts = cache(
const query = graphql(GET_PRODUCTS_QUERY);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const response = await client.fetch({
document: query,
variables: { entityIds: productIds, first, imageWidth, imageHeight },
customerId,
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

const products = removeEdgesAndNodes(response.data.site.products);
Expand Down
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-quick-search-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ export const getQuickSearchResults = cache(
const query = graphql(GET_QUICK_SEARCH_RESULTS_QUERY);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const response = await client.fetch({
document: query,
variables: { filters: { searchTerm }, imageHeight, imageWidth },
customerId,
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

const { products } = response.data.site.search.searchProducts;
Expand Down
8 changes: 5 additions & 3 deletions apps/core/client/queries/get-related-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ export const getRelatedProducts = cache(
const query = graphql(GET_RELATED_PRODUCTS);
const customerId = await getSessionCustomerId();

const revalidate = process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET
? Number(process.env.NEXT_PUBLIC_DEFAULT_REVALIDATE_TARGET)
: undefined;

const response = await client.fetch({
document: query,
variables: { entityId: productId, optionValueIds, first, imageWidth, imageHeight },
fetchOptions: {
cache: customerId ? 'no-store' : 'force-cache',
},
fetchOptions: customerId ? { cache: 'no-store' } : { next: { revalidate } },
});

const { product } = response.data.site;
Expand Down

0 comments on commit 5985f47

Please sign in to comment.