From 5985f47ad434372c51391e709bdba001465abfab Mon Sep 17 00:00:00 2001 From: Jorge Moya Date: Fri, 16 Feb 2024 14:15:46 -0600 Subject: [PATCH] feat(core): add default revalidate target --- .env.example | 4 ++++ apps/core/client/queries/get-best-selling-products.ts | 8 +++++--- apps/core/client/queries/get-category-tree.ts | 8 +++++--- apps/core/client/queries/get-category.ts | 8 +++++--- apps/core/client/queries/get-featured-products.ts | 8 +++++--- apps/core/client/queries/get-product.ts | 8 +++++--- apps/core/client/queries/get-products.ts | 8 +++++--- apps/core/client/queries/get-quick-search-results.ts | 8 +++++--- apps/core/client/queries/get-related-products.ts | 8 +++++--- 9 files changed, 44 insertions(+), 24 deletions(-) diff --git a/.env.example b/.env.example index 2039740e2f..e523374dd6 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/apps/core/client/queries/get-best-selling-products.ts b/apps/core/client/queries/get-best-selling-products.ts index 1fd23c4b20..ebefbdb135 100644 --- a/apps/core/client/queries/get-best-selling-products.ts +++ b/apps/core/client/queries/get-best-selling-products.ts @@ -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; diff --git a/apps/core/client/queries/get-category-tree.ts b/apps/core/client/queries/get-category-tree.ts index 88978f8a72..723cbc68e5 100644 --- a/apps/core/client/queries/get-category-tree.ts +++ b/apps/core/client/queries/get-category-tree.ts @@ -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; diff --git a/apps/core/client/queries/get-category.ts b/apps/core/client/queries/get-category.ts index 9f2e13cfaa..267f5b091e 100644 --- a/apps/core/client/queries/get-category.ts +++ b/apps/core/client/queries/get-category.ts @@ -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; diff --git a/apps/core/client/queries/get-featured-products.ts b/apps/core/client/queries/get-featured-products.ts index 3a91b59407..7cb310ec7c 100644 --- a/apps/core/client/queries/get-featured-products.ts +++ b/apps/core/client/queries/get-featured-products.ts @@ -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; diff --git a/apps/core/client/queries/get-product.ts b/apps/core/client/queries/get-product.ts index 687b80458a..3c72586361 100644 --- a/apps/core/client/queries/get-product.ts +++ b/apps/core/client/queries/get-product.ts @@ -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; diff --git a/apps/core/client/queries/get-products.ts b/apps/core/client/queries/get-products.ts index ec2d46aeb0..d95979e37b 100644 --- a/apps/core/client/queries/get-products.ts +++ b/apps/core/client/queries/get-products.ts @@ -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); diff --git a/apps/core/client/queries/get-quick-search-results.ts b/apps/core/client/queries/get-quick-search-results.ts index abb3d78b63..dad05aecd9 100644 --- a/apps/core/client/queries/get-quick-search-results.ts +++ b/apps/core/client/queries/get-quick-search-results.ts @@ -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; diff --git a/apps/core/client/queries/get-related-products.ts b/apps/core/client/queries/get-related-products.ts index 76205f0bd5..faec005587 100644 --- a/apps/core/client/queries/get-related-products.ts +++ b/apps/core/client/queries/get-related-products.ts @@ -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;