diff --git a/.changeset/fast-moles-explain.md b/.changeset/fast-moles-explain.md new file mode 100644 index 0000000000..95198a540d --- /dev/null +++ b/.changeset/fast-moles-explain.md @@ -0,0 +1,5 @@ +--- +'@graphcommerce/magento-product': patch +--- + +The ProductFilters and ProductList queries would only run after the hygraphPageContent query would be resolved, although they don't depend on each other, now they run in parallel. diff --git a/examples/magento-graphcms/pages/[...url].tsx b/examples/magento-graphcms/pages/[...url].tsx index 8690d449bf..f019f3001c 100644 --- a/examples/magento-graphcms/pages/[...url].tsx +++ b/examples/magento-graphcms/pages/[...url].tsx @@ -166,38 +166,29 @@ export const getStaticProps: GetPageStaticProps = async ({ params, locale }) => } const pages = hygraphPageContent(staticClient, url, category) - const hasPage = filteredCategoryUid ? false : (await pages).data.pages.length > 0 const hasCategory = Boolean(productListParams && categoryUid) - if (!productListParams || !(hasPage || hasCategory)) - return redirectOrNotFound(staticClient, conf, params, locale) - - if (!hasCategory) { - return { - props: { - ...(await categoryPage).data, - ...(await pages).data, - ...(await layout).data, - apolloState: await conf.then(() => client.cache.extract()), - }, - revalidate: 60 * 20, - } - } + const filters = hasCategory + ? staticClient.query({ + query: ProductFiltersDocument, + variables: { filters: { category_uid: { eq: categoryUid } } }, + }) + : undefined + const products = hasCategory + ? staticClient.query({ + query: ProductListDocument, + variables: { + pageSize: (await conf).data.storeConfig?.grid_per_page ?? 24, + ...productListParams, + filters: { ...productListParams?.filters, category_uid: { eq: categoryUid } }, + }, + }) + : undefined - const filters = staticClient.query({ - query: ProductFiltersDocument, - variables: { filters: { category_uid: { eq: categoryUid } } }, - }) - const products = staticClient.query({ - query: ProductListDocument, - variables: { - pageSize: (await conf).data.storeConfig?.grid_per_page ?? 24, - ...productListParams, - filters: { ...productListParams.filters, category_uid: { eq: categoryUid } }, - }, - }) + const hasPage = filteredCategoryUid ? false : (await pages).data.pages.length > 0 + if (!hasCategory && !hasPage) return redirectOrNotFound(staticClient, conf, params, locale) - if ((await products).errors) return { notFound: true } + if ((await products)?.errors) return { notFound: true } const { category_name, category_url_path } = (await categoryPage).data.categories?.items?.[0]?.breadcrumbs?.[0] ?? {} @@ -210,9 +201,9 @@ export const getStaticProps: GetPageStaticProps = async ({ params, locale }) => const result = { props: { ...(await categoryPage).data, - ...(await products).data, + ...(await products)?.data, ...(await pages).data, - ...(await filters).data, + ...(await filters)?.data, ...(await layout).data, filterTypes: await filterTypes, params: productListParams, diff --git a/packages/magento-product/components/ProductListItems/filteredProductList.tsx b/packages/magento-product/components/ProductListItems/filteredProductList.tsx index 8399435ec1..476c82c83d 100644 --- a/packages/magento-product/components/ProductListItems/filteredProductList.tsx +++ b/packages/magento-product/components/ProductListItems/filteredProductList.tsx @@ -11,7 +11,7 @@ export function parseParams( query: string[], filterTypes: FilterTypes, search: string | null = null, -): ProductListParams | false { +): ProductListParams | undefined { const categoryVariables: ProductListParams = { url, filters: {}, sort: {}, search } const typeMap = filterTypes @@ -59,7 +59,7 @@ export function parseParams( return undefined }, undefined) - return error ? false : categoryVariables + return error ? undefined : categoryVariables } export function extractUrlQuery(params?: { url: string[] }) {