Skip to content

Commit

Permalink
chore: added ?nocache param for search page
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-ebey committed Jan 14, 2022
1 parent cfa66a3 commit 49cb32b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
9 changes: 7 additions & 2 deletions app/models/ecommerce-provider.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ export interface EcommerceProvider {
locale: Language,
items: CartItem[]
): Promise<CartInfo | undefined>;
getCategories(language: Language, count: number): Promise<Category[]>;
getCategories(
language: Language,
count: number,
nocache?: boolean
): Promise<Category[]>;
getCheckoutUrl(language: Language, items: CartItem[]): Promise<string>;
getFeaturedProducts(language: Language): Promise<Product[]>;
getPage(language: Language, slug: string): Promise<FullPage | undefined>;
Expand All @@ -104,7 +108,8 @@ export interface EcommerceProvider {
sort?: string,
search?: string,
cursor?: string,
perPage?: number
perPage?: number,
nocache?: boolean
): Promise<ProductsResult>;
getSortByOptions(language: Language): Promise<SortByOption[]>;
getWishlistInfo(
Expand Down
35 changes: 27 additions & 8 deletions app/models/ecommerce-providers/shopify.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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());
}

Expand Down Expand Up @@ -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 => ({
Expand Down Expand Up @@ -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} `;
Expand Down Expand Up @@ -300,7 +318,8 @@ export function createShopifyProvider({
query: q,
collection: category,
cursor,
}
},
nocache
);

let productsInfo = category
Expand Down
5 changes: 3 additions & 2 deletions app/route-containers/cdp/cdp.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);

Expand Down

0 comments on commit 49cb32b

Please sign in to comment.