From 49cb32bdbd7f7223ba013193536471090d6a8eb6 Mon Sep 17 00:00:00 2001 From: Jacob Ebey Date: Fri, 14 Jan 2022 09:35:14 -0800 Subject: [PATCH] chore: added `?nocache` param for search page --- app/models/ecommerce-provider.server.ts | 9 +++-- .../ecommerce-providers/shopify.server.ts | 35 ++++++++++++++----- app/route-containers/cdp/cdp.server.ts | 5 +-- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/models/ecommerce-provider.server.ts b/app/models/ecommerce-provider.server.ts index 97a2bed..2baaa3f 100644 --- a/app/models/ecommerce-provider.server.ts +++ b/app/models/ecommerce-provider.server.ts @@ -88,7 +88,11 @@ export interface EcommerceProvider { locale: Language, items: CartItem[] ): Promise; - getCategories(language: Language, count: number): Promise; + getCategories( + language: Language, + count: number, + nocache?: boolean + ): Promise; getCheckoutUrl(language: Language, items: CartItem[]): Promise; getFeaturedProducts(language: Language): Promise; getPage(language: Language, slug: string): Promise; @@ -104,7 +108,8 @@ export interface EcommerceProvider { sort?: string, search?: string, cursor?: string, - perPage?: number + perPage?: number, + nocache?: boolean ): Promise; getSortByOptions(language: Language): Promise; getWishlistInfo( diff --git a/app/models/ecommerce-providers/shopify.server.ts b/app/models/ecommerce-providers/shopify.server.ts index 349a914..36a1d6c 100644 --- a/app/models/ecommerce-providers/shopify.server.ts +++ b/app/models/ecommerce-providers/shopify.server.ts @@ -26,7 +26,12 @@ export function createShopifyProvider({ storefrontAccessToken, }: ShopifyProviderOptions): EcommerceProvider { let href = `https://${shop}.myshopify.com/api/2021-10/graphql.json`; - async function query(locale: string, query: string, variables?: any) { + async function query( + locale: string, + query: string, + variables?: any, + nocache?: boolean + ) { let request = new Request(href, { method: "POST", headers: { @@ -42,7 +47,7 @@ export function createShopifyProvider({ ? maxAgeSeconds(request.clone()) : maxAgeSeconds; - if (cache && typeof maxAge === "number") { + if (!nocache && cache && typeof maxAge === "number") { return cache(request, maxAge).then((res) => res.json()); } @@ -109,10 +114,15 @@ export function createShopifyProvider({ items: fullItems, }; }, - async getCategories(locale, count) { - let json = await query(locale, getAllCollectionQuery, { - first: count, - }); + async getCategories(locale, count, nocache) { + let json = await query( + locale, + getAllCollectionQuery, + { + first: count, + }, + nocache + ); let categories = json.data.collections.edges.map( ({ node: { title, handle } }: any): Category => ({ @@ -256,7 +266,15 @@ export function createShopifyProvider({ })), }; }, - async getProducts(locale, category, sort, search, cursor, perPage = 30) { + async getProducts( + locale, + category, + sort, + search, + cursor, + perPage = 30, + nocache + ) { let q = ""; if (search) { q += `product_type:${search} OR title:${search} OR tag:${search} `; @@ -300,7 +318,8 @@ export function createShopifyProvider({ query: q, collection: category, cursor, - } + }, + nocache ); let productsInfo = category diff --git a/app/route-containers/cdp/cdp.server.ts b/app/route-containers/cdp/cdp.server.ts index 1f12f63..2eb4739 100644 --- a/app/route-containers/cdp/cdp.server.ts +++ b/app/route-containers/cdp/cdp.server.ts @@ -38,11 +38,12 @@ export let loader: LoaderFunction = async ({ request, params }) => { let sort = url.searchParams.get("sort") || undefined; let search = url.searchParams.get("q") || undefined; let cursor = url.searchParams.get("cursor") || undefined; + let nocache = url.searchParams.has("nocache"); let [categories, sortByOptions, productsPage, wishlist] = await Promise.all([ - commerce.getCategories(lang, 250), + commerce.getCategories(lang, 250, nocache), commerce.getSortByOptions(lang), - commerce.getProducts(lang, category, sort, search, cursor), + commerce.getProducts(lang, category, sort, search, cursor, 30, nocache), session.getWishlist(), ]);