Skip to content

Commit

Permalink
Merge branch 'main' into feat/cart-product-url
Browse files Browse the repository at this point in the history
  • Loading branch information
luanargolodev committed Oct 16, 2024
2 parents 39e29dc + 8afe473 commit d3a1879
Show file tree
Hide file tree
Showing 8 changed files with 3,823 additions and 3,682 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@
"jsx": "react-jsx",
"jsxImportSource": "preact"
},
"version": "0.62.6"
"version": "0.62.8"
}
29 changes: 25 additions & 4 deletions shopify/loaders/ProductListingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export interface Props {
* @description search for metafields
*/
metafields?: Metafield[];
/**
* @title Starting page query parameter offset.
* @description Set the starting page offset. Default to 1.
*/
pageOffset?: number;
/**
* @hide
* @description it is hidden because only page prop is not sufficient, we need cursors
Expand Down Expand Up @@ -80,7 +85,11 @@ const loader = async (

const count = props.count ?? 12;
const query = props.query || url.searchParams.get("q") || "";
const page = props.page || Number(url.searchParams.get("page")) || 0;
const currentPageoffset = props.pageOffset ?? 1;
const pageParam = url.searchParams.get("page")
? Number(url.searchParams.get("page")) - currentPageoffset
: 0;
const page = props.page || pageParam;
const endCursor = props.endCursor || url.searchParams.get("endCursor") || "";
const startCursor = props.startCursor ||
url.searchParams.get("startCursor") || "";
Expand All @@ -96,6 +105,8 @@ const loader = async (
| undefined = undefined;
let shopifyFilters = undefined;
let records = undefined;
let collectionTitle = undefined;
let collectionDescription = undefined;

const sort = url.searchParams.get("sort") ?? "";

Expand Down Expand Up @@ -156,6 +167,8 @@ const loader = async (
hasPreviousPage = Boolean(
data?.collection?.products.pageInfo.hasPreviousPage ?? false,
);
collectionTitle = data.collection?.title;
collectionDescription = data.collection?.description;
}

// Transform Shopify product format into schema.org's compatible format
Expand All @@ -169,18 +182,19 @@ const loader = async (
const previousPage = new URLSearchParams(url.searchParams);

if (hasNextPage) {
nextPage.set("page", (page + 1).toString());
nextPage.set("page", (page + currentPageoffset + 1).toString());
nextPage.set("startCursor", shopifyProducts?.pageInfo.endCursor ?? "");
nextPage.delete("endCursor");
}

if (hasPreviousPage) {
previousPage.set("page", (page - 1).toString());
previousPage.set("page", (page + currentPageoffset - 1).toString());
previousPage.set("endCursor", shopifyProducts?.pageInfo.startCursor ?? "");
previousPage.delete("startCursor");
}

const filters = shopifyFilters?.map((filter) => toFilter(filter, url));
const currentPage = page + currentPageoffset;

return {
"@type": "ProductListingPage",
Expand All @@ -200,11 +214,18 @@ const loader = async (
pageInfo: {
nextPage: hasNextPage ? `?${nextPage}` : undefined,
previousPage: hasPreviousPage ? `?${previousPage}` : undefined,
currentPage: page,
currentPage,
records,
recordPerPage: count,
},
sortOptions: isSearch ? searchSortOptions : sortOptions,
seo: {
title: collectionTitle || "",
description: collectionDescription || "",
canonical: `${url.origin}${url.pathname}${
page >= 1 ? `?page=${page}` : ""
}`,
},
};
};

Expand Down
7 changes: 7 additions & 0 deletions shopify/loaders/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Route } from "../../website/flags/audience.ts";
import { TextReplace } from "../../website/handlers/proxy.ts";
import { AppContext } from "../mod.ts";
import { withDigestCookie } from "../utils/password.ts";

Expand Down Expand Up @@ -32,11 +33,13 @@ const buildProxyRoutes = (
includeSiteMap,
generateDecoSiteMap,
excludePathsFromDecoSiteMap,
replaces,
}: {
extraPaths: string[];
includeSiteMap?: string[];
generateDecoSiteMap?: boolean;
excludePathsFromDecoSiteMap: string[];
replaces: TextReplace[];
ctx: AppContext;
},
) => {
Expand Down Expand Up @@ -65,6 +68,7 @@ const buildProxyRoutes = (
url: urlToProxy,
host: hostToUse,
customHeaders: withDigestCookie(ctx),
replaces,
},
},
});
Expand Down Expand Up @@ -126,6 +130,7 @@ export interface Props {
* @title Exclude paths from /deco-sitemap.xml
*/
excludePathsFromDecoSiteMap?: string[];
replaces?: TextReplace[];
}

/**
Expand All @@ -137,6 +142,7 @@ function loader(
includeSiteMap = [],
generateDecoSiteMap = true,
excludePathsFromDecoSiteMap = [],
replaces = [],
}: Props,
_req: Request,
ctx: AppContext,
Expand All @@ -146,6 +152,7 @@ function loader(
excludePathsFromDecoSiteMap,
includeSiteMap,
extraPaths: extraPathsToProxy,
replaces,
ctx,
});
}
Expand Down
2 changes: 2 additions & 0 deletions shopify/utils/storefront/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ export const ProductsByCollection = {
){
collection(handle: $handle) {
handle
description
title
products(
first: $first,
last: $last,
Expand Down
55 changes: 47 additions & 8 deletions shopify/utils/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
BreadcrumbList,
Filter,
ListItem,
Product,
ProductDetailsPage,
PropertyValue,
Expand Down Expand Up @@ -63,20 +64,58 @@ export const toProductPage = (
};
};

export const toBreadcrumbItem = ({
name,
position,
item,
}: {
name: string;
position: number;
item: string;
}): ListItem => ({
"@type": "ListItem",
name: decodeURI(name),
position,
item,
});

export const toBreadcrumbList = (
product: ProductShopify,
sku: SkuShopify,
): BreadcrumbList => {
return {
let list: ListItem[] = [];
const collection = product.collections?.nodes[0];

if (collection) {
list = [
toBreadcrumbItem({
name: collection.title,
position: 1,
item: `/${collection.handle}`,
}),
toBreadcrumbItem({
name: product.title,
position: 2,
item: getPath(product, sku),
}),
];
} else {
list = [
toBreadcrumbItem({
name: product.title,
position: 2,
item: getPath(product, sku),
}),
];
}

const data: BreadcrumbList = {
"@type": "BreadcrumbList",
numberOfItems: 1,
itemListElement: [{
"@type": "ListItem",
name: product.title,
item: getPath(product, sku),
position: 1,
}],
numberOfItems: list.length,
itemListElement: list,
};

return data as BreadcrumbList;
};

export const toProduct = (
Expand Down
1 change: 1 addition & 0 deletions vtex/utils/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export interface VTEXCommerceStable {
"POST /api/checkout/pub/orderForm/:orderFormId/items/update": {
response: OrderForm;
body: {
noSplitItem?: boolean;
orderItems: Array<{
quantity: number;
index: number;
Expand Down
Loading

0 comments on commit d3a1879

Please sign in to comment.